我正在编写一个简单的类来测试如何将对象列表传递给方法并访问方法中的列表。
class baseMakeUp {
var Brand: String
var Color: String
var Rating: Int = 0
init (Brand: String, Color: String) {
self.Brand = Brand
self.Color = Color
}
}
var listOfBaseMakeUp = [baseMakeUp]()
listOfBaseMakeUp.append(baseMakeUp(Brand:"testA" , Color:"don't knowA"))
listOfBaseMakeUp.append(baseMakeUp(Brand:"testB" , Color:"don't knowB"))
for testObject in listOfBaseMakeUp {
print(testObject.Brand)
}
func testMehod( listTempOb: [baseMakeUp] ) -> Void{
for ob in listTempOb {
print(ob.Brand)
}
}
但是当我在 testMehod函数中访问Brand属性时,它显示使用未解析的标识符' testObject'
我来自c#,所以发现有点尴尬,有人给我一些资源链接如何处理这些?
答案 0 :(得分:1)
你的功能写错了:
func testMehod( listTempOb: [baseMakeUp] ) -> Void{
for ob in listTempOb {
**print(testObject.Brand)**
}
}
print(testObject.Brand)
应更改为:print(ob.Brand)