美文网首页iOS Tips
iOS 代码规范 之 Objective - C

iOS 代码规范 之 Objective - C

作者: _白丁_ | 来源:发表于2016-04-08 10:05 被阅读157次

    以下内容整理于我一位好朋友 性感小胡须

    他的博客
    他的github

    文件夹的创建规范: 按功能模块创建文件夹,子文件夹按MVC
    枚举的写法, 参照Apple的写法,查看UIView.h即可

    .h 中的规范

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController {
        UIView *_myView; // 方便查看枚举的写法,点进去之后往上翻即可看到
    }
    
    // 头文件声明的方法,注释写详细
    /**
     *  初始化
     *
     *  @param item 课程内容
     */
    - (void)initWithCourseModel:(NSObject *)CourseModel;
    
    @property (nonatomic, strong) NSObject *object;
    
    // Xcode7.0之后 NSArray<NSDate *>
    // Xcode7.0之前,在注释中写明数组里面的元素类型 ,如 :[NSDate]
    // @property (nonatomic, strong) NSArray<NSDate *> *dates;
    
    

    写注释的时候尽量表明用意和想法,不要直白的翻译。
    原则上是每一个方法都需要写注释的,有些直接通过方法名就能看出来的可以不用写注释
    初始化方法的返回类型,建议写instancetype, 不建议写id.

    // 常量定义,不要写宏
    static NSString *const reuseID = @"reuseID";
    static CGFloat const topMargin = 8.f;
    

    .m 中的规范

    @interface ViewController ()
    // 一、属性
    // 属性命名: 命名尽量和业务相关
    // 属性和实例变量均可,注意写注释就OK了
    
    /** 属性注释 */
    @property (nonatomic, strong) NSObject *object1;
    
    // 尽量不用或少用懒加载, 如果实在要用,希望在注释中写明
    /** xx属性(懒加载) */
    @property (nonatomic, strong) NSObject *lazyProperty;
    
    // 访问属性
    // 统一_xxx访问
    // 特殊情况: init和dealloc里面务必使用 _xxx 访问
    
    @end
    
    @implementation ViewController {
        // 变量命名注意以 _ 开头
        NSObject *_instanceVar; ///< 实例变量注释
    }
    
    // 一、控制器代码组织
    #pragma mark - Life Cycle
    - (instancetype)init {
        self = [super init];
        if (self) {
            //...配置
        }
        return self;
    }
    - (void)dealloc {} //这个放前面,移除通知和KVO之类的,看得更清晰
    - (void)loadView {} //这个一般用不到
    - (void)viewDidLoad {
    }
    // ....
    - (void)viewDidAppear:(BOOL)animated {}
    
    #pragma mark - Init View // 主要初始化一些视图之类的
    
    #pragma mark - Network Request // 网络请求
    
    // 系统协议放前面,自定义协议放后面
    #pragma mark - UITableViewDelegate
    #pragma mark - CustomDelegate
    
    #pragma mark - Event Response // 按钮点击,手势响应之类的
    
    #pragma mark - Private Methods // 封装的一些内部调用方法
    
    #pragma mark - Public Methods // 可供外部调用的接口
    
    #pragma mark - Getters And Setters // 这个就不多说了。存取
    
    

    二、方法风格.

    // 建议, 注意表明第一个参数的意思
    - (void)getxxxDataWithUid:(id)uid xxx:(id)xxx {
        
    }
    
    // 不建议, 这里当你调用的时候不知道getxxxData后面跟的是啥参数,语意不清。
    - (void)getxxxData:(id)uid xxx:(id)xxx {
    
    }
    
    // 按钮点击, 统一以onClick开头
    - (void)xxxButtonClick:(UIButton *)sender {
        
    }
    
    // 三、其他
    - (void)test {
        NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"value", @"key", nil];
        NSArray *array2 = [NSArray arrayWithObjects:@"1", @"2", nil];
    
        NSMutableDictionary *mtbDict2 = [NSMutableDictionary dictionary];
        [mtbDict2 setValue:@"value1" forKey:@"key1"];
        
        // if语句
        BOOL condition = YES;
        if (condition) {
            
        } else {
        
        }
    }
    
    

    如果你觉得我的文章对你有帮助,向我发个红包吧!
    我将衷心德感谢你对我的支持!你的支持就是我的动力!

    微信扫码向我发红包

    微信.jpeg

    支付宝扫码向我发红包

    支付宝.jpeg

    相关文章

      网友评论

        本文标题:iOS 代码规范 之 Objective - C

        本文链接:https://www.haomeiwen.com/subject/vubflttx.html