0

Hi all real newbie question here.

I have an array like this:

 var daysInMonth = Array<([MyCustomClass], NSDate)>()

How can I append an element to this?

I am having difficulty doing so. Trying something like this:

daysInMonth.append([MyCustomClass](), someDate)

or

daysInMonth.append(  ([MyCustomClass](), someDate)   )

will not work (i'd like to add an empty array initial above of type MyCustomClass, as well as some date that I have) but these are failing (error missing parameter #2 in call)

Any thoughts on what I am lacking in my syntax?

Thanks!

2
  • So you're trying to append 2 separate values at once? Commented Aug 28, 2014 at 3:32
  • 1
    I am not sure what you are trying to achieve but it has a bit of a "code smell" about it. Rather than using an array of types it is probably better design to define a new struct that has an array of MyCustomClass and an NSDate and then declaring an array of those structs Commented Aug 28, 2014 at 3:42

2 Answers 2

2

It looks like a swift bug to me. The swift compiler cannot correctly parse the "( (...) )" as passing in a tuple to a function.

If I break the append operation into two statements, it works.

var daysInMonth = Array<([MyCustomClass], NSDate)>()

let data = ([MyCustomClass()], NSDate()) // assuming MyCustomClass init() taks no parameter 

daysInMonth.append(data) 

note: It was [MyCustomClass]() in your question, which is incorrect.

Sign up to request clarification or add additional context in comments.

4 Comments

Which is better because it is more clear and each statement is doing one thing. But to use the new syntax would be even better.
@Zaph Yeah, I expect them to be equivalent. So I am bit surprised by the 'bug' (if it is indeed one)
Thanks Anthony. Appreciate your time.
@Anthony hey I had a question so apparently by doing [MyCustomClass()] it ends up instantiating a new instance of MyCustomClass() and adding it to the array - this produces an array as the first parameter with one object in the array. I actually don't want that, rather an empty array as the first parameter (later i'm going to add my own values). I was going crazy before I realized this because it always had one extra element in the array (and this was why). If I remove the ()'s it doesn't work (same error). Thanks!
1

Try declaring your array using the newer Array syntax instead:

var daysInMonth = [([MyCustomClass], NSDate)]()

Then, this works:

daysInMonth.append(([MyCustomClass](), NSDate()))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.