Swift3 linked to a chain in case of EU condition errors?

This code worked very well in Swift 2.3, and I don’t understand why I need to expand TestClassto check if the number is greater than 4. This is a whole point of options binding to save an extra call.

Now, to do this job, I have to check if there is testClass != nil(or use implicit expansion using the operator if let), and then check the quantity.

Is this really the only way?

import UIKit

class testClass
{
    var optionalInt:Int?
}

var test:testClass?

if test?.optionalInt > 4
{

}
+4
source share
3 answers

Additional comparison operators are removed from Swift 3. SE-0121

You need to write something like this:

if test?.optionalInt ?? 0 > 4
{

}
0
source

. , , . (>) .

, , > , 4. Int, .

+5

First of all, where do you initialize your testvar? Of course, it will be zero unless you attach importance to it!

And as for the extra chain, what the problem writes:

if let optionalInt = test?.optionalInt, optionalInt > 4
{

}

As always, security> brevity.

+5
source

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


All Articles