Effective Objective-C 2.0(读书笔记)三

2017-03-16  本文已影响8人  木子影

#第三章 接口与API设计
  讲述文件与方法命名的规范。

十五:用前缀避免命名空间冲突

要点

十六:提供全能初始化方法

一种必须走自定义初始化方法的思路:

-(id)init{
       return [self initWithWidth5.0f andHeight:10.0f];
}

-(id)init{
        @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Must use initWithWidth:andHeight: instead."] userInfo:nil];
}

全能初始化方法的调用链一定要维系,再继承的时候。每个子类的全能初始化方法都应调用其超类的对应方法,并逐层向上。

要点:

十七:实现description方法

利用字典实现类的打印:
-(NSString*)description
{
        return [NSString stringWithFormat:@"<%@: %p, %@>",[self class],self,
        @{@"title":_title,
             @"latitude":@(_latitude),
             @"longitude":@(_longitude)
            }];
}

要点:

十八:尽量使用不可变对象

十九:使用清晰而协调的命名方式

要点:

20:为私有方法加前缀

要点

21:理解OC错误模型

要点

二十二:理解NSCopying协议

要点

上一篇 下一篇

猜你喜欢

热点阅读