你確定你真的想要一個數組中的字典嗎?你給的代碼表示更多的名爲列陣列,可以使用類似以下內容來實現:
struct Name {
var firstName : String
var lastName : String
}
var persons1 : Array<Name> = [
Name(firstName: "Foo", lastName: "Bar"),
Name(firstName: "John", lastName: "Doe")
]
persons1[0].firstName // "Foo"
var persons2 : Array<(firstName: String, lastName:String)> = [
(firstName: "Mary", lastName: "Mean"),
(firstName: "Foo", lastName: "Bar"),
(firstName: "John", lastName: "Doe")
]
persons2[1].firstName // "Bar"
這些都是正確的陣列和不客氣這樣使用下標。字典類型通常是鍵和值的組合,即暱稱作爲鍵,名稱作爲值。
var nickNames : [String:String] = [
"mame" : "Mary Mean",
"foba" : "Foo Bar",
"jodo" : "John Doe"]
nickNames["mame"]! // "Mary Mean"
在這裏,我們查找的鍵值,並獲得回報的可選值,這是我強行解開......
所有這些都可以相當容易地追加,但千萬注意,命名元組變體persons2
不遵循推薦的做法。還要注意,字典數組允許包含在我最後一次注入中建議的不同鍵上。
persons1.append(Name(firstName: "Some", lastName: "Guy"))
persons2.append(firstName: "Another", lastName: "Girl")
nickNames["anna"] = "Ann Nabel"
// Array of Dictionaries
var persons : [[String:String]] = [
[ "firstName" : "Firstly", "lastName" : "Lastly"],
[ "firstName" : "Donald", "lastName" : "Duck"]
]
persons.append(["firstName" : "Georg", "middleName" : "Friedrich", "lastName" : "Händel"])
問題:該錯誤是針對您的問題中未包含的另一行代碼,不是嗎? – Antonio 2014-11-04 12:27:25