IOS开发(58)之在 Block Object 中获取变量
来源:互联网 发布:阿里云服务器申请流程 编辑:程序博客网 时间:2024/04/30 10:57
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开发(58)之在 Block Object 中获取变量
- IOS开发(59)之Block Object的调用
- Block如何在ios开发中应用
- Ios开发之block在ios开发中的应用
- iOS开发之 成员变量 解决block循环引用方案
- IOS开发基础Object-C( 15)-- block的使用方法
- iOS开发之Block
- iOS 开发之 GCD解析(block 如何被添加进 queue 中,以及 block 执行)
- iOS 开发之 GCD解析(block 如何被添加进 queue 中,以及 block 执行)
- ios学习笔记之block在ios开发中的应用
- ios学习笔记之block在ios开发中的应用
- ios学习笔记之block在ios开发中的应用
- ios学习笔记之block在ios开发中的应用
- ios学习笔记之block在ios开发中的应用
- IOS中 Block用法进阶二 局部变量和全局变量在Block中的使用
- (转自:ff20081528)IOS学习笔记之block在ios开发中的应用
- iOS开发之Block(1)
- iOS开发之Block(2)
- Linux软连接和硬链接
- 新浪微博Oauth2.0授权 获取Access Token以及API的使用
- vim python设置
- 对Spring中IOC和DI的理解
- 泛型的使用
- IOS开发(58)之在 Block Object 中获取变量
- 单选框回显效果的解决办法
- 黑马程序员-ADO.net-SQL语句进阶练习
- POJ 图论
- Java异常处理中,try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
- Android中使用WakeLock保证后台操作的顺利进行
- this class is not key value coding-compliant for the key buttonPressed.' 错误解决方式
- 链表常见面试题
- HttpClient的execute的阻塞问题