问题是打印中的无限递归。一旦你设置了完整的循环,打印一个动物,你打印它的配偶,它打印它的配偶,它打印它的配偶等等,直到你用完堆栈空间并崩溃。
您需要通过打印出动物的配偶而不调用该动物的完整打印来打破这一点,如下所示:
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 风格约定是在, ->
, before等之间放置空格{
(事实上,如果不这样做,有时会导致编译问题)。虽然我发现后者有点难以阅读,但陪审团仍然在a: b
反对。a:b