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

原创粉丝点击