I tried to enter the the first program into the playground
var errorCodeString: String?
//errorCodeString = "404"
print(errorCodeString)
It doesn’t matter if the second line is commented out or not but there is a yellow warning triangle next to the print statement. The warning is: “Expression implicitly from ‘String?’ to Any”
It doesn’t matter if the assignment statement is commented out or not, nothing is printed out in the playground side (right side) or in the console window.
I haven’t found an errata for this issue, any help to get past this would be very helpful. Is there a possible compiler setting that I don’t have correct?
Since you are dealing with an optional string, you need to unwrap its value when you need the value.
For example:
var errorCodeString: String?
// errorCodeString = "404"
if let error = errorCodeString {
print (error)
}
else {
print ("error is not set!")
}
Make sure that you fully the understand the optionals: Why need them, when and how to use them.
I fully understand what optionals are, and what they are used for. I even have a very good understanding on how they are implemented. I don’t appreciate the condencending tone. The post was in regards to why the code, copied directly from the book this forum was setup for did not seem to work as specified. I wanted to play with the syntax and ways of using optionals. A task that is impossible to do if the development environment isn’t working properly. A fact that was alluded to when I mentioned compiler options.
Instead of helping, you decide to create a useless post and add a snide comment in an attempt to make yourself look superior. Call me a jerk or get me knocked off this board but if this is the help I can expect to receive here I don’t need it.
For the record my problem is solved. The code listed in the book, that I posted here, works just fine as I expected. The advanced implementation questions I had have now been answered as I was able to explore and test the code.
this “issue” comes with swift 3 and newer version of Xcode 8.x.
there’s nothing wrong with your compiler options.
Let me say now compiler is more “vigilant”.
3 workaround to avoid this warning:
var errorCodeString: String?
errorCodeString = "404"
print(String(describing: errorCodeString))
print(errorCodeString!)
print(errorCodeString ?? "")
I personally prefer (where applicable) the last one.
Watch out for the second one, if you haven’t any value obviously that will crash the app