Swift类:参考周期

时间:2015-01-03 09:44:08

标签: class swift strong-references

当我运行以下程序时,它会产生分段错误。能帮我解决一下原因吗?感谢

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)

2 个答案:

答案 0 :(得分:4)

问题在于打印时的无限递归。一旦你设置完整的周期,打印一个动物,你打印其配偶,打印其配偶,永远打印其配偶等,直到你用完堆栈空间和崩溃。

你需要通过打印出一只动物的配偶来打破这种情况,而不需要打印那种动物的完整图片,如下所示:

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

注意,您必须完全使用协议和var实现Printable以避免此问题,否则您将获得默认实现,这仍然会遇到问题。

顺便说一句,Swift样式约定是在=->之类的内容之间放置空格,然后{等等。事实上,如果不这样做,偶尔会导致编译问题)。陪审团仍在a: b vs a:b,但我发现后者有点难以阅读。

答案 1 :(得分:0)

您的代码会触发堆栈溢出。 description方法包含spouse描述,该描述将在永无止境的周期中触发其spouse等的描述。试试这个:

func description() -> String {
  return "name= \(name) and age=\(age) spouse=\(spouse?.name)"
}