iOS Developer

Objective-C编码规范精选

2017-05-23  本文已影响124人  kamous

代码不仅是可以编译的,同时应该是 “有效” 的。好的代码有一些特性:简明,自我解释,优秀的组织,良好的文档,良好的命名,优秀的设计以及可以被久经考验。 ——《禅与 Objective-C 编程艺术》

依据日常个人和团队编码习惯总结、挑选出几点Objective-C代码规范,整理出此文,持续更新。
多条规范和思路参考《禅与 Objective-C 编程艺术》一书,非常推荐一读。

命名规范

驼峰命名

static CSDataManager *gDataManager = nil; // good
static CSDataManager *dataManager = nil; // avoid

前缀

@interface OCBaseViewController : UIViewController
@end
@interface ViewController () {
    BOOL _hasViewed; // avoid
}
@property (nonatomic, assign) BOOL isToday; // good
@end
- (void)_privateMethod { // avoid
}

#pragma mark - Private Method
- (void)privateMethod { // good
}
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; // 执行性,good
+ (id)arrayWithArray:(NSArray *)array; // 返回性,good
+ (id)getArrayWithArray:(NSArray *)array; // 返回性,avoid

代码格式

空格

@property (nonatomic, strong) NSString* name;       // avoid
@property (nonatomic, strong) NSString *password;   // good
-(void)viewDidLoad{ // avoid
    [super viewDidLoad];
    if([self isOk]){ // avoid
        // ...
    }else{ // avoid
        // ...
    }
}

- (void)viewDidLoad { // good
    [super viewDidLoad];
    if ([self isOk]) { // good
        // ...
    } else { // good
        // ...
    }
}
    if ( i>10 ) { // avoid
        i ++; // avoid
    } else {
        i+=2; // avoid
    }
    
    // good
    if (i > 10) {
        i++;
    } else {
        i += 2;
    }
@property (nonatomic,readonly ,strong) NSString* name; // avoid
NSArray *array = @[@"A" , @"B" ,@"C"]; // avoid

@property (nonatomic, readonly, strong) NSString* name; // good
NSArray *array = @[@"A", @"B", @"C"]; // good

括号

    if (i > 10) // avoid
        i++;
    else
        i--;
    
    
    if (i > 10) { // good
        i++;
    } else {
        i--;
    }
// avoid
- (void)test
{
    if ([self isOK])
    {
        // ...
    }
    else
    {
        // ...
    }
}


// good
- (void)test {
    if ([self isOK]) {
        // ...
    } else {
        // ...
    }
}

属性

// avoid
@property (copy, nonatomic, readonly) NSString *name;
@property (nonatomic, assign, readonly) NSInteger age;
@property (readwrite, strong, nonatomic) CSCard *card;

// good
@property (nonatomic, readonly, copy) NSString *name;
@property (nonatomic, readonly, assign) NSInteger age;
@property (nonatomic, readwrite, strong) CSCard *card;
    @property (nonatomic, copy) NSString *name; // good

    NSMutableString * name = [[NSMutableString alloc] initWithString:@"User1"];
    CSUserModel *user = [CSUserModel new];
    user.name = name;
    [name appendString:@"0"];
    NSLog(@"%@", user.name); // output:User1
    
        
    @property (nonatomic, strong) NSString *name; // avoid

    NSMutableString * name = [[NSMutableString alloc] initWithString:@"User1"];
    CSUserModel *user = [CSUserModel new];
    user.name = name;
    [name appendString:@"0"];
    NSLog(@"%@", user.name); // output:User10, Something Wrong!

其它语法

    if (obj == nil && finish == YES && result == NO){ // avoid
        
    }
    
    if(!obj && finish && !result){ // good
        
    }

换行

// avoid
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion {
    // ...
}

// good
+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations
                 completion:(void (^)(BOOL finished))completion {
    // ...
}

注释

// avoid
- (void)test {
    //Just For Debug
    BOOL isTest = YES;//Main Logic
    //...
}

// good
- (void)test {
    // Just For Debug
    BOOL isTest = YES; // Main Logic
    // ...
}
/**
 执行xxx操作,可能失败。
 
 xxxxxxxxxxxxxxxxxx
 xxxxxxxxxxxxxxxxxx
 xxxxxxxxxxxxxxxxxx(具体使用事项)

 @param error NSError,-1:xxx;-2:xxxxx
 */
- (void)methodWithError:(NSError **)error {
    // ...
}

代码组织

#pragma

- (void)dealloc { /* ... */ }
- (instancetype)init { /* ... */ }

#pragma mark - View Lifecycle
- (void)viewDidLoad { /* ... */ }
- (void)didReceiveMemoryWarning { /* ... */ }

#pragma mark - Setter Getter
- (void)setCustomProperty:(id)value { /* ... */ }
- (id)customProperty { /* ... */ }

#pragma mark - IBActions
- (IBAction)onOkButtonClick:(id)sender { /* ... */ }

#pragma mark - Public
- (void)publicMethod { /* ... */ }

#pragma mark - Private
- (void)privateMethod { /* ... */ }

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { /* ... */ }

#pragma mark - Superclass
- (void)superClassMethod { /* ... */ }

#pragma mark - NSObject
- (NSString *)description { /* ... */ }
// avoid
@interface CodeStyleViewController () <UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate>
@end

// good
@interface CSViewController () <
UITableViewDelegate,
UITableViewDataSource,
UIActionSheetDelegate
>
@end
// avoid
- (void)method {
    if ([self conditionA]) {
        // some code..
        if ([self conditionB]) {
            // main logic...
        }
    }
}

// good
- (void)methodB {
    if (![self conditionA]) {
        return;
    }
    
    if (![self conditionB]) {
        return;
    }
    
    // some codeA..
    // main logic...
}

接口规范

@class CSShareViewController;
@protocol CSShareDelegate <NSObject> // avoid

- (void)shareFinished:(BOOL)isSuccess; // avoid

@end



@class CSShareViewController;
@protocol CSShareViewControllerDelegate <NSObject> // good

- (void)shareViewController:(CSShareViewController *)shareViewController // good
              shareFinished:(BOOL)isSuccess;

@end
// avoid
- (void)methodWithError:(NSError **)error {
    // ...
}

- (void)test1 {
    NSError *error = nil;
    if ([self methodWithError:&error]) { // avoid
        // Main Logic
    } else {
        // Handle Error
    }
}

// good
- (BOOL)methodWithError:(NSError **)error {
    // ...
}
- (void)test {
    NSError *error = nil;
    if ([self methodWithError:&error]) { // good
        // Main Logic
    } else {
        // Handle Error
    }
}

参考文章:
*《禅与 Objective-C 编程艺术》

我的博客原址:Objective-C编码规范精选

上一篇 下一篇

猜你喜欢

热点阅读