迅速内联,如果条件为nil [英] Inline if condition with nil in swift

查看:242
本文介绍了迅速内联,如果条件为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果条件如下,我正在尝试使用内联:

I am trying to use inline if condition as follows:

topDisplay.text = topDisplay.text!.rangeOfString(".") ? "Sth for true" : "Sth for false"

这里的想法是如果有.".在topDisplay.text中!然后做点什么,否则做点别的.如果没有",方法rangeOfString返回nil.被发现.因此,我想知道是否可以在内联条件表达式中检查nil.

The idea here is if there is "." in the topDisplay.text! then do something, if not, do something else. The method, rangeOfString, returns nil if no "." is found. So I am wondering is it possible to check nil within inline condition expression.

((顺便说一句,您可能会发现我正在尝试在斯坦福大学在线课程中添加."按钮用于计算器分配,并且仅使用一行来实现此功能,如提示所述))

((btw, you might find out that I am trying to add "." button for calculator assignment in Stanford's online course, and to use only one line to implement this function as the hint describes))

推荐答案

所以我想知道是否可以在内联条件表达式中检查nil.

So I am wondering is it possible to check nil within inline condition expression.

好的. rangeOfString(".") != nil是一个 boolean 表达式,它可以 用作条件表达式中的条件:

Sure. rangeOfString(".") != nil is a boolean expression which can be used as the condition in the conditional expression:

topDisplay.text = topDisplay.text!.rangeOfString(".") != nil ? "Sth for true" : "Sth for false"

这篇关于迅速内联,如果条件为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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