Objective-C if 语句中的感叹号是什么意思?

发布于 2024-11-18 13:56:39 字数 50 浏览 1 评论 0原文

我想知道 if(!anObject) 中的感叹号是什么意思。

I am wondering what the exclamation mark in if(!anObject) means.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

长不大的小祸害 2024-11-25 13:56:39

它是布尔 NOT 运算符,也称为否定。

!true == false;
!false == true;

It is the boolean NOT operator also called negation.

!true == false;
!false == true;
指尖凝香 2024-11-25 13:56:39

这就是 逻辑 NOT 运算符,即 if( thisThisIsNotTrue ) { doStuff }.

That is the Logical NOT operator, i.e., if( thisThisIsNotTrue ) { doStuff }.

红墙和绿瓦 2024-11-25 13:56:39

它是一个 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 @"+".

守望孤独 2024-11-25 13:56:39

如果它总是相加,那么你的字符串永远不会是“+”。

除非 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 "!"....

温馨耳语 2024-11-25 13:56:39

正如每个人都提到的只是一个 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.

纵山崖 2024-11-25 13:56:39

你不应该添加“!”到“if”中条件的开始。您的代码表示,如果运算符的文本不是 +,则添加,依此类推。你的代码应该是这样的;

-(IBAction) 计算结果 {

a = [txtOperand1.text intValue];
b = [txtOperand2.text intValue];

if ([txtOperator.text isEqualToString: @"+"]) {
    int sum=a+b;
    [result setText: [NSString stringWithFormat:@"%d", sum]];

} else if ([txtOperator.text isEqualToString: @"-"]) {
    int sum=a-b;
    [result setText: [NSString stringWithFormat:@"%d", sum]];
}
else if  ([txtOperator.text isEqualToString: @"/"]) {
    int sum=a/b;
    [result setText: [NSString stringWithFormat:@"%d", sum]];

}
else if  ([txtOperator.text isEqualToString: @"*"]) {
    int sum=a * b;
    [result setText: [NSString stringWithFormat:@"%d", sum]];


}
else [result setText:@"nothing"]; 

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 {

a = [txtOperand1.text intValue];
b = [txtOperand2.text intValue];

if ([txtOperator.text isEqualToString: @"+"]) {
    int sum=a+b;
    [result setText: [NSString stringWithFormat:@"%d", sum]];

} else if ([txtOperator.text isEqualToString: @"-"]) {
    int sum=a-b;
    [result setText: [NSString stringWithFormat:@"%d", sum]];
}
else if  ([txtOperator.text isEqualToString: @"/"]) {
    int sum=a/b;
    [result setText: [NSString stringWithFormat:@"%d", sum]];

}
else if  ([txtOperator.text isEqualToString: @"*"]) {
    int sum=a * b;
    [result setText: [NSString stringWithFormat:@"%d", sum]];


}
else [result setText:@"nothing"]; 
妥活 2024-11-25 13:56:39

那么它在这样的语句中的使用怎么样(取自在线课程示例):

(在此示例中,按下一个按钮,单击该按钮后,下面的代码将在“默认”状态和“选定”状态之间“切换”州):

- (IBAction)flipCard:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
}

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):

- (IBAction)flipCard:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文