《Effective Objective-C 2.0》 备忘录

242 阅读3分钟
1Accustoming Yourself to Objective-C熟悉Objective-C
1.1Familiarize Yourself with Objective-C’s Roots了解Objective-C语言的起源
1.2Minimize Importing Headers in Headers在类的头文件中尽量少引入其他头文件
1.3Prefer Literal Syntax over the Equivalent Methods多用字面量语法,少用与之等价的方法
1.4Prefer Typed Constants to Preprocessor #define多用类型常量,少用#define预处理指令
1.5Use Enumerations for States, Options, and Status Codes用枚举表示状态、选项、状态码


2Objects, Messaging, and the Runtime对象、消息、运行期
2.1Understand Properties理解"属性"这一概念
2.2Access Instance Variables Primarily Directly When Accessing Them Internally在对象内部尽量直接访问实例变量
2.3Understand Object Equality理解"对象等同性"这一概念
2.4Use the Class Cluster Pattern to Hide Implementation Detail以"类族模式"隐藏实现细节
2.5Use Associated Objects to Attach Custom Data to Existing Classes在既有类中使用关联对象存放自定义数据
2.6Understand the Role of objc_msgSend理解objc_msgSend的作用
2.7Understand Message Forwarding理解消息转发机制
2.8Consider Method Swizzling to Debug Opaque Methods用"方法调配技术"调试"黑盒方法"
2.9Understand What a Class Object Is理解"类对象"的用意


3Interface and API Design接口与API设计
3.1Use Prefix Names to Avoid Namespace Clashes用前缀避免命名空间冲突
3.2Have a Designated Initializer提供"全能初始化方法"
3.3Implement the description Method实现description方法
3.4Prefer Immutable Objects尽量使用不可变对象
3.5Use Clear and Consistent Naming使用清晰而协调的命名方式
3.6Prefix Private Method Names为私有方法名加前缀
3.7Understand the Objective-C Error Model理解Objective-C错误模型
3.8Understand the NSCopying Protocol理解NSCopying协议


4Protocols and Categories协议与分类
4.1Use Delegate and Data Source Protocols for Interobject Communication通过委托与数据源协议进行对象间通信
4.2Use Categories to Break Class Implementations into Manageable Segments将类的实现代码分散到便于管理的数个分类之中
4.3Always Prefix Category Names on Third-Party Classes总是为第三方类的分类名称加前缀
4.4Avoid Properties in Categories勿在分类中声明属性
4.5Use the Class-Continuation Category to Hide Implementation Detail使用"class-continuation分类"隐藏实现细节"
4.6Use a Protocol to Provide Anonymous Objects通过协议提供匿名对象


5Memory Management内存管理
5.1Understand Reference Counting理解引用计数
5.2Use ARC to Make Reference Counting Easier以ARC简化引用计数
5.3Release References and Clean Up Observation State Only in dealloc在dealloc方法中只释放引用并解除监听
5.4Beware of Memory Management with Exception-Safe Code编写"异常安全代码"时留意内存管理问题
5.5Use Weak References to Avoid Retain Cycles以弱引用避免保留环
5.6Use Autorelease Pool Blocks to Reduce High-Memory Waterline以"自动释放池块"降低内存峰值
5.7Use Zombies to Help Debug Memory-Management Problems用"僵尸对象"调试内存管理问题
5.8Avoid Using retainCount不要使用retainCount


6Blocks and Grand Central Dispatch块与大中枢派发
6.1Understand Blocks理解"块"这一概念
6.2Create typedefs for Common Block Types为常用的块类型创建typedef
6.3Use Handler Blocks to Reduce Code Separation用handler块降低代码分散程度
6.4Avoid Retain Cycles Introduced by Blocks Referencing the Object Owning Them用块引用其所属对象时不要出现保留环
6.5Prefer Dispatch Queues to Locks for Synchronization多用派发队列,少用同步锁
6.6Prefer GCD to performSelector and Friends多用GCD,少用performSelector系列方法
6.7Know When to Use GCD and When to Use Operation Queues掌握GCD及操作队列的使用时机
6.8Use Dispatch Groups to Take Advantage of Platform Scaling通过Dispatch Group机制,根据系统资源状况来执行任务
6.9Use dispatch_once for Thread-Safe Single-Time Code Execution使用dispatch_once来执行只需运行一次的线程安全代码
6.10Avoid dispatch_get_current_queue不要使用dispatch_get_current_queue


7The System Frameworks系统框架
7.1Familiarize Yourself with the System Frameworks熟悉系统框架
7.2Prefer Block Enumeration to for Loops多用块枚举,少用for循环
7.3Use Toll-Free Bridging for Collections with Custom Memory-Management Semantics对自定义其内存管理语义的collection使用无缝桥接
7.4Use NSCache Instead of NSDictionary for Caches构建缓存时选用NSCache而非NSDictionary
7.5Keep initialize and load Implementations Lean精简initialize与load的实现代码
7.6Remember that NSTimer Retains Its Target别忘了NSTimer会保留其目标对象