This question already has an answer here:
- Array of tuples in Swift 3 answers
I searched google, but i cant find a way to append new element to array object in Swift. The error code "Missing arguments fot parameter "name" in call" is come up. My code is followed.
var arrayObj: [(id: Int, name: String)] = []
var idInt: Int = 1
var nameString: String = "hoge"
arrayObj.append(( // ----> "Missing arguments fot parameter "name" in call"
id: idInt,
name: nameString
))
if you know any solution, I'd be very glad. Thanks.
More fun workarounds:
and
Curiously, this works if you use a
typealias
:EDIT:
One more workaround:
Just assign the tuple to a temp variable:
Not sure why it doesn't work that way - just checked on a function, like this:
Result: the function works, the array method doesn't.
Update: Tried this code in a playground:
Result: it works when passing the
tuple
variable todoSomething
- using the literal tuple instead doesn't, although the compiler message is different:Apparently passing a literal tuple to a method of a generic class (where the tuple is the generic type) is not correctly handled by the compiler.
Update #2: I repeated the test on a non-generic class, but using a generic method, and in this case it worked:
So it's definitely a problem related to generic classes only.
I just want to clear some things up:
If you have an array of tuples with named elements, just like in the answer, you can use the syntax described below to add elements to the array:
If you have an array of tuples with not named elements, like this:
use this:
If you created a typealias for you tuple, you can add elements to the list with the tuple syntax: