OC 代码规范

作者: 呆萌的中指 | 来源:发表于2017-05-29 23:37 被阅读0次

代码规范

好久没更新了,确实忙,前段时间正好听前辈提起这方面的事情,>醍醐灌顶,哈哈,不废话了,把之前为了重构外包的代码,为项目>组写的规范贴出来给大家分享
提醒:
1.因为是规范,所以没有为什么,可以自己Google下
2.一些自己的习惯请忽略。。。。。。。。。

目的:写出执行迅速、便于理解、便于维护、便于扩展、不易出错的代码。


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

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

2. __kindof

- (void)viewDidLoad {
    [super viewDidLoad];
     UIButton *but1 = [self getSubView];//不需要强转
}

- (__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之间,定义的所有对象属性和方法
默认都是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预处理指令

define

#define kAnimationDuration @"男"

类型常量

//只在本实现文件中使用,前面字母加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];
    
    • 取某个下标的对象

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

    NSArray <NSString *> *newArray = @[@"1", @"2", @"3"];
    
    • 取某个下标的对象

       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. 用枚举表示状态、选项、状态码等

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

9. 命名方式

  • 当应用程序自身和其所应用的程序库都引入了同名的第三方库,则应用前缀避免冲突

    #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. 将类的实现代码分散到便于管理的数个分类之中

#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. 为常用的块类型创建typedef

  • 不定义别名:

    - (void)workWithPresonBlock:(void (^) (NSString *name))personBlock;
    
  • 定义别名:

//两个块签名相同,但最好还是定义多个typedef,重构代码或需要修改时对其他的typedef无影响。
typedef void(^PersonWorkBlock)(NSString *);
typedef void(^PersonPlayBlock)(NSString *);
- (void)workWithPresonWorkBlock:(PersonWorkBlock)personWorkBlock;
- (void)playWithPresonPlayBlock:(PersonPlayBlock)personPlayBlock;

12. 多用块枚举,少用for循环

  • 块枚举

    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


相关文章

  • Raywenderlich 的 OC 代码规范

    Raywenderlich 的 OC 代码规范

  • iOS知识整理(二)

    OC知识整理 代码规范 iOS 代码规范 #pragma mark - 添加子视图#pragma mark - 添...

  • OC代码规范

    资源网上现成有,也没有必要完全重新定义,看了几份后,觉得这份挺符合的,就决定先这么采用,具体可以在实际开发中再进行...

  • OC代码规范

    命名 Preferred : Not Preferred : 代码组织 使用#pragma mark - 将生命...

  • OC代码规范

    Objective-C代码规范1、变量名、方法名写注释;2、在视图控制器里添加控件时,初始化、布局、赋值分到不同的...

  • oc代码规范

    1.属性命名 // 属性命名:小驼峰+类型后缀 变量尽量以描述性的方式来命名。单个字符的变量命名应该尽量避免,除了...

  • OC代码规范

    OC代码规范 一、代码格式 1.1、使用空格而不是制表符Tab 不要在工程里使用 Tab 键,使用空格来进行缩进。...

  • OC 代码规范

    代码规范 好久没更新了,确实忙,前段时间正好听前辈提起这方面的事情,>醍醐灌顶,哈哈,不废话了,把之前为了重构外包...

  • OC代码规范

    Version: 0.01 本文部份章节摘自《苹果 Cocoa 编码规范》(即Apple’s Cocoa Codi...

  • OC代码规范的spacecommander使用 - 推酷

    OC代码规范的spacecommander使用 - 推酷

网友评论

    本文标题:OC 代码规范

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