【Cocoa and Object-c : Up and Running 笔记】05 Object-C 高级语法
来源:互联网 发布:功夫英语 知乎 编辑:程序博客网 时间:2024/05/01 16:55
委托
委托是一种设计模式。当一个事件发生时,委托允许一个对象把消息传递给其他对象。
成为一个框架类的委托:
第一步,声明类采用了类/父类名字后,方括号里类定义中的协议。
- //MyTableViewController.h
- @interface MyTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- @end
第二步,设置对象作为委托
- //MyTableViewController.m
- [tableView setDelegate:self];
- @end
第三步,实现委托方法
为自定义的类实现委托:
第一步,声明协议方法
- // Superman.h
- #import <Foundation/Foundation.h>
- @protocol SupermanDelegate <NSObject>
- - (void)dodgeBullet;
- - (void)seeThroughThings;
- - (void)fly;
- @optional
- - (void)eat;
- @end
- @interface Superman : NSObject
- // Create a property for the delegate reference
- @property (nonatomic, weak) id <SupermanDelegate> delegate;
- // Define other methods and properties
- @end
第二步,设置委托对象
- // Superman.m
- [self setDelegate:anObject];
第三步,开始发送委托消息
- // Superman.m
- [self.delegate fly];
Blocks
Blocks是添加到C,Objective-C和C++中一个语言级别的特性,它允许你创建不同的代码段,这些代码段就像值一样可以在方法和函数间传递。块是多核程序设计实现的基础。
声明block局部变量:
- returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
声明block属性:
- @property (nonatomic, copy) returnType (^blockName)(parameterTypes);
接收block作为一个方法参数:
- - (void)aMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
在方法调用中将block作为一个参数传递
- [someObject someMethodThatTakesABlock: ^returnType (parameters) {...}];
定义一个block类型:
- typedef returnType (^TypeName)(parameterTypes);
- TypeName blockName = ^returnType(parameters) {...};
0 0
- 【Cocoa and Object-c : Up and Running 笔记】05 Object-C 高级语法
- 【Cocoa and Object-c : Up and Running 笔记】04 Object-C 基础语法
- 【Cocoa and Object-c : Up and Running 笔记】06 总结
- 【Cocoa and Object-c : Up and Running 笔记】03 Simple C for Object-c
- 【Cocoa and Object-c : Up and Running 笔记】07 C语言内存模型
- 【Cocoa and Object-c : Up and Running 笔记】08 C指针(转)
- 【Cocoa and Object-c : Up and Running 笔记】02 基本认识
- [object-c] self and super
- Object-C Property and Synthesize
- Object-C NSArray and NSMutableArray
- objective-c invoke javascript object and function
- ANSI C and object-oriented programming
- Object C self and super 关键字
- Brad Cox and his Object-C
- Object-C系列-<push and pop>
- Object-C系列-<present and dismiss>
- Object-c基本语法
- object-c 语法
- 【js设计模式笔记---工厂模式】
- android不支持的c库与改进方法
- 微信营销如何提升转化率与购买率
- guava(4)函数式编程
- Linux shell 变量 数学 运算
- 【Cocoa and Object-c : Up and Running 笔记】05 Object-C 高级语法
- struts2中的拦截器
- linux下卸载mysql
- 数据挖掘中的分类和聚类的区别
- DeCAF: A deep convolutional activation feature for generic visual recognition
- 程序猿必须知道的算法(第一弹)回溯法
- IOS系统架构
- Aspose.Word控件实现Word文档的操作(创建和合并单元格)
- 连接到 Flash 调试器