Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift3 optionals chaining in IF conditions bug?

Tags:

swift

swift3

This code worked just fine in Swift 2.3 and I don't understand why I have to unwrap TestClass to check if number is bigger than 4. This is whole point of chaining optionals, to save additional call.

Now to make this work, I have to check if testClass != nil (or use implicit unwrap with if let statement) and then check count.

Is this really the only way?

import UIKit

class testClass
{
    var optionalInt:Int?
}

var test:testClass?

if test?.optionalInt > 4
{

}
like image 815
user2282197 Avatar asked Jan 06 '23 09:01

user2282197


1 Answers

It's not a bug. It is, alas, intentional. Implicit unwrapping of optionals in comparisons (>) has been removed from the language.

So, the problem now is that what's on the left side of the > is an Optional, and you can no longer compare that directly to 4. You have to unwrap it and get an Int, one way or another.

like image 74
matt Avatar answered Jan 07 '23 21:01

matt