Objective-C if 语句中的感叹号是什么意思?
我想知道 if(!anObject)
中的感叹号是什么意思。
I am wondering what the exclamation mark in if(!anObject)
means.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想知道 if(!anObject)
中的感叹号是什么意思。
I am wondering what the exclamation mark in if(!anObject)
means.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
它是布尔 NOT 运算符,也称为否定。
It is the boolean NOT operator also called negation.
这就是 逻辑 NOT 运算符,即
if( thisThisIsNotTrue ) { doStuff }.
That is the Logical NOT operator, i.e.,
if( thisThisIsNotTrue ) { doStuff }
.它是一个 C 运算符,简单的意思是“不”。所以 !YES == NO 和 !NO == YES 都是 true 语句。例如,
if (![txtOperator.text isEqualToString: @"+"])
检查 txtOperator.text 是否不等于 @"+"。It's a C operator, simply meaning "not". So !YES == NO and !NO == YES are both true statements.
if (![txtOperator.text isEqualToString: @"+"])
, for example, checks to see if txtOperator.text is NOT equal to @"+".如果它总是相加,那么你的字符串永远不会是“+”。
除非 txtOperator.txt 完全等于 @"+",否则逻辑将始终添加 a+b。
有趣的是,如果你确实传递了一个加号,它总是会减去,只有前两种情况会被击中,因为如果第一个不正确,第二个总是会如此。
基本上,去掉所有的“!”......
If it always adds, then your string is never "+".
The logic as you have it will always add a+b unless the txtOperator.txt is exactly equal to @"+".
Interestingly if you did pass a plus it would always subtract, only the first two cases would ever be hit because if the first was not true the second always would be.
Basically, take out all the "!"....
正如每个人都提到的只是一个 NOT 运算符,我相信可能会让您感到困惑的是括号 [],Objective C,来自一种称为“闲聊”的语言,它对对象使用发送消息方法,括号用于发送该消息信息。这些消息确实是函数。
As everyone has mention is just a NOT operator, what I believe might have confused you is the brackets [], Objective C, comes from a language called small talk, that uses a send message approach to objects, the brackets are used to send that message. The messages are really functions.
你不应该添加“!”到“if”中条件的开始。您的代码表示,如果运算符的文本不是 +,则添加,依此类推。你的代码应该是这样的;
-(IBAction) 计算结果 {
You should not add "!" to the start of condition in "if". Your code says that if operator's text is not +, then add and so on. Your code should be like this;
-(IBAction) calculateResult {
那么它在这样的语句中的使用怎么样(取自在线课程示例):
(在此示例中,按下一个按钮,单击该按钮后,下面的代码将在“默认”状态和“选定”状态之间“切换”州):
So what about its use in a statement like this (taken from an online class example):
(In this example, a button is pressed and upon clicking the button, the code below "toggles" between the Default state and the Selected state):