Swift3可选链在IF条件下出现错误? [英] Swift3 optionals chaining in IF conditions bug?

查看:89
本文介绍了Swift3可选链在IF条件下出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码在Swift 2.3中工作得很好,我不明白为什么我必须拆开TestClass来检查数字是否大于4.这是链接可选项的全部目的,以节省额外的调用.

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.

现在要执行此操作,我必须检查是否testClass != nil(或对if let语句使用隐式解包),然后检查计数.

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
{

}

推荐答案

可选比较运算符已从Swift 3中删除. SE-0121

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

您需要编写如下内容:

if test?.optionalInt ?? 0 > 4
{

}

这篇关于Swift3可选链在IF条件下出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆