使用纯代码创建一个视图对象:
1 2 3 UIView *subView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];subView.backgroundColor = [UIColor whiteColor]; [self .view addSubview:subView];
这段代码可以转换成另外一种等价写法:
1 2 3 4 5 6 UIView *subView = ({ UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; view.backgroundColor = [UIColor whiteColor]; [self .view addSubview:view]; view; });
使用字面量语言
1 2 NSArray *arrayA = [NSArray arrayWithObjects:object1, object2, object3, nil ];NSArray *arrayB = @[object1, object2, object3];
使用字面量语法的缺点:使用字面量创建都是不可变对象,如果想创建可变对象需要复制一份:
1 NSMutableArray *mutableArray = [@[@1 , @2 , @3 ] mutableCopy];
使用类型常量替换#define预处理指令
1 2 #define ANIMATION_DURATION 1.0 static const NSTimeInterval kAnimationDuration = 1.0 ;
代码结构
1 2 3 4 5 6 7 8 9 10 11 12 #pragma mark - life cycle #pragma mark - UITableViewDataSource #pragma mark - CustomDelegate #pragma mark - event response #pragma mark - private methods #pragma mark - getters and setters
获取当前导航控制器
1 2 3 4 5 6 #pragma mark Implement @protocol PCNavigationDelegate - (UINavigationController *)navigationController { UITabBarController *tabBar = (UITabBarController *)self .window.rootViewController; return (UINavigationController *)tabBar.selectedViewController; }
Effective-objectivec-读书笔记-Item-4-如何正确定义常量
http://www.dewen.net.cn/q/5522/C+++%E5%90%84%E7%A7%8D%E5%85%A8%E5%B1%80%E5%B8%B8%E9%87%8F%E7%9A%84%E5%A3%B0%E6%98%8E%E6%96%B9%E5%BC%8F%E7%9A%84%E4%BC%98%E7%BC%BA%E7%82%B9%EF%BC%9F