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 1178
失恋的感觉
失恋的感觉 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:28

    This is a change that was made in this pull request due to the fact that interpolating Optional(...) into the resultant string is often undesirable, and can be especially surprising in cases with implicitly unwrapped optionals. You can see the full discussion of this change on the mailing list here.

    As mentioned in the pull request discussion (although unfortunately not by Xcode) – one slightly nicer way to silence the warning than the use of String(describing:) is to add a cast to the optional type of whatever you're interpolating, so for example:

    var i: Int? = 5
    var d: Double? = nil
    
    print("description of i: \(i as Int?)")    // description of i: Optional(5)
    print("description of d: \(d as Double?)") // description of d: nil
    

    Which can also be generalised to as Optional:

    print("description of i: \(i as Optional)") // description of i: Optional(5)
    print("description of d: \(d as Optional)") // description of d: nil
    

    In Swift 5, with the new string interpolation system introduced by SE-0228, another option is to add a custom appendInterpolation overload for DefaultStringInterpolation:

    extension DefaultStringInterpolation {
      mutating func appendInterpolation<T>(optional: T?) {
        appendInterpolation(String(describing: optional))
      }
    }
    
    var i: Int? = 5
    var d: Double? = nil
    
    print("description of i: \(optional: i)") // description of i: Optional(5)
    print("description of d: \(optional: d)") // description of d: nil
    

    And, if desired, you could even remove the argument label to disable the warning entirely within a module (or within a particular file if you mark it as fileprivate):

    extension DefaultStringInterpolation {
      mutating func appendInterpolation<T>(_ optional: T?) {
        appendInterpolation(String(describing: optional))
      }
    }
    
    var i: Int? = 5
    var d: Double? = nil
    
    print("description of i: \(i)") // description of i: Optional(5)
    print("description of d: \(d)") // description of d: nil
    

    Though personally I would prefer to keep the argument label.

    0 讨论(0)
  • 2020-12-12 19:33

    After updating to Xcode 8.3 and getting a lot of warning messages, I came up with the following that is more like the original output behavior, easy to add in, reduces the verboseness of using "String(describing:)" both in code and output.

    Basically, add an Optional extension that gives a String describing the thing in the optional, or simply "nil" if not set. In addition, if the thing in the optional is a String, put it in quotes.

    extension Optional {
        var orNil : String {
            if self == nil {
                return "nil"
            }
            if "\(Wrapped.self)" == "String" {
                return "\"\(self!)\""
            }
            return "\(self!)"
        }
    }
    

    And usage in a playground:

    var s : String?
    var i : Int?
    var d : Double?
    
    var mixed = "s = \(s.orNil)    i = \(i.orNil)   d = \(d.orNil)" // "s = nil    i = nil   d = nil"
    
    d = 3
    i = 5
    s = ""
    mixed = "s = \(s.orNil)    i = \(i.orNil)   d = \(d.orNil)" // "s = ""    i = 5   d = 3.0"
    
    s = "Test"
    d = nil
    mixed = "s = \(s.orNil)    i = \(i.orNil)   d = \(d.orNil)" // "s = "Test"    i = 5   d = nil"
    

    Thanks for help from following link:

    check-if-variable-is-an-optional-and-what-type-it-wraps

    0 讨论(0)
提交回复
热议问题