How to solve "String Interpolation" gives a description of debugging for an optional value, did you want to make it explicit? "in Xcode 8.3 beta?

Starting with beta version 8.3, warnings about the "Line Interpolation" cylinders give a description of debugging for an optional value, did you want to make it explicit? "appeared in my code.

For example, a warning appeared in the following situation up, where the parameters can lead to zero:

let msg = "*** Error \(options["taskDescription"]): cannot load \(sUrl) \(error)" 

As previously developed, it was optional for me (and for the compiler) that the options be interpolated as "zero". But the compiler changed his mind.

What the compiler offers is to add a String constructor with a description like this:

 let msg = "*** Error \(String(describing: options["taskDescription"])): cannot load \(sUrl) \(error)" 

Obviously, the results are clear, but, in my opinion, very cumbersome. Is there a better option? Should I fix all of these warnings or is it better to wait for the next beta?

Screenshot for description

+45
swift swift3 string-interpolation optional
Mar 01 '17 at 22:05
source share
6 answers

This change made in this pull request due to the fact that the interpolation of Optional(...) in the resulting string is often undesirable, and can be especially unexpected in cases with implicitly expanded options . You can see a full discussion of this change on the mailing list here .

As mentioned in the discussion of stretching request (although unfortunately not Xcode), one more pleasant way to silence a warning than using String(describing:) is to add a cast to the optional type of what you interpolate, 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 generalized 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 

Another possible way to turn off an alert is to access Optional debugDescription :

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

Although it is worth noting that the documentation for CustomDebugStringConvertible prevents CustomDebugStringConvertible from directly accessing.

+57
Mar 01 '17 at 22:21
source share

It seems that using String (description: optional) is simple.

default value? does not make sense for non-strings, for example, int. If Int is zero, you want the log to show "nil" not a default other Int, for example. 0.

Some codes of the site for testing:

 var optionalString : String? = nil var optionalInt : Int? = nil var description_ = "" description_ = description_ + "optionalString: \(String(describing: optionalString))\r" description_ = description_ + " optionalInt: \(String(describing: optionalInt))\r" print(description_) 

Exit

 optionalString: nil optionalInt: nil 
+12
Mar 28 '17 at 11:50
source share

After upgrading to Xcode 8.3 and receiving a large number of warning messages, I came up with the following, which is more like the original output behavior, which is easy to add, reduces the verbosity of using "String (description :)" in both code and output.

Basically, add an optional extension that gives a line describing the thing in optional, or just "zero" if not specified. Also, if the item in the optional is a string, put it in quotation marks.

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

And use on the 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 the help at the following link:

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

+6
Mar 28 '17 at 22:26
source share

Two easy ways to solve this problem.

Option 1:

The first may be a "forced expansion" of the value that you would like to return using bang (!)

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

Output:

 5 

Option 2:

Another way that might be the best way is to safely-deploy the value you want to return.

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

Output:

 5 

We recommend going with option 2.

Tip. Avoid deploying power (!) If possible, as we are not sure that we will always have a value to be deployed.

+6
Jul 12 '17 at 16:55
source share

See Ole Begeman's fix for this. I like it. Does it create ??? which you can then use as follows:

 var someValue: Int? = 5 print("The value is \(someValue ??? "unknown")") // β†’ "The value is 5" someValue = nil print("The value is \(someValue ??? "unknown")") // β†’ "The value is unknown" 
+3
Jun 28 '17 at 19:38 on
source share

Double-click on the yellow triangle displayed in the line containing this warning. This will show FixIt with two solutions.

Screenshot added

  • Use String(describing:) to disable this warning:

    Using this, it will become String(describing:<Variable>)

    Eg .: String(describing: employeeName)

  • Provide a default value to avoid this warning:

    Using this, it will become (<Variable> ?? default value)

    For example: employeeName ?? "Anonymous" as! String employeeName ?? "Anonymous" as! String

+2
Jul 05 '17 at 5:27
source share



All Articles