在Swift上使用AnyObject的NSArray

时间:2015-03-27 00:23:56

标签: swift ios8 xcode6

我怎样才能让它发挥作用:

var variable0 : Int = 10
var variable1 : [(name: String, title: String)] = []

variable1.append(name: "name 1", title: "title 1")
variable1.append(name: "name 2", title: "title 2")

var variable2 :NSArray = [variable0,variable1]

我想要一个带有这两个变量的NSArray。 但是得到了一个错误:

Type '[(name:String, title: String)]' does not conform to protocol 'AnyObject

1 个答案:

答案 0 :(得分:1)

您无法将其存储在NSArray中,但您可以将其存储为Array<Any>,因为元组不符合AnyObject

var variable0 : Int = 10
var variable1 : [(name: String, title: String)] = []

variable1.append(name: "name 1", title: "title 1")
variable1.append(name: "name 2", title: "title 2")

var variable2 :Array<Any> = [variable0,variable1]
println(variable2[0]) // 10
println(variable2[1]) // [(name 1, title 1), (name 2, title 2)]