Swift 中的结构数组
Array of struct in Swift
元素迭代产生错误
could not find member 'convertFromStringInterpolationSegment'
println("\(contacts[count].name)")"
,而直接列表项打印正常。
我错过了什么?
struct Person {
var name: String
var surname: String
var phone: String
var isCustomer: Bool
init(name: String, surname: String, phone: String, isCustomer: Bool)
{
self.name = name
self.surname = surname
self.phone = phone
self.isCustomer = isCustomer
}
}
var contacts: [Person] = []
var person1: Person = Person(name: "Jack", surname: "Johnson", phone: "7827493", isCustomer: false)
contacts.append(person1)
var count: Int = 0
for count in contacts {
println("\(contacts[count].name)") // here's where I get an error
}
println(contacts[0].name) // prints just fine - "Jack"
for-in
循环迭代项目集合,并在每次迭代时提供实际项目而不是其索引。所以你的循环应该重写为:
for contact in contacts {
println("\(contact.name)") // here's where I get an error
}
注意这一行:
var count: Int = 0
对您的代码没有影响,因为 for-in
中的 count
变量被重新定义并且对嵌套在循环内的代码块可见。
如果您仍想使用索引,则必须将循环修改为:
for var count = 0; count < contacts.count; ++count {
或
for count in 0..<contacts.count {
最后,如果您同时需要索引和值,也许最简单的方法是通过 enumerate
全局函数,其中 returns (index, value) 元组的列表:
for (index, contact) in enumerate(contacts) {
println("Index: \(index)")
println("Value: \(contact)")
}
首先,你不应该在struct中使用init(),因为
结构在此代码块中具有初始值设定项 default.Then:
/*
var count: Int = 0
for count in contacts {
println("\(contacts[count].name)") // here's where I get an error
}
*/
您的变量 "count" 不是整数,它的类型是 "Person"。
试试这个:
/*
for count in contacts {
println(count.name) // It`s must be OKey.
}
*/
希望能帮到你,对不起我的不好English:D
元素迭代产生错误
could not find member 'convertFromStringInterpolationSegment'
println("\(contacts[count].name)")"
,而直接列表项打印正常。
我错过了什么?
struct Person {
var name: String
var surname: String
var phone: String
var isCustomer: Bool
init(name: String, surname: String, phone: String, isCustomer: Bool)
{
self.name = name
self.surname = surname
self.phone = phone
self.isCustomer = isCustomer
}
}
var contacts: [Person] = []
var person1: Person = Person(name: "Jack", surname: "Johnson", phone: "7827493", isCustomer: false)
contacts.append(person1)
var count: Int = 0
for count in contacts {
println("\(contacts[count].name)") // here's where I get an error
}
println(contacts[0].name) // prints just fine - "Jack"
for-in
循环迭代项目集合,并在每次迭代时提供实际项目而不是其索引。所以你的循环应该重写为:
for contact in contacts {
println("\(contact.name)") // here's where I get an error
}
注意这一行:
var count: Int = 0
对您的代码没有影响,因为 for-in
中的 count
变量被重新定义并且对嵌套在循环内的代码块可见。
如果您仍想使用索引,则必须将循环修改为:
for var count = 0; count < contacts.count; ++count {
或
for count in 0..<contacts.count {
最后,如果您同时需要索引和值,也许最简单的方法是通过 enumerate
全局函数,其中 returns (index, value) 元组的列表:
for (index, contact) in enumerate(contacts) {
println("Index: \(index)")
println("Value: \(contact)")
}
首先,你不应该在struct中使用init(),因为 结构在此代码块中具有初始值设定项 default.Then:
/*
var count: Int = 0
for count in contacts {
println("\(contacts[count].name)") // here's where I get an error
}
*/
您的变量 "count" 不是整数,它的类型是 "Person"。 试试这个:
/*
for count in contacts {
println(count.name) // It`s must be OKey.
}
*/
希望能帮到你,对不起我的不好English:D