iOS约束
typeof NS_ENUM 替代enum
typeof NS_ENUM(NSInteger, ZColor) {
ZColorRed,
ZColorBlue
}
枚举类型的命名规则和函数的命名规则相同:命名时使用驼峰命名法,勿使用下划线命名法。
基本数据类型
避免使用基本类型, 用Foundation数据类型
基本 | Foundation数据类型 |
---|---|
int | NSInteger |
unsigned | NSUInteger |
float | CGFloat |
动画时间 | NSTimeInterval (重点) |
根据特性,字段注意使用NSUInteger
NSArray *items = @[@1, @2, @3];
for (int i = -1; i < items.count; i++) {
NSLog(@”%d”, i);
}
,for循环一次都没有进。
数组的count是NSUInteger类型的,-1与其比较时隐式转换成NSUInteger,变成了一个很大的数字:
来源:http://blog.sunnyxx.com/2014/12/20/64-bit-tips/
数据遍历
数组遍历还是更推荐用for-in或block版本
实验结果:http://blog.sunnyxx.com/2014/04/30/ios_iterator/
命名规范
错误,不要使用”and”来连接参数
(int)runModalForDirectory:(NSString )path andFile:(NSString )name andTypes:(NSArray *)fileTypes;
错误,不要使用”and”来阐明有多个参数
(instancetype)initWithName:(CGFloat)width andAge:(CGFloat)height;
正确,使用”and”来表示两个相对独立的操作(从设计上来说,这时候也可以拆分成两个独立的方法)
(BOOL)openFile:(NSString )fullPath withApplication:(NSString )appName andDeactivate:(BOOL)flag;
关键字
copy,weak(assign |assigin 可作用非OC对象(简单赋值), nil区别),strong
readonly,readwrite
__strong, __weak
nonatomic,atomic
- **block在非ARC模式 使用copy, 栈区-》 堆区, ARC模式下 strong 亦可
- NSString、NSArray、NSDictionary 经常使用copy关键字
可通过实现NSCopying, NSMutableCopying协议 - (id)copyWithZone: (NSZone*) zone
copy 修饰符
而不是去 复写 NSObject corp
@property 的本质是什么?
待整理