如果语句不是 运行 - iOS
If statement not running - iOS
我有一个 iOS
应用程序,其中包含一些简单且合乎逻辑的 if-statements
。但是他们不会 运行 我不明白为什么。这是我的简单代码:
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
if ((day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6))) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else if ((day == (2 || 4 || 5 || 6)) && (day != (1 || 3 || 7))) {
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}
}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
}
我做错了什么?
你可以把断点放在if语句上,然后检查是否调用了if语句。
你有点过于复杂了。
if(day==1 || day==2 || day==3){
[self runSetupVX_4];
}else{
[self runSetupVX_2];
}
应该可以解决问题
这可以编译,但它做的事情与您想要实现的完全不同:
day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6)
语句 OR 1、3 和 7,然后是 OR 2、4、5 和 6,然后再执行比较。
正确的做法是将day
分别与1、3、7进行比较。如果任何比较成功,也可以保证日期不是 2、4、5 或 6:
if (day == 1 || day == 3 || day == 7)
...
if (day == 2 || day == 4 || day == 5 || day == 6)
...
你也可以用switch
重写这个:
switch(day) {
case 1:
case 3:
case 7:
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
break;
case 2:
case 4:
case 5:
case 6:
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
break;
}
如果每个 if 语句的第一部分为真,那么第二部分每次都为假。所以你不必检查第二部分。
您可以这样修改您的代码:
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
if (day ==1 || day==3 || day==7) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else{
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
}
希望这对你有用:
if (day == 1 || day == 2 || day == 3) {
// Run the calendar setup code for
// Sunday/Tuesday OR Saturday.
[self runSetupVX_4];
}
else if (day == 2 || day == 4 || day == 5 || day == 6) {
// Run setup code for Monday
// Wednesday, Thursday and Friday.
[self runSetupVX_2];
}
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
//if ((day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6))) {
//here u will get 0 or 1 from this (1 || 3 || 7) && (day != (2 || 4 || 5 || 6)))
//And u r comparing with 0 or 1 with day so please check below code how to compare
if (((day == 1 || 3 || 7) && (day != 2 || 4 || 5 || 6)) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else if ((day == 2 || 4 || 5 || 6)&& (day != 1 || 3 || 7)) {
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}
}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
} //you have problems with comparing variable please check it
我有一个 iOS
应用程序,其中包含一些简单且合乎逻辑的 if-statements
。但是他们不会 运行 我不明白为什么。这是我的简单代码:
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
if ((day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6))) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else if ((day == (2 || 4 || 5 || 6)) && (day != (1 || 3 || 7))) {
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}
}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
}
我做错了什么?
你可以把断点放在if语句上,然后检查是否调用了if语句。
你有点过于复杂了。
if(day==1 || day==2 || day==3){
[self runSetupVX_4];
}else{
[self runSetupVX_2];
}
应该可以解决问题
这可以编译,但它做的事情与您想要实现的完全不同:
day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6)
语句 OR 1、3 和 7,然后是 OR 2、4、5 和 6,然后再执行比较。
正确的做法是将day
分别与1、3、7进行比较。如果任何比较成功,也可以保证日期不是 2、4、5 或 6:
if (day == 1 || day == 3 || day == 7)
...
if (day == 2 || day == 4 || day == 5 || day == 6)
...
你也可以用switch
重写这个:
switch(day) {
case 1:
case 3:
case 7:
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
break;
case 2:
case 4:
case 5:
case 6:
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
break;
}
如果每个 if 语句的第一部分为真,那么第二部分每次都为假。所以你不必检查第二部分。
您可以这样修改您的代码:
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
if (day ==1 || day==3 || day==7) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else{
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
}
希望这对你有用:
if (day == 1 || day == 2 || day == 3) {
// Run the calendar setup code for
// Sunday/Tuesday OR Saturday.
[self runSetupVX_4];
}
else if (day == 2 || day == 4 || day == 5 || day == 6) {
// Run setup code for Monday
// Wednesday, Thursday and Friday.
[self runSetupVX_2];
}
-(void)viewDidLoad {
[super viewDidLoad];
// Run setup code.
int day = [self currentDay];
//if ((day == (1 || 3 || 7) && (day != (2 || 4 || 5 || 6))) {
//here u will get 0 or 1 from this (1 || 3 || 7) && (day != (2 || 4 || 5 || 6)))
//And u r comparing with 0 or 1 with day so please check below code how to compare
if (((day == 1 || 3 || 7) && (day != 2 || 4 || 5 || 6)) {
// Run the calendar setup code for
// Sunday/Tuesday OR saturday.
[self runSetupVX_4];
}
else if ((day == 2 || 4 || 5 || 6)&& (day != 1 || 3 || 7)) {
// Run setup code for Monday
// wednesday, thursday and friday.
[self runSetupVX_2];
}
}
-(int)currentDay {
NSDateComponents *component = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [component weekday];
} //you have problems with comparing variable please check it