Reputation: 23
So my code is like this (Playground)
import Foundation
public class Node {}
var test: Node = Node()
var arrayTest = [Int, Node]()
arrayTest.append(2, test)
Error xCode shows @ append Line: Accessing members of protocol type value 'Int' is unimplemented
But if I change the second array value from "Node" to "String" everything works fine. If I delete "Int, " so that its only a Node Array it works too. What am I missing? Why doesn't it work like that?
Upvotes: 2
Views: 1755
Reputation: 623
Appending a tuple to an array worked as expected when my tuple had 2 elements. When I jumped to a tuple with 4 elements, then I got the "Accessing members..." message.
The straightforward approach didn't work.
myArray.append(tupleItem1, tupleItem2, tupleItem3, tupleItem4) // ! Accessing members of protocol type value ...
The suggested workaround didn't work.
myArray.append((tupleItem1, tupleItem2, tupleItem3, tupleItem4)) // ! Missing argument for parameter #2 in call
What did work was a two-step append.
let myElement = (tupleItem1, tupleItem2, tupleItem3, tupleItem4)
myArray.append(myElement)
Upvotes: 1
Reputation: 5811
And if otherwise you're building a dictionary, then it will look something like this:
var arrayTest = Dictionary<Int, Node>()
arrayTest[2] = test
Upvotes: 0
Reputation: 72760
If your goal is to store (Int, Node)
tuples in the array, then you should enclose the tuple in parenthesis, either when specifying the array type and when using append
:
var test: Node = Node()
var arrayTest = [(Int, Node)]()
// ^ ^
arrayTest.append((2, test))
// ^ ^
Upvotes: 2