iOS 日常开发细节(代码篇)

1.宏定义

// ...表示宏定义的可变参数
// __VA_ARGS__:表示函数里面的可变参数
#ifdef DEBUG
#define QYLog(...) NSLog(__VA_ARGS__)
#else
#define QYLog(...)
#endif
// 偏好设置存储的好处
// 1.不需要关心文件名
// 2.快速进行键值对存储

// 补充:控制器的view
// UITabBarController控制器的view在一创建控制器的时候就会加载view
// UIViewController的view,才是懒加载。

2.枚举 enum,NS_ENUM,NS_OPTIONS

在iOS6和Mac OS 10.8以后Apple引入了两个宏NS_ENUM,NS_OPTIONS来重新定义C语言风格的enum,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。

enum{   
    UIViewAnimationTransitionNone,  
    UIViewAnimationTransitionFlipFromLeft,  
    UIViewAnimationTransitionFlipFromRight,  
    UIViewAnimationTransitionCurlUp,  
    UIViewAnimationTransitionCurlDown,  
} UIViewAnimationTransition;  

替换为:

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {  
    UIViewAnimationTransitionNone,//默认从0开始  
    UIViewAnimationTransitionFlipFromLeft,  
    UIViewAnimationTransitionFlipFromRight,  
    UIViewAnimationTransitionCurlUp,  
    UIViewAnimationTransitionCurlDown,  
};  

位移操作枚举定义

 
enum {  
    UIViewAutoresizingNone                 = 0,  
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,  
    UIViewAutoresizingFlexibleWidth        = 1 << 1,  
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,  
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,  
    UIViewAutoresizingFlexibleHeight       = 1 << 4,  
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5  
};  
typedef NSUInteger UIViewAutoresizing; // UIViewAutoresizing相当于NSUInteger的一个别名使用。  

替换为:

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {  
    UIViewAutoresizingNone                 = 0,  
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,  
    UIViewAutoresizingFlexibleWidth        = 1 << 1,  
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,  
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,  
    UIViewAutoresizingFlexibleHeight       = 1 << 4,  
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5  
};  

你可能感兴趣的:(iOS 日常开发细节(代码篇))