Swift 类:引用循环
Swift class: reference cycle
当我运行下面的程序时,它产生了分段错误。你能帮我弄清楚为什么吗?谢谢
class Animal:NSObject{
var name:String!
var age:UInt!
weak var spouse:Animal?
init(name:String,age:UInt){
self.name=name
self.age=age
}
func description() ->String{ //to become printable
return "name= \(name) and age=\(age) spouse=\(spouse)"
}
}
let dog=Animal(name:"Lucky",age:3)
let cat=Animal(name:"Branson",age:4)
dog.spouse=cat
cat.spouse=dog //It doesnt crash if I comment this line out
println(dog)
您的代码触发了堆栈溢出。 description
方法包含 spouse
描述,这将依次触发其 spouse
的描述,如此循环往复。试试这个:
func description() -> String {
return "name= \(name) and age=\(age) spouse=\(spouse?.name)"
}
问题是您的打印中的无限递归。一旦你设置了完整的循环,打印一个动物,你打印它的配偶,它打印它的配偶,打印它的配偶等等直到你 运行 出栈 space 并崩溃。
你需要通过打印出动物的配偶而不调用该动物的完整打印来打破这一点,就像这样:
class Animal: NSObject {
// you should avoid using implicitly unwrapped optionals
// unless you absolutely have to for a specific reason that
// doesn’t appear to apply here (so remove the !s)
var name: String
var age: UInt
weak var spouse: Animal?
init(name: String, age: UInt) {
self.name = name
self.age = age
}
}
// to make something printable, you need to conform
// to the Printable protocol
extension Animal: Printable {
// And make description is a var rather than a function
override var description: String {
let spousal_status = spouse?.name ?? "None"
return "name=\(name) and age=\(age), spouse=\(spousal_status)"
}
}
let dog = Animal(name: "Lucky", age: 3)
let cat = Animal(name: "Branson", age: 4)
dog.spouse = cat
dog.description
cat.spouse = dog
println(dog) // Prints name=Lucky and age=3, spouse=Branson
请注意,您必须 Printable
完全使用协议和 var 来避免此问题,否则您将获得默认实现,但仍会遇到此问题。
顺便说一句,Swift 风格约定是在 =
、->
、{
等之间放置 spaces(实际上如果你不这样做,你偶尔会导致编译问题)。 a: b
与 a:b
的对比仍然悬而未决,尽管我发现后者更难阅读。
当我运行下面的程序时,它产生了分段错误。你能帮我弄清楚为什么吗?谢谢
class Animal:NSObject{
var name:String!
var age:UInt!
weak var spouse:Animal?
init(name:String,age:UInt){
self.name=name
self.age=age
}
func description() ->String{ //to become printable
return "name= \(name) and age=\(age) spouse=\(spouse)"
}
}
let dog=Animal(name:"Lucky",age:3)
let cat=Animal(name:"Branson",age:4)
dog.spouse=cat
cat.spouse=dog //It doesnt crash if I comment this line out
println(dog)
您的代码触发了堆栈溢出。 description
方法包含 spouse
描述,这将依次触发其 spouse
的描述,如此循环往复。试试这个:
func description() -> String {
return "name= \(name) and age=\(age) spouse=\(spouse?.name)"
}
问题是您的打印中的无限递归。一旦你设置了完整的循环,打印一个动物,你打印它的配偶,它打印它的配偶,打印它的配偶等等直到你 运行 出栈 space 并崩溃。
你需要通过打印出动物的配偶而不调用该动物的完整打印来打破这一点,就像这样:
class Animal: NSObject {
// you should avoid using implicitly unwrapped optionals
// unless you absolutely have to for a specific reason that
// doesn’t appear to apply here (so remove the !s)
var name: String
var age: UInt
weak var spouse: Animal?
init(name: String, age: UInt) {
self.name = name
self.age = age
}
}
// to make something printable, you need to conform
// to the Printable protocol
extension Animal: Printable {
// And make description is a var rather than a function
override var description: String {
let spousal_status = spouse?.name ?? "None"
return "name=\(name) and age=\(age), spouse=\(spousal_status)"
}
}
let dog = Animal(name: "Lucky", age: 3)
let cat = Animal(name: "Branson", age: 4)
dog.spouse = cat
dog.description
cat.spouse = dog
println(dog) // Prints name=Lucky and age=3, spouse=Branson
请注意,您必须 Printable
完全使用协议和 var 来避免此问题,否则您将获得默认实现,但仍会遇到此问题。
顺便说一句,Swift 风格约定是在 =
、->
、{
等之间放置 spaces(实际上如果你不这样做,你偶尔会导致编译问题)。 a: b
与 a:b
的对比仍然悬而未决,尽管我发现后者更难阅读。