7.if条件语句
for example:
BOOL mrHiggieIsMean = YES;//BOOL类型值,以YES和NO来区分 if (mrHiggieIsMean) { NSLog(@"Confirmed: he is super mean"); }
if-else if-else语法
8.switch(expression){
case value:
break;
}
与其他编程语言语法一样
9.for循环
for example:
NSArray *newHats = @[@"Cowboy", @"Conductor", @"Baseball"]; for (NSString *hat in newHats) { NSLog(@"Trying on new %@ hat", hat); if([mrHiggie tryOnHat:hat]) { NSLog(@"Mr. Higgie loves it"); } else { NSLog(@"Mr. Higgie hates it"); } }
针对于NSDictionary(键值对):
NSDictionary *funnyWords = @{ @"Schadenfreude": @"pleasure derived by someone from another person's misfortune.", @"Portmanteau": @"consisting of or combining two or more separable aspects or qualities", @"Penultimate": @"second to the last" }; for (NSString *word in funnyWords) { NSString *definition = funnyWords[word]; NSLog(@"%@ is defined as %@", word, definition); }
10.block 代码块,类似于object,只不过前边不用“*”,而是用“^”;
for example:
(一)Blocks with no arguments:
void (^logMessage)(void) = ^{//第一个void为返回类型,第二个void为argument参数类型。 NSLog(@"Hello from inside the block"); };
logMessage();//调用block(代码块)
(二)Blocks with arguments:
基本类型:
void (^sumNumbers)(NSUInteger, NSUInteger) = ^(NSUInteger num1, NSUInteger num2){ NSLog(@"The sum of the numbers is %lu", num1 + num2); };
sumNumbers(45, 89);
sumNumbers(18, 56);、
对象类型:
NSArray和NSString类型
void (^logCount)(NSArray *) = ^(NSArray *array){ NSLog(@"There are %lu objects in this array", [array count]); }; logCount(@[@"Mr.", @"Higgie"]); logCount(@[@"Mr.", @"Jony", @"Ive", @"Higgie"]);
void (^myFirstBlock)(NSString *) = ^(NSString *string){ NSLog(@"Hello from inside the block include %@",string); }; myFirstBlock(@"Hello"); myFirstBlock(@"World");
天生我才必有用,千金散去还复来!