美文网首页
由Faux Pas看iOS的编码规范

由Faux Pas看iOS的编码规范

作者: kyson老师 | 来源:发表于2019-03-21 11:12 被阅读0次

本系列博客是本人的开发笔记。为了方便讨论,本人新建了一个微信群(iOS技术讨论群),想要加入的,请添加本人微信:zhujinhui207407,【加我前请备注:iOS 】,本人博客http://www.kyson.cn 也在不停的更新中,欢迎一起讨论

iOS质量保障工具集中我们提到了Faux Pas这款强大的工具。由于篇幅原因没有做进一步的讲解,本文将详细描述Faux Pas给我们带来的编码规范的建议。

函数参数变量的值,不应该被直接修改。

错误:

- (void)_uploadImage:(UIImage *)image needCompress:(BOOL)compress isAvatar:(BOOL)isAvatar showLoading:(BOOL)showLoading
{
    if (compress) {
        // 压缩
        image = [image processImage];
    }
    // ......
}

正确:

- (void)_uploadImage:(UIImage *)image needCompress:(BOOL)compress isAvatar:(BOOL)isAvatar showLoading:(BOOL)showLoading
{
    UIImage *imgUpload = compress ? [image processImage] : image;
    // ......
}

不应该对本类的引用进行硬编码

这样可以在运行时,让指定的类接受到消息。这样可以避免未能执行子类的方法。
错误:

+ (DDAppConfigManger *)defaultManger
{
    static DDAppConfigManger *_manger = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_manger == nil) {
            _manger = [[DDAppConfigManger alloc] init];
        }
    });
    return _manger;
}

正确:

+ (instancetype)defaultManger
{
    static DDAppConfigManger *_manger = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_manger == nil) {
            _manger = [[self alloc] init];
        }
    });
    return _manger;
}

三目运算符

错误:

NSString *cityID = ([UserAccountManger defaultManger].staffInfo.city_id ? [UserAccountManger defaultManger].staffInfo.city_id : @"0");

正确:

NSString *cityID = ([UserAccountManger defaultManger].staffInfo.city_id ? : @"0");

避免在init或者dealloc方法中调用属性的setter方法

这样有可能会触发KVC的通知,破坏监听者数据的合法和一致性。
参考链接:
Don’t Use Accessor Methods in Initializer Methods and dealloc
错误:

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init])
    {
        if (dict.allKeys.count) {
            self.bank_name = stringFromObject(dict, @"bankName");
        }
    }
    return self;
}

正确:

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init])
    {
        if (dict.allKeys.count) {
            _bank_name = stringFromObject(dict, @"bankName");
        }
    }
    return self;
}

工程的Class Prefix设置应该填上相应的值

遵守语言习惯

因为系统会默认给非dynamic的属性生成setter和getter方法,所以属性的命名最好不要以get作为前缀。
错误:

/**
 get 短信验证码
 */
@property (strong, nonatomic, readonly, nonnull) RACCommand *getVerifyCodeCommand;

正确:

/**
 get 短信验证码
 */
@property (strong, nonatomic, readonly, nonnull) RACCommand *requestVerifyCodeCommand;

错误:

@interface NSObject (PropertyList)
 
/**
 *  获取所有属性
 *
 *  @return
 */
- (NSArray *)getAllProperties;
@end

正确:

@interface NSObject (PropertyList)
 
/**
 *  获取所有属性
 *
 *  @return
 */
- (NSArray *)ndd_allProperties;
@end

避免潜在的断言边际效应

错误:

NSAssert(orEmpty(self.actionAddress).length > 0, @"Invalid Request actionAddress stubRequestsPassingTest. Request actionAddress must not be nil or empty.");

正确:

NSAssert(orEmpty(_actionAddress).length > 0, @"Invalid Request actionAddress stubRequestsPassingTest. Request actionAddress must not be nil or empty.");

相关文章

网友评论

      本文标题:由Faux Pas看iOS的编码规范

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