使用Swift,我试图创建一个充满对象的数组。我一直收到这个错误,实例成员joe(第一个值)不能用于类型‘Friend’(类)
然后我想打印每个对象的name的值。这是我的代码。
import UIKit
class Friend {
var name:String = "aName"
var athletic = 0
var brains = 0
var male:Bool = false
init (name:String, brains:Int, athletic:Int, male:Bool){
self.name=name
self.athletic=athletic
self.brains=brains
self.male=male
}
let joe = Friend(name: "Joe", brains: 2, athletic: 3, male: true)
let dave = Friend(name: "Dave", brains: 4, athletic: 4, male: true)
let brent = Friend(name: "Bent", brains: 5, athletic: 1, male: true)
let logan = Friend(name: "Logan", brains: 1, athletic: 5, male: true)
var allFriends: [Friend] = [joe, dave, brent, logan] //this is where the error occurs.
for i in allFriends {
print allFriends[i].name
}
}请帮忙谢谢您:)
发布于 2015-12-04 05:53:06
您缺少一个右大括号:
class Friend {
var name:String = "aName"
var athletic = 0
var brains = 0
var male:Bool = false
init (name: String, brains: Int, athletic: Int, male: Bool){
self.name=name
self.athletic=athletic
self.brains=brains
self.male=male
} // THIS IS THE MISSING BRACE
}
let joe = Friend(name: "Joe", brains: 2, athletic: 3, male: true)
let dave = Friend(name: "Dave", brains: 4, athletic: 4, male: true)
let brent = Friend(name: "Bent", brains: 5, athletic: 1, male: true)
let logan = Friend(name: "Logan", brains: 1, athletic: 5, male: true)
var allFriends = [joe, dave, brent, logan] //this is where the error occurs.
for i in allFriends {
print(i.name)
}https://stackoverflow.com/questions/34076807
复制相似问题