数组:
NSArray* aa = @[@"123",@123,@YES];
[aa enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%d,%@",idx,obj);
}];
NSDictionary* dic = @{@"zc":@100,@"yy":@120};
[dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@,%@",key,obj); }];
附录:数字、数组以及字典的新初始化方式
NSNumber * number = @1;
NSArray * array = @[@"one", @"two"];
NSDictionary * dict = @{@"key1":@"value1", @"key2":@"value2"};
NSNumber *fortyTwo = @42; // 等价于 [NSNumber numberWithInt:42]
NSNumber *fortyTwoUnsigned = @42U; // 等价于 [NSNumber numberWithUnsignedInt:42U]
NSNumber *fortyTwoLong = @42L; // 等价于 [NSNumber numberWithLong:42L]
NSNumber *fortyTwoLongLong = @42LL; // 等价于 [NSNumber numberWithLongLong:42LL]
NSNumber *piFloat = @3.141592654F; // 等价于 [NSNumber numberWithFloat:3.141592654F]
NSNumber *piDouble = @3.1415926535; // 等价于 [NSNumber numberWithDouble:3.1415926535]
NSNumber *yesNumber = @YES; // 等价于 [NSNumber numberWithBool:YES]
NSNumber *noNumber = @NO; // 等价于 [NSNumber numberWithBool:NO]
NSArray * array = @[]; // 等价于 [NSArray array]
NSDictionary * dict = @{}; // 等价于 [NSDictionary dictionary]
2.转换方式:菜单->Edit ->Refactor->Convert to Modern Objective-C Syntax
三行代码教会你使用NSNotification:
这里是NSNotification最简单的使用,也是最常用的使用。是在MVC构架应用程序中链接View与Controller的最佳链接途径。
[[NSNotificationCenter defaultCenter] addObserver:self.text selector:@selector(setHidden:) name:@"yincang" object:nil];//注册消息
[[NSNotificationCenter defaultCenter] postNotificationName:@"yincang" object:[NSNumber numberWithBool:YES]];//调用消息
[[NSNotificationCenter defaultCenter] removeObserver:self.text name:@"yincang" object:nil];//注销消息
需要注意的是,注册过的消息在ViewDisapper的位置最好注销掉。
本例是在发消息调用self.text控件的 setHidden方法。