Swift 游乐场中的分段错误
Segmentation Fault in Swift Playground
我是新来的,我在 Swift 做一些练习,我发现了这个问题:
当我尝试执行这段代码时,出现了分段错误,我不知道为什么。
class Persona{
private var nome:String?
private var cognome:String?
private var eta:Int?
public var Sesso:String{
get{
return Sesso
}
set{
if newValue=="M" || newValue == "m" {
Sesso="Maschio"
}
else{
Sesso="Femmina"
}
}
}
init(nome:String, cognome:String, eta:Int)
{
self.nome=nome
self.cognome=cognome
self.eta=eta
}
init(){}
func getNome()->String{ return nome! }
func getCognome()->String{ return cognome! }
func getEta()->Int{ return eta! }
func setNome(nome:String)->(){ self.nome=nome }
func setCognome(cognome:String)->(){ self.cognome=cognome }
func setEta(eta:Int)->(){ self.eta=eta }
}
var p1 = Persona(nome:"AAAA",cognome:"BBBB",eta:22)
p1.Sesso = "M"
p1.setEta(eta:44)
print("\(p1.getNome()) \t \(p1.getCognome()) \t \(p1.getEta()) \t \(p1.Sesso)")
p1.setEta(eta:22)
问题是您无法访问变量自身的 getter 和 setter。
您可以使用 didSet
public var Sesso : String{
didSet {
if Sesso == "M" || Sesso == "m" {
Sesso = "Maschio"
}
else{
Sesso = "Femmina"
}
}
}
您可以从 this answer and can also check apple documentation on Properties 了解更多关于 getters 和 setters 的信息,以获得更多说明。
您基本上是在尝试使用 computed property
In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.
这里的关键是"which do not actually store a value"。当您尝试使用 get
ter 时,可以看得更清楚,因为 print(p1.Sesso)
也会导致崩溃,因为您最终会陷入 getter 的无限循环,一遍又一遍地调用自己。你应该做的是定义计算的 属性 但同时定义另一个存储的 属性 实际保存数据:
private var _sesso:String = ""
public var Sesso:String{
get{
return _sesso
}
set{
if newValue=="M" || newValue == "m" {
_sesso="Maschio"
}
else{
_sesso="Femmina"
}
}
}
我是新来的,我在 Swift 做一些练习,我发现了这个问题:
当我尝试执行这段代码时,出现了分段错误,我不知道为什么。
class Persona{
private var nome:String?
private var cognome:String?
private var eta:Int?
public var Sesso:String{
get{
return Sesso
}
set{
if newValue=="M" || newValue == "m" {
Sesso="Maschio"
}
else{
Sesso="Femmina"
}
}
}
init(nome:String, cognome:String, eta:Int)
{
self.nome=nome
self.cognome=cognome
self.eta=eta
}
init(){}
func getNome()->String{ return nome! }
func getCognome()->String{ return cognome! }
func getEta()->Int{ return eta! }
func setNome(nome:String)->(){ self.nome=nome }
func setCognome(cognome:String)->(){ self.cognome=cognome }
func setEta(eta:Int)->(){ self.eta=eta }
}
var p1 = Persona(nome:"AAAA",cognome:"BBBB",eta:22)
p1.Sesso = "M"
p1.setEta(eta:44)
print("\(p1.getNome()) \t \(p1.getCognome()) \t \(p1.getEta()) \t \(p1.Sesso)")
p1.setEta(eta:22)
问题是您无法访问变量自身的 getter 和 setter。
您可以使用 didSet
public var Sesso : String{
didSet {
if Sesso == "M" || Sesso == "m" {
Sesso = "Maschio"
}
else{
Sesso = "Femmina"
}
}
}
您可以从 this answer and can also check apple documentation on Properties 了解更多关于 getters 和 setters 的信息,以获得更多说明。
您基本上是在尝试使用 computed property
In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.
这里的关键是"which do not actually store a value"。当您尝试使用 get
ter 时,可以看得更清楚,因为 print(p1.Sesso)
也会导致崩溃,因为您最终会陷入 getter 的无限循环,一遍又一遍地调用自己。你应该做的是定义计算的 属性 但同时定义另一个存储的 属性 实际保存数据:
private var _sesso:String = ""
public var Sesso:String{
get{
return _sesso
}
set{
if newValue=="M" || newValue == "m" {
_sesso="Maschio"
}
else{
_sesso="Femmina"
}
}
}