问题描述
我正在处理的本教程说,我必须禁用保存"按钮,直到文本字段中包含一些值为止.这是代码:
This tutorial I am currently working on says I have to disable the Save Button until the text field has some value in it.Here is the code:
saveButton.isEnabled = !text.isEmpty
text.isEmpty之前的感叹号是否表示启用了保存按钮,否则文本不是空的,与!=等于不等于的方式相同吗?我知道感叹号表示强制展开,但是我认为您在文本后面加上了感叹号.顺便说一句(我已经对其进行了测试,并且按照教程中的说明进行了工作)
Does the exclamation mark before text.isEmpty mean that the save button is enabled is the text is not empty same way that != mean not equal to?I know exclamation mark means force unwrap, but I thought you put the exclamation mark after the text.BTW(I have tested it and it works as the tutorial says so)
推荐答案
在text.isEmpty之前的惊叹号称为 逻辑NOT 运算符,它将布尔值取反.
The exclamation mark before text.isEmpty called Logical NOT operator, it inverts the boolean value.
saveButton.isEnabled = !text.isEmpty
表示如果text为空,则>不会启用,反之亦然.
means that if text is empty, the saveButton will not be enabled, and vice versa.
为了更清楚一点,如果我们尝试将其翻译为if语句,则应为:
To make it more clear, if we tried to translate it as an if-statement, it should be as:
if text.isEmpty { saveButton.isEnabled = false } else { saveButton.isEnabled = true }
这篇关于文字前的快速感叹号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!