gpt4 book ai didi

xcode - 实例成员不能用于类型

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:39 26 4
gpt4 key购买 nike

@IBAction func saveDetails(sender: AnyObject) {
Person.firstName = firstNameTF.text
Person.lastName = lastNameTF.text
}

上面是我试图实现的函数,下面是我试图创建实例并将文本字段中的数据存储在...中的类...我收到错误消息“实例成员“firstName”不能用于 Person 类型”。我几乎可以肯定我的类(class)已正确设置和初始化,所以我看不出问题出在哪里?

class Person {
var firstName : String = ""
var middleName : String? = nil
var lastName : String = ""
var yearOfBirth : Int? = nil
var age : Int! {
get {
guard let _ = yearOfBirth else {
return nil
}
return currentYear - yearOfBirth!
}
set {
yearOfBirth = currentYear - newValue
}
}

init(firstName: String, lastName: String, yearOfBirth: Int? = nil, middleName: String? = nil){
self.firstName = firstName
self.lastName = lastName
self.yearOfBirth = yearOfBirth
self.middleName = middleName
}
convenience init(firstName: String, lastName: String, age: Int, middleName: String? = nil) {
self.init(firstName: firstName, lastName: lastName, yearOfBirth: nil, middleName: middleName)
self.age = age
}

}

最佳答案

错误消息说您不能调用(类型)Person 的属性。

使用给定的初始化器创建一个Person instance

@IBAction func saveDetails(sender: AnyObject) {
let person = Person(firstName:firstNameTF.text, lastName:lastNameTF.text)
// do something with person
}

关于xcode - 实例成员不能用于类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36472475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com