block相关

对于block对self的操作:

//不能单独在block块内使用self
void (^incorrectBlockObject)(void) = ^{
    NSLog(@"self = %@", self); /* self is undefined here */
};
//可以通过传递参数的方式使用self
void (^correctBlockObject)(id) = ^(id self){ 
    NSLog(@"self = %@", self);
};
- (void) callCorrectBlockObject{
    correctBlockObject(self);
}

//方法内部的block块可以单独使用self
- (void) simpleMethod{
    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中的点语法

//可以在方法内部的block块中使用点语法
#import "AppDelegate.h"

@interface AppDelegate()
@property (nonatomic, copy) NSString *stringProperty; 
@end

@implementation AppDelegate
- (void) simpleMethod{
    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块中只能使用[self propertyXXX]
void (^incorrectBlockObject)(id) = ^(id self){ 
     NSLog(@"self = %@", self);
     /* Should use setter method instead of this */
     self.stringProperty = @"Block Objects";         
     /* Compile-time Error */
     /* Should use getter method instead of this */
     NSLog(@"self.stringProperty = %@", self.stringProperty); 
    /* Compile-time Error */
};

//使用[self propertyXXX]代替
void (^correctBlockObject)(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]);
};
block块会对变量进行copy操作

typedef void (^BlockWithNoParams)(void);
- (void) scopeTest{
    NSUInteger integerValue = 10;
    BlockWithNoParams myBlock = ^{
        NSLog(@"Integer value inside the block = %lu",
              (unsigned long)integerValue);
    };
    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);
}

//输出:
/*
2015-03-31 11:55:57.557 testBlock[20096:527288] Integer value inside the block = 10
2015-03-31 11:55:57.557 testBlock[20096:527288] Integer value outside the block = 20
*/
//使用__block
typedef void (^BlockWithNoParams)(void);
- (void) scopeTest{
    __block NSUInteger integerValue = 10; //加入block前缀
    BlockWithNoParams myBlock = ^{
        NSLog(@"Integer value inside the block = %lu",
              (unsigned long)integerValue);
    };
    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);
}

//输出:
/*
2015-03-31 11:55:57.557 testBlock[20096:527288] Integer value inside the block = 20
2015-03-31 11:55:57.557 testBlock[20096:527288] Integer value outside the block = 20
*/
若要在block块内改变变量的值,也要使用__block

- (void)simpleMethod{
    __block NSUInteger outsideVariable = 10;
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:@[@2,@4,@3]];
    NSArray * array2 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSUInteger insideVariable = 20;
        outsideVariable = 50;
        NSLog(@"Outside variable is %ld", (long)outsideVariable);
        NSLog(@"Inside variable is %ld", (long)insideVariable);
}
//输出:
/*
2015-03-31 11:55:57.556 testBlock[20096:527288] Outside variable is 50
2015-03-31 11:55:57.556 testBlock[20096:527288] Inside variable is 20

*/



转载于:https://my.oschina.net/u/1782374/blog/394188

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值