摘要:
Block语法:1 int (^sum)(int, int) = ^(int a, int b) {2 return a+b;3 };以上代码定义了一个名为sum的Block对象,它带有两个int参数,返回int,等式右边就是Block的具体实现。调用方法:1 int i = sum(10, 20);2 NSLog(@"%i", i);使用typedef定义Block: 1 typedef int (^Sum) (int, int); 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool {
阅读全文
posted @ 2013-04-28 16:36
蓝易
阅读(204)
推荐(0)
编辑
摘要:
Protocol简单来说就是一系列不属于任何类的方法列表,其中声明的方法可以被任何类实现,这种模式一般称为代理模式(delegation)。通过Protocol定义各种行为,在不同的场景采用不同的实现方式。在Xcode上新建文件,选择Objective-C protocol方式创建Protocol:Protocol填写协议名:因为Protocol可以被任何类实现,所以只会生成一个头文件:ButtonDelegate.h 1 #import <Foundation/Foundation.h> 2 3 @class Button; 4 5 // Protocol本身可以继承 6 @pr
阅读全文
posted @ 2013-04-28 16:35
蓝易
阅读(509)
推荐(0)
编辑
摘要:
一、Category的使用无论一个类设计的如何完美,都不可避免的会遇到没有预测到的需求,那怎么扩展现有的类呢?当然,继承是个不错的选择。但是Objective-C提供了一种特别的方式来扩展类,称为Category,可以动态的为现有的类添加新的行为。例如,我们现在有一个Person类。Person.h1 @interface Person : NSObject2 3 - (void) test;4 5 @endPerson.m1 #import "Person.h"2 3 @implementation Person4 5 - (void)test {6 NSLog(@&qu
阅读全文
posted @ 2013-04-28 16:34
蓝易
阅读(378)
推荐(0)
编辑