iOS Developer

OC 代码规范

2017-05-29  本文已影响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.分类定义常量

#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

需要注意的是

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. 命名方式

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

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

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

13. 注释

/**
 类注释
 */
@interface QYPerson : NSObject
/* 属性注释 */
@property (nonatomic, copy) NSString *name;

@property (nonatomic, strong) QYBoy *boy; //属性注释

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

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

@end


上一篇 下一篇

猜你喜欢

热点阅读