IOS开发(58)之在 Block Object 中获取变量
2013-05-09 22:20
295 查看
1 前言
通过本次学习,我们就能够理解在 Objective-C 方法和在 Block Objects 中获取变量的区别。2 代码实例
main.m#import <Foundation/Foundation.h> #import "TestDemo.h" int main(int argc, const char * argv[]) { @autoreleasepool { TestDemo *test = [[TestDemo alloc] init]; // [test log1]; // [test simpleMethod]; // [test simpleMethod2]; // [test simpleMethod3]; // [test callCorrectBlockObject]; // [test simpleMethod4]; // [test log2]; // [test scopeTest]; [test scopeTest2]; } return 0; }
TestDemo.h
#import <Foundation/Foundation.h> @interface TestDemo : NSObject @property (nonatomic, strong) NSString *stringProperty; -(void)log1; - (void) simpleMethod; - (void) simpleMethod2; - (void) simpleMethod3; - (void) callCorrectBlockObject; - (void) simpleMethod4; -(void)log2; - (void) scopeTest; -(void) scopeTest2; @end
TestDemo.m
#import "TestDemo.h" @implementation TestDemo @synthesize stringProperty; void (^independentBlockObject)(void) = ^(void){ NSInteger localInteger = 10; NSLog(@"local integer = %ld", (long)localInteger); localInteger = 20; NSLog(@"local integer = %ld", (long)localInteger); }; -(void)log1{ independentBlockObject(); } //只读 - (void) simpleMethod{ NSUInteger outsideVariable = 10; NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"obj1", @"obj2", nil]; [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSUInteger insideVariable = 20; NSLog(@"Outside variable = %lu", (unsigned long)outsideVariable); NSLog(@"Inside variable = %lu", (unsigned long)insideVariable); /* Return value for our block object */ return NSOrderedSame; }]; } //可该 - (void) simpleMethod2{ __block NSUInteger outsideVariable = 10; NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"obj1", @"obj2", nil]; [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSUInteger insideVariable = 20; outsideVariable = 30; NSLog(@"Outside variable = %lu", (unsigned long)outsideVariable); NSLog(@"Inside variable = %lu", (unsigned long)insideVariable); /* Return value for our block object */ return NSOrderedSame; }]; } //获得self - (void) simpleMethod3{ NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"obj1", @"obj2", nil]; [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSLog(@"self = %@", self); /* Return value for our block object */ return NSOrderedSame; }]; } //无变化Block Object要获得 self需要传参数进去 void (^correctBlockObject)(id) = ^(id self){ NSLog(@"self = %@", self); }; - (void) callCorrectBlockObject{ correctBlockObject(self); } //修改类属性 - (void) simpleMethod4{ NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"obj1", @"obj2", nil]; [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSLog(@"self = %@", self); self.stringProperty = @"Block Objects"; NSLog(@"String property = %@", self.stringProperty); /* Return value for our block object */ return NSOrderedSame; }]; } //在独立 Block Object 内部,你不能使用 dot notation 读写一个已声明属性,在这个场景中可以使用这个合成属性的 getter and setter 方法来代替 dot notation: void (^correctBlockObject1)(id) = ^(id self){ NSLog(@"self = %@", self); /* This will work fine */ [self setStringProperty:@"Block Objects"]; /* This will work fine as well */ NSLog(@"self.stringProperty = %@",[self stringProperty]); }; -(void)log2{ correctBlockObject1(self); } //当出现在内联 Block Objects 中,有一条非常重要的规则你必须记住:内联 Block Objects 在其词法区域 会为这些变量复制值。此处发生的事情是在执行 Block 的地方 Block Object 自身一直有一个 integerValue 变量的只读复制。 typedef void (^BlockWithNoParams)(void); - (void) scopeTest{ NSUInteger integerValue = 10; /*************** Definition of internal block object ***************/ BlockWithNoParams myBlock = ^{ NSLog(@"Integer value inside the block = %lu", (unsigned long)integerValue); }; /*************** End definition of internal block object ***************/ integerValue = 20; /* Call the block here after changing the value of the integerValue variable */ myBlock(); NSLog(@"Integer value outside the block = %lu",(unsigned long)integerValue); } //可改变变量值测试 -(void) scopeTest2{ __block NSUInteger integerValue = 10; /*************** Definition of internal block object ***************/ BlockWithNoParams myBlock = ^{ NSLog(@"Integer value inside the block = %lu", (unsigned long)integerValue); }; /*************** End definition of internal block object ***************/ integerValue = 20; /* Call the block here after changing the value of the integerValue variable */ myBlock(); NSLog(@"Integer value outside the block = %lu", (unsigned long)integerValue); } @end
运行结果
2013-05-09 22:25:34.087 BlockObjectParamTest[946:303] Integer value inside the block = 20
2013-05-09 22:25:34.089 BlockObjectParamTest[946:303] Integer value outside the block = 20
3 结语
以上就是所有内容,希望对大家有所帮助。Demo代码下载:http://download.csdn.net/detail/u010013695/5350819
相关文章推荐
- iOS开发获取本机手机号码
- 【零基础学习iOS开发】【02-C语言】06-变量与内存
- iOS开发之获取WIFI信号强度
- IOS开发——获取本地音频文件(属性/信息)
- iOS开发之百度免费API使用案例(身份证号获取地址、手机号查归属地、MD5解密等)
- 李洪强iOS开发-网络新闻获取数据思路回顾
- iOS开发-进阶:通过URL地址,从网络上获取图片
- iOS开发Swift篇—(二)变量和常量
- iOS开发之获取系统相册中的图片与视频(内带url转换)
- iOS开发- 动态获取UIWebView高度
- IOS开发 SDWebImage获取网络图片的尺寸
- IOS开发实例-获取用户当前地理坐标
- 【iOS开发笔记21/50】获取应用程序的名称和版本号
- iOS开发之获取系统相册中的图片与视频(内带url转换)
- iOS开发- 获取精确剩余电量
- iOS开发 获取图片点击位置的颜色
- IOS开发 手机联系人获取与使用ABAddressBookRef
- IOS开发 获取手机通讯录
- IOS开发需要注意的property变量声明