Swift:具有多个keys:values的关联数组
Swift: associative array with multiple keys:values
我不是 Swift 方面的专家,几个月来我一直在使用它来构建 Mac 应用程序。我想在内存中表示一个数据结构,如 PHP 关联数组 但在 Swift 中。假设我有一个 table 的数据要加载到内存中,具有以下 fields/records:
ID Surname Name
1 XXX YYY
2 ZZZ WWW
3 JJJ KKK
我想要获得的是一个关联数组,就像我在 PHP 中可以获得的那样:
$arr[1]["Surname"] = "XXX"
$arr[1]["Name"] = "YYY"
$arr[2]["Surname"] = "ZZZ"
$arr[2]["Name"] = "WWW"
我无法在 Swift 中找到正确的数据结构来获得相同的结果。我尝试使用以下代码:
class resObject: NSObject {
private var cvs = [Int: [String: String]]()
override init() {
self.cvs[0] = ["Name" : "XXX"]
self.cvs[0] = ["Surname" : "YYY"]
self.cvs[1] = ["Name" : "ZZZ"]
self.cvs[1] = ["Surname" : "WWW"]
for (key, arr) in cvs {
let sur = arr["Surname"]
let nam = arr["Name"]
println("Row \(key) - Surname: \(sur), Name: \(nam)")
}
super.init()
}
}
在我看来它非常接近,但它不起作用。我在输出中得到的是以下内容(我不关心 "Optional(s)":
Row 0 - Surname: Optional("XXX"), Name: nil
Row 1 - Surname: Optional("ZZZ"), Name: nil
我尝试在调试中进行一些测试,我注意到内存中保存的数据只是最后使用的 key:value 对的数据(即,如果我首先分配姓氏,然后分配姓名,我得到姓氏作为 nil 和具有正确值的名称)。
请考虑,如示例中,我在声明变量时不知道数据结构,所以我将其声明为空,稍后以编程方式填充它。
我不知道是我没有正确声明数据结构,还是 Swift 不允许这样做。任何帮助将不胜感激。
非常感谢。
问候,
阿莱西奥
一种方式是 Dictionary
的 structs
。考虑:
struct Person {
var firstName: String
var lastName: String
}
var peopleByID = [ Int: Person ]()
peopleByID[1] = Person(firstName: "First", lastName: "Last")
peopleByID[27] = Person(firstName: "Another", lastName: "LastName")
var myID = 1 // Try changing this to 2 later
if let p = peopleByID[myID] {
println("Found: \(p.firstName) with ID: \(myID)")
}
else {
println("No one found with ID: \(myID)")
}
然后您可以更新结构:
peopleByID[1].firstName = "XXX"
peopleByID[27].lastName = "ZZZ"
可以自由迭代:
for p in peopleByID.keys {
println("Key: \(p) value: \(peopleByID[p]!.firstName)")
}
请注意,单纯的 [Person] 数组并不那么热门,因为 ID:
-- 可能不是 Ints,但通常是 Strings
-- 即使它们仍然是 Int,数组占用的存储空间与编号最高的索引成比例,而 Dictionary 仅与存储的对象数量成比例占用存储空间。想象一下只存储两个 ID:523123 和 2467411。
编辑
看来您没有提前知道每个Person
对象的属性。这很奇怪,但你应该这样做:
struct Person {
var attributes = [String : String]() // A dictionary of String keys and String values
}
var peopleByID = [ Int : Person ]()
// and then:
var p1 = Person()
var p2 = Person()
p1.attributes["Surname"] = "Somename"
p1.attributes["Name"] = "Firstname"
p2.attributes["Address"] = "123 Main St."
peopleByID[1] = p1
peopleByID[2] = p2
if let person1 = peopleByID[1] {
println(person1.attributes["Surname"]!)
for attrKey in person1.attributes.keys {
println("Key: \(attrKey) value: \(person1.attributes[attrKey]!)")
}
}
我不是 Swift 方面的专家,几个月来我一直在使用它来构建 Mac 应用程序。我想在内存中表示一个数据结构,如 PHP 关联数组 但在 Swift 中。假设我有一个 table 的数据要加载到内存中,具有以下 fields/records:
ID Surname Name
1 XXX YYY
2 ZZZ WWW
3 JJJ KKK
我想要获得的是一个关联数组,就像我在 PHP 中可以获得的那样:
$arr[1]["Surname"] = "XXX"
$arr[1]["Name"] = "YYY"
$arr[2]["Surname"] = "ZZZ"
$arr[2]["Name"] = "WWW"
我无法在 Swift 中找到正确的数据结构来获得相同的结果。我尝试使用以下代码:
class resObject: NSObject {
private var cvs = [Int: [String: String]]()
override init() {
self.cvs[0] = ["Name" : "XXX"]
self.cvs[0] = ["Surname" : "YYY"]
self.cvs[1] = ["Name" : "ZZZ"]
self.cvs[1] = ["Surname" : "WWW"]
for (key, arr) in cvs {
let sur = arr["Surname"]
let nam = arr["Name"]
println("Row \(key) - Surname: \(sur), Name: \(nam)")
}
super.init()
}
}
在我看来它非常接近,但它不起作用。我在输出中得到的是以下内容(我不关心 "Optional(s)":
Row 0 - Surname: Optional("XXX"), Name: nil
Row 1 - Surname: Optional("ZZZ"), Name: nil
我尝试在调试中进行一些测试,我注意到内存中保存的数据只是最后使用的 key:value 对的数据(即,如果我首先分配姓氏,然后分配姓名,我得到姓氏作为 nil 和具有正确值的名称)。
请考虑,如示例中,我在声明变量时不知道数据结构,所以我将其声明为空,稍后以编程方式填充它。
我不知道是我没有正确声明数据结构,还是 Swift 不允许这样做。任何帮助将不胜感激。
非常感谢。 问候, 阿莱西奥
一种方式是 Dictionary
的 structs
。考虑:
struct Person {
var firstName: String
var lastName: String
}
var peopleByID = [ Int: Person ]()
peopleByID[1] = Person(firstName: "First", lastName: "Last")
peopleByID[27] = Person(firstName: "Another", lastName: "LastName")
var myID = 1 // Try changing this to 2 later
if let p = peopleByID[myID] {
println("Found: \(p.firstName) with ID: \(myID)")
}
else {
println("No one found with ID: \(myID)")
}
然后您可以更新结构:
peopleByID[1].firstName = "XXX"
peopleByID[27].lastName = "ZZZ"
可以自由迭代:
for p in peopleByID.keys {
println("Key: \(p) value: \(peopleByID[p]!.firstName)")
}
请注意,单纯的 [Person] 数组并不那么热门,因为 ID:
-- 可能不是 Ints,但通常是 Strings
-- 即使它们仍然是 Int,数组占用的存储空间与编号最高的索引成比例,而 Dictionary 仅与存储的对象数量成比例占用存储空间。想象一下只存储两个 ID:523123 和 2467411。
编辑
看来您没有提前知道每个Person
对象的属性。这很奇怪,但你应该这样做:
struct Person {
var attributes = [String : String]() // A dictionary of String keys and String values
}
var peopleByID = [ Int : Person ]()
// and then:
var p1 = Person()
var p2 = Person()
p1.attributes["Surname"] = "Somename"
p1.attributes["Name"] = "Firstname"
p2.attributes["Address"] = "123 Main St."
peopleByID[1] = p1
peopleByID[2] = p2
if let person1 = peopleByID[1] {
println(person1.attributes["Surname"]!)
for attrKey in person1.attributes.keys {
println("Key: \(attrKey) value: \(person1.attributes[attrKey]!)")
}
}