我是一名经验丰富的程序员,因此这种神秘的行为对我来说完全是个谜。
我有一个简单的 if
语句,仅当恰好有两个 bool 变量为 false
时才应输入该语句。但是,由于某种原因,当只有一个 if
语句为 false
时,就会输入 if
语句。
我的代码如下所示:
BOOL connected = [self connected];
NSLog(@"Connected to the internet: %@", connected ? @"YES" : @"NO");
BOOL notConnectedMessageShown = ((FOLDAppDelegate *)[[UIApplication sharedApplication] delegate]).notConnectedMessageShown;
NSLog(@"notConnectedMessageShown: %@", notConnectedMessageShown ? @"YES" : @"NO");
if (!connected && !notConnectedMessageShown);
{
NSLog(@"Entering if statement");
}
NSLog
打印以下内容:
"Connected to the internet: YES"
"notConnectedMessageShown: NO"
"Entering if statement"
我真的不明白。由于第一个变量首先是true
,根据我的编程技巧,整个if
语句应该被跳过吗?
有人知道这里发生了什么吗?
最佳答案
if
末尾有一个分号
if (!connected && !notConnectedMessageShown); <<--- this ; is wrong
这样,“true”条件的 block 就是空的,并且您的代码总是紧随其后进入该 block 。
应该是这样的:
if (!connected && !notConnectedMessageShown) <<-- see here
{
NSLog(@"Entering if statement");
}
关于Objective-C if 语句输入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11700506/