所以我的代码就像这样(游乐场)
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
但是,如果我从" Node"更改第二个数组值to" String"一切正常。 如果我删除" Int,"所以它只有一个节点阵列也可以工作。 我错过了什么?为什么它不像那样工作?
答案 0 :(得分:1)
如果你的目标是在数组中存储(Int, Node)
元组,那么你应该在指定数组类型和使用append
时将括号括在括号中:
var test: Node = Node()
var arrayTest = [(Int, Node)]()
// ^ ^
arrayTest.append((2, test))
// ^ ^
答案 1 :(得分:1)
当我的元组有2个元素时,将元组附加到数组按预期工作。当我跳到一个包含4个元素的元组时,我得到了“访问成员......”的消息。
直接的方法不起作用。
myArray.append(tupleItem1, tupleItem2, tupleItem3, tupleItem4) // ! Accessing members of protocol type value ...
建议的解决方法不起作用。
myArray.append((tupleItem1, tupleItem2, tupleItem3, tupleItem4)) // ! Missing argument for parameter #2 in call
工作是两步追加。
let myElement = (tupleItem1, tupleItem2, tupleItem3, tupleItem4)
myArray.append(myElement)
答案 2 :(得分:0)
如果不是你在构建字典,那么它看起来像这样:
var arrayTest = Dictionary<Int, Node>()
arrayTest[2] = test