我是一个经验丰富的程序员,所以这种神秘的行为对我来说完全是个谜
我有一个简单的if
语句,只有当正好有两个布尔变量是false
时,才应输入。但是,由于某些原因,当其中只有一个是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
这样,一个"真"条件的块就是空的,你的代码总是在它后面进入块
应该是这样的:
if (!connected && !notConnectedMessageShown) <<-- see here
{
NSLog(@"Entering if statement");
}