How to solve “String interpolation produces a debug description for an optional value; did you mean to make this explicit?” in Xcode 8.3 beta?

后端 未结 8 1215
失恋的感觉
失恋的感觉 2020-12-12 19:04

Since beta 8.3, zillions warnings \"String interpolation produces a debug description for an optional value; did you mean to make this explicit?\" appeared in my code.

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 19:26

    Swift 5

    My solution is making an extension which unwrap Optional object to Any.

    When you log the object or print it out, you can see the actual object or ⭕️ (combination from text and visual character). It's useful to look at, especially in the console log.

    extension Optional {
        var logable: Any {
            switch self {
            case .none:
                return "|⭕️"
            case let .some(value):
                return value
            }
        }
    }
    
    // sample
    var x: Int?
    print("Logging optional without warning: \(x.logable)")
    // → Logging optional without warning: |⭕️
    

提交回复
热议问题