iOS中接口与API设计(一)

1,278 阅读3分钟

这是我参与8月更文挑战的第22天,活动详情查看:8月更文挑战

用前缀避免命名冲突

  • 以公司、应用程序或二者皆有关系的关联之名为前缀

  • 一般前缀应该是三个字母的(Apple宣传保留了俩个字母的前缀)

  • 分类前一定要加前缀

  • 编写三方库时,一定要加前缀,用到别的三方库时,应该在别的三方库前加上你的前缀,避免别人集成进去后和其他三方库冲突

    比如:你的三方库中用到了YYLibrary的库,你的工程库叫QQLibrary,修改之后应该是QQYYLibrary


清晰而协调的命名方式

  • 给方法命名注意事项

    • 如果方法返回值是新创建的,那么方法名的首个词应是返回值的类型,除非前面还有修饰语
    • 应该把表示参数类型的名词放在参数前面
    • 如果方法要在当前对象上执行操作,那么就应该包含动词;若执行操作时还需要参数,则应该在动词后面加上一个或多个名词
    • 不要使用str这种简称,应该用string这样的全称
    • BOOL值属性应该加is前缀,如果某方法返回非其属性的BOOL值,那么应该根据其功能处理,选用hasis当前缀
    • get这个前缀留给那些借由“输出参数”来保存返回值的方法
  • 类与协议的命名

    为类和协议的名称加上前缀

  • 私有方法加前缀

    • 给私有方法加前缀,是为了方便调试以及与公有api好区分
    • 不要单用一个下划线做私有方法的前缀,下划线被苹果公司预留了

尽量使用不可变对象

  • 使用属性时,可将其声明为“readonly”(默认:readwrite
  • 比如通过初始化方法传入的属性,那么单独声明的属性值就可以声明为readonly
#import <Foundation/Foundation.h>
@interface Phone : NSObject
@property (nonatomic,copy,readonly) NSString *name;
@property (nonatomic,assign,readonly) NSInteger price;

- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price;
@end

  • 如果想修改数据,且不想让外人知道,通常可以在对象内部分类中将其设置为readwrite
#import "Phone.h"
//分类中重新声明
@interface Phone()
@property (nonatomic,copy,readwrite) NSString *name;
@property (nonatomic,assign,readwrite) NSInteger price;
@end

@implementation Phone
- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price{
    self = [super init];
    if (self) {
        
        _name = [name copy];
        _price = price;
    }
    return self;
}
@end
  • 尽量不要把可变的arraysetdictionary等作为公开属性,应该提供相应的方法,以此修改对象中的可变属性 为什么不能直接把phoneArray定义成可变的,通过phoneArray来控制? 比如在添加或者删除时,Phone对象可能要执行其他相关操作,如果直接从底层修改了内部用于存储的phoneArray,在Phone对象不知情时,直接修改phoneArray可能会让对象间各数据不一致
#import <Foundation/Foundation.h>
@interface Phone : NSObject
@property (nonatomic,copy,readonly) NSString *name;
@property (nonatomic,assign,readonly) NSInteger price;
@property (nonatomic,strong,readonly) NSArray *phoneArray;

- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price;
- (void)addPhone:(Phone *)phone;
- (void)removePhone:(Phone *)phone;
@end

#import "Phone.h"
@interface Phone()
@property (nonatomic,copy,readwrite) NSString *name;
@property (nonatomic,assign,readwrite) NSInteger price;
@property (nonatomic,strong,readwrite) NSMutableArray *savePhoneArray;
@end

@implementation Phone

- (instancetype)initWithName:(NSString *)name withPrice:(NSInteger)price{
    self = [super init];
    if (self) {
        _name = [name copy];
        _price = price;
        _savePhoneArray = [[NSMutableArray alloc]init];
    }
    return self;
}
- (instancetype)init{
    return [self initWithName:@"iPhone" withPrice:999];
}
-(NSMutableArray *)savePhoneArray{
    if (_savePhoneArray == nil) {
        _savePhoneArray = [NSMutableArray new];
    }
    return _savePhoneArray;
}

- (void)addPhone:(Phone *)phone{
    [self.savePhoneArray addObject:phone];
}
- (NSArray *)phoneArray{
    return [self.savePhoneArray copy];
}
- (void)removePhone:(Phone *)phone{
    [self.savePhoneArray removeObject:phone];
}
@end

关于iOS接口与API设计相关知识点