user2282197 user2282197 - 1 year ago 84
Swift Question

Swift3 optionals chaining in IF conditions bug?

This code worked just fine in Swift 2.3 and I don't understand why I have to unwrap

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


Answer Source

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

You need to write something like this:

if test?.optionalInt ?? 0 > 4