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 1176
失恋的感觉
失恋的感觉 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:16

    Two easier ways of dealing with this issue.

    Option 1:

    The first would be by "force-unwrapping" the value you would like to return using a bang (!)

    var someValue: Int? = 5
    print(someValue!)
    

    Output:

    5
    

    Option 2:

    The other way, which could be the better way - is to "safely-unwrap" the value you want returned.

    var someValue: Int? = 5
    
    if let newValue = someValue {
        print(newValue)
    }
    

    Output:

    5
    

    Would recommend to go with option 2.

    Tip: Avoid force unwrapping (!) where possible as we are not sure if we will always have the value to be unwrapped.

提交回复
热议问题