码迷,mamicode.com
首页 > 其他好文 > 详细

< Objective-C >block

时间:2015-06-09 21:32:27      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

块(block)的用法

        //无参无返回值
        void (^message)(void) = ^{
            NSLog(@"Hello World!\n");
        };
        
        //有参有返回值
        NSString *(^repeat)(NSString *, int) = ^(NSString *str, int times) {
            NSMutableString *result = [NSMutableString string];
            for(int i=0; i<times; i++) {
                [result appendString:str];
            }
            return result;
        };
        
        message();
        NSString *str = repeat(@"Hello\n",3);
        NSLog(@"%@",str);        

 

block块内部可以读取块外部,所在方法范围内的变量

block不能修改读取的变量

如果要修改,必须在变量前使用__block(双下划线)修饰

< Objective-C >block

标签:

原文地址:http://www.cnblogs.com/aY-Wonder/p/4564488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!