OC 代碼規(guī)范

代碼規(guī)范

好久沒更新了,確實(shí)忙,前段時間正好聽前輩提起這方面的事情,>醍醐灌頂,哈哈,不廢話了,把之前為了重構(gòu)外包的代碼,為項目>組寫的規(guī)范貼出來給大家分享
提醒:
1.因為是規(guī)范,所以沒有為什么,可以自己Google下
2.一些自己的習(xí)慣請忽略。。。。。。。。。

目的:寫出執(zhí)行迅速、便于理解、便于維護(hù)、便于擴(kuò)展、不易出錯的代碼。


1. 使用泛型(制定容器類的類型)

NSArray<NSString *> *strings = @[@"sun", @"yuan"];
NSDictionary<NSString *, NSNumber *> *mapping = @{@"a": @1, @"b": @2};

2. __kindof

- (void)viewDidLoad {
    [super viewDidLoad];
     UIButton *but1 = [self getSubView];//不需要強(qiáng)轉(zhuǎn)
}

- (__kindof UIView *)getSubView {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    return button;
}

3. 使用nonnull、nullable、null_resettable

nonnull 聲明的屬性不能為空(getter方法和setter方法都有)
@property (nullable, nonatomic, copy) NSString *userId;

//在NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END之間,定義的所有對象屬性和方法
默認(rèn)都是nonnull
NS_ASSUME_NONNULL_BEGIN
@interface ViewController : UIViewController
@property (nonatomic, weak) UILabel *label;
@property (nonatomic, weak) UIButton * __nullable button;
@property (null_resettable, nonatomic, strong) NSArray *friends;
@end
NS_ASSUME_NONNULL_END

//nullable 聲明的屬性可以為空
@property (nullable, nonatomic, copy) NSString *str;
//null_resettable setter可以賦空值,getter方法取值不能為空
@property(null_resettable, nonatomic,strong) UIView *view;

4. 多用類型常量(含有類型信息),少用#define預(yù)處理指令

define

#define kAnimationDuration @"男"

類型常量

//只在本實(shí)現(xiàn)文件中使用,前面字母加k
static const NSTimeInterval kAnimationDuration = 0.5;

//全局使用
//.h文件
extern NSString *const QYBoySex;
//.m文件
NSString *const QYBoySex = @"男”;

5.分類定義常量

  • 例如定義顏色 和 NSUserDefaults 的 Key
#pragma mark - UserDefaults Keys
typedef NSString *QYUserDefaultsKey;

///name
FOUNDATION_EXPORT QYUserDefaultsKey const KQYUserName;
///password
FOUNDATION_EXPORT QYUserDefaultsKey const KQYUserPassword;

#pragma mark - QY Color Hex
typedef NSString *HJDColorHex;

///blue
FOUNDATION_EXPORT HJDColorHex const QYNavBlue;
FOUNDATION_EXPORT HJDColorHex const QYButtonBlue;

6. 在類的頭文件中盡量少引用其他頭文件,使用向前聲明該類的方式

QYPerson:

#import <Foundation/Foundation.h>
//#import “QYBoy.h"
@class QYBoy;
@interface QYPerson : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) QYBoy *boy;

@end

#import "QYPerson.h"
#import "QYBoy.h"

@implementation QYPerson

@end

QYBoy:

#import <Foundation/Foundation.h>
//#import "QYPerson.h"
@class QYPerson;

@interface QYBoy : NSObject

@property (nonatomic, copy) NSString *name;

- (void)setBoyWithPerson:(QYPerson *)person;

@end

7. 多用字面量語法,少用與之等價的方法

NSArray

  • 非字面量語法

    NSArray <NSString *> *array = [NSArray arrayWithObjects:@"1", @"2",  @"3", nil];
    
    • 取某個下標(biāo)的對象

      NSString *str2 = newArray[1];
      
  • 字面量語法

    NSArray <NSString *> *newArray = @[@"1", @"2", @"3"];
    
    • 取某個下標(biāo)的對象

       NSString *str1 = [array objectAtIndex:1];
      

需要注意的是

NSString *obj1 = @"1";
NSString *obj2 = nil;
NSString *obj3 = @"3";
//字面量語法 array1只含有obj1
NSArray <NSString *> *array1 = [NSArray arrayWithObjects:obj1, obj2, obj2, nil];
//非字面量語法 crash
NSArray <NSString *> *newArray1 = @[obj1, obj2, obj3];

8. 用枚舉表示狀態(tài)、選項、狀態(tài)碼等

typedef NS_ENUM(NSInteger, QYBoyStatus) {
    QYBoyIsRuning,
    QYBoyIsSleeping,
    QYBoyIsEating
};

9. 命名方式

  • 當(dāng)應(yīng)用程序自身和其所應(yīng)用的程序庫都引入了同名的第三方庫,則應(yīng)用前綴避免沖突

    #import "ABCLibrary"
    #import “QYLibrary"
    
  • 開頭小寫后面單詞首字母大寫

    //全局變量
    static const CGFloat QYScreenSize = [UIScreen mainScreen].bounds.size.width;
    //屬性命名 開頭小寫后面單詞首字母大寫
    @property (nullable, nonatomic, copy) NSString *boyName;
    //方法命名 開頭小寫后面單詞首字母大寫
    - (void)addFriendWithPerson:(QYPerson *)person
    
  • 從命名上可以知道方法的功能

    NSString *text = @"she is a boy”;
    NSString *newText = [text stringByReplacingOccurrencesOfString:@"boy" withString:@"girl"];//代碼讀起來像平時語言,易懂
    
    //錯誤的方法命名
    
  • (void)configCellWith:(QYBoyModel *)boyModel :(NSInteger)index;
    //正確的方法命名
  • (void)configCellWithBoyModel:(QYBoyModel *)boyModel andIndex:(NSInteger)index;
    
    
  • 為私有方法名加前綴

    - (void)p_setUpUI {
    }
    
  • 布爾值類型屬性命名

    //getter方法用isLogined,setter用logined
    @property(nonatomic, readonly, getter=isLogined) BOOL logined;
    

10. 將類的實(shí)現(xiàn)代碼分散到便于管理的數(shù)個分類之中

#import <Foundation/Foundation.h>

@class QYBoy;

@interface QYPerson : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSArray *friends;
@property (nonatomic, strong) QYBoy *boy;

- (void)addFriends:(QYPerson *)friend;
- (void)removeFriend:(QYPerson *)friend;
- (BOOL)isFriendWithPerson:(QYPerson *)person;

@end

@interface QYPerson (Work)

- (void)goToWork;
- (void)goOffWork;

@end

@interface QYPerson (Play)

- (void)playAgame;
- (void)PlayWithThePhone;

@end

11. 為常用的塊類型創(chuàng)建typedef

  • 不定義別名:

    - (void)workWithPresonBlock:(void (^) (NSString *name))personBlock;
    
  • 定義別名:

//兩個塊簽名相同,但最好還是定義多個typedef,重構(gòu)代碼或需要修改時對其他的typedef無影響。
typedef void(^PersonWorkBlock)(NSString *);
typedef void(^PersonPlayBlock)(NSString *);
- (void)workWithPresonWorkBlock:(PersonWorkBlock)personWorkBlock;
- (void)playWithPresonPlayBlock:(PersonPlayBlock)personPlayBlock;

12. 多用塊枚舉,少用for循環(huán)

  • 塊枚舉

    NSArray *arr1 = @[@"1",@"2",@"3"];
    [arr1 enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //do something
    }];
        
    NSDictionary *dic1 = @{@"1":@"one",@"2":@"two",@"3":@"three"};
    [dic1 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        //do something
    }];
    

13. 注釋

/**
 類注釋
 */
@interface QYPerson : NSObject
/* 屬性注釋 */
@property (nonatomic, copy) NSString *name;

@property (nonatomic, strong) QYBoy *boy; //屬性注釋

/**
 添加朋友(方法注釋)

 @param Friend 某一個朋友對象
 */
- (void)addFriends:(QYPerson *)friend;

@end


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容