block相關

對於block對self的操做: ide

//不能單獨在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

*/
相關文章
相關標籤/搜索