-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-10952] xCode shows warnings about casting optional(?) value to Any while it unwrapped(!) #59619
Comments
The warning is correct: an implicitly-unwrapped optional type is unwrapped if it needs to be, and converting to Any is not a case where it needs to be. You can read more about this at Any ideas on how to make the error message more clear? |
Comment by Ido (JIRA) I’ve just noticed it happens also when I’m trying to print the value of Type!, there is a reason for me to forced unwrapped this variable, so why the compiler want me to treat it as optional if I told it it shouldn’t ever be with nil value? Sorry, but I still don’t know why the warning is correct when it’s just ignoring my force unwrapping. |
You aren't force-unwrapping it. |
Comment by Ido (JIRA) So what is the case that it would unwrap it automatically if I always have to give it a default value/force unwrap it in the expression itself? |
You do not always have to force-unwrap it in the expression itself. As described in the blog post, it will be unwrapped automatically if the context is expecting a non-optional version of the value. But |
Comment by Ido (JIRA) I think I got it now, thanks for the explanation! And about your question for more understandable message - You may change it to something like: Value of type 'Optional(Type!)' could not be converted to 'Any', you may specify a default value or force-unwrap it This would give more context to the developer about having an "auto unwrapped variable" that is an Optional in the time of the conversion and will give some examples for what should be done. |
Environment
macOS 10.14.4 (18E226)
xCode 11.0 beta (11M336w)
Additional Detail from JIRA
md5: 924d60396351aa05301bb722f224bcf9
Issue Description:
When trying to pass value with type `Type!`, Xcode shows this warning:
As you can see, Xcode ignoring that the type is already unwrapped and treats it as an optional value.
For example:
You may ignore the logic itself and focus on this line:
I'm getting the warning on the `font` variable, although you know it's defined as unwrapped value in the `UILabel` class:
The text was updated successfully, but these errors were encountered: