为什么 NSLog 不支持 Swift 对象(转)

https://segmentfault.com/a/1190000005668218

也就说:

1. 如果是 OC 的对象,重写 description 的 get 方法就可以了。

2. 如果不是 OC 对象,遵循协议  CustomDebugStringConvertible 就可以了。

import Foundation

print("Hello, World!")

class Foo: CustomDebugStringConvertible {
    var debugDescription: String {
        return "debug Foo."
    }
}

NSLog("(Foo())")

class Bar: NSObject {
    override var description: String {
        return "debug Bar."
    }
}

NSLog("%@", Bar())

输出:

Hello, World!
2016-06-09 10:59:30.465 NSLog[9843:180519] debug Foo.
2016-06-09 10:59:30.468 NSLog[9843:180519] debug Bar.
原文地址:https://www.cnblogs.com/xilifeng/p/5572103.html