The optional type "$ T11" cannot be used as a boolean; test for '! = nil' instead of installing Xcode 6 beta 7

Here is the code where I get the error:

for (key, value) in info { let fieldValue: AnyObject? = value if (!fieldValue || fieldValue?.length == 0) { // this line gives the error informationComplete = false; } } 

This is what Xcode suggests using, which causes another error:

 for (key, value) in info { let fieldValue: AnyObject? = value if ((!fieldValue || fieldValue?.length == 0) != nil) { //bool not convertible to string informationComplete = false; } } 

Help is appreciated.

thank you for your time

+5
source share
1 answer

Options are no longer considered logical expressions (as indicated in the Swift Reference - Change History ):

Options already implicitly evaluate true when they have a value and false when they do not, to avoid confusion when working with additional Bool values. Instead, do an explicit check against nil with == or! = To find out if an optional value contains a value.

therefore you should make it explicit as follows:

 if (fieldValue == nil || ... 

I remember what changed in beta 6 - did you use beta 5?

+11
source

Source: https://habr.com/ru/post/1201745/


All Articles