1 if
mrHiggieIsMean = NO; if (mrHiggieIsMean) { NSLog(@"Confirmed: he is super mean"); }
2 if else
BOOL mrHiggieIsMean = [mrHiggie areYouMean]; if (mrHiggieIsMean) { NSLog(@"Confirmed: he is super mean"); }else{ NSLog(@"No, actually he‘s really nice"); }
3 if - else if - else
NSNumber *meannessScale = [mrHiggie meannessScale]; if([meannessScale intValue] < 4) { NSLog(@"Mr. Higgie is on the nice side"); } else if([meannessScale intValue] < 8) { NSLog(@"Mr. Higgie is sorta nice but not really"); } else { NSLog(@"Mr. Higgie is definitely mean"); }
4 Equal strings
传递给 isEqualToString: 比较两个NSString,如果相同则会返回TRUE,反之返回FALSE。
可以用在if的条件判断中:
if([myString isEqualToString:otherString]) { NSLog(@"Hello from inside the if!"); }
5 switch case
NSInteger day = getDayOfWeek(); switch (day) { case 1: { NSLog(@"Monday"); break; } case 2: { NSLog(@"Tuesday"); break; } /* snip Wednesday through Saturday */ case 7: { NSLog(@"Sunday"); break; } }
6 Switch on enums
switch使用的闪光点在与枚举结合使用。
typedef NS_ENUM(NSInteger, DayOfWeek) { DayOfWeekMonday = 1, DayOfWeekTuesday = 2, DayOfWeekWednesday = 3, DayOfWeekThursday = 4, DayOfWeekFriday = 5, DayOfWeekSaturday = 6, DayOfWeekSunday = 7 };
时间: 2024-10-10 23:34:12