如何优雅地解决 Objective-C 不支持方法默认参数的问题
Posted iOS开发
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何优雅地解决 Objective-C 不支持方法默认参数的问题相关的知识,希望对你有一定的参考价值。
https://juejin.im/post/5d4ff10f51882515084f55a3
背景
@interface TTDoSomething:NSObject
+ ( void)doSomethingWithName:( NSString *)name configA:(ConfigA)configA configB:(ConfigB)configB configC:(ConfigC)configC;
@end
@interface TTDoSomething:NSObject
+ ( void)doSomethingWithName:( NSString *)name configA:(ConfigA)configA;
+ ( void)doSomethingWithName:( NSString *)name configB:(ConfigB)configB;
+ ( void)doSomethingWithName:( NSString *)name configC:(ConfigC)configC;
+ ( void)doSomethingWithName:( NSString *)name configA:(ConfigA)configA configB:(ConfigB)configB configC:(ConfigC)configC;
@end
@implementation TTDoSomething
+ ( void)doSomethingWithName:( NSString *)name configA:(ConfigA)configA {
[ self doSomethingWithName:name configA:configA configB: nil configC: nil];
}
+ ( void)doSomethingWithName:( NSString *)name configB:(ConfigB)configB {
[ self doSomethingWithName:name configA: nil configB:configB configC: nil];
}
...
@end
-
组合不灵活,如果需要增加 2 个种 config 组合的便利方法,需要增加新方法; -
扩展不便,如果全能方法增加了新的配置 configD 需要在现有便利方法的基础上增加更多的便利方法;
链式表达式
.h
@interface TTDoSomethingMaker:NSObject //这里命名也参考 Masonry, 定义为 Maker
// 声明需要的 block 属性,包括 Name 和 Config
@property ( nonatomic, copy, readonly) TTDoSomethingMaker *(^name)( NSString *name);
@property ( nonatomic, copy, readonly) TTDoSomethingMaker *(^configA)(ConfigA configA);
@property ( nonatomic, copy, readonly) TTDoSomethingMaker *(^configB)(ConfigB configB);
@property ( nonatomic, copy, readonly) TTDoSomethingMaker *(^configC)(ConfigC configC);
@end
.m
@interface TTDoSomethingMaker ()
// 在.m 中声明保存 name 和 config 的属性,这里不需要暴露给外部,所以不需要声明在 .h
@property( nonatomic, copy) NSString *nameValue;
@property( nonatomic, assign) ConfigA configAValue;
@property( nonatomic, assign) ConfigB configBValue;
@property( nonatomic, assign) ConfigC configCValue;
@end
@implementation TTDoSomethingMaker
// 实现所有的 block getter
- ( NSString *)name {
return ^TTDoSomethingMaker *( NSString *name) {
self.nameValue = name;
return self;
};
}
- (ConfigA)configA {
return ^TTDoSomethingMaker *(ConfigA configA) {
self.configAValue = configA;
return self;
};
}
- (ConfigB)configB {
return ^TTDoSomethingMaker *(ConfigB configB) {
self.configBValue = configB;
return self;
};
}
- (ConfigC)configC {
return ^TTDoSomethingMaker *(ConfigC configC) {
self.configCValue = configC;
return self;
};
}
@end
+ ( void)doSomething:( void(^)(TTDoSomethingMaker *maker))block {
TTDoSomethingMaker *maker = TTDoSomethingMaker. new;
block ?: block(maker);
// todo
[self doSomethingWithName:maker.nameValue configA:maker.configAValue configB:configBValue configC:configCValue]
}
[ TTDoSomething doSomething:^(TTDoSomethingMaker *maker) {
maker.name(@"Demo").configA(ConfigAOne);
}];
[ TTDoSomething doSomethingWithName:@"Demo" configA:ConfigAOne configB:0 configC:0];
小优化一发
+ ( void)doSomething:( void(^)(TTDoSomethingMaker *maker))block {
TTDoSomethingMaker *maker = TTDoSomethingMaker.new;
!block ?: block(maker);
NSAssert(maker.nameValue, @"Parameter 'name' should not be nil.");
// 将 ConfigB 配置为 ConfigBTwo
if (maker.configBValue == 0) {
maker.configBValue = ConfigBTwo;
}
[ self doSomethingWithName:maker.nameValue configA:maker.configAValue configB:configBValue configC:configCValue]
}
#define TTDoSomethingMakerProperty(TYPE, NAME) - (TTDoSomethingMaker *(^)(TYPE))NAME {\
return ^TTDoSomethingMaker *(TYPE NAME) {\
self.NAME ##Value = NAME;\
return self;\
};\
}\
@implementation TTDoSomethingMaker
TTDoSomethingMakerProperty( NSString *, name)
TTDoSomethingMakerProperty(ConfigA, configA)
TTDoSomethingMakerProperty(ConfigB, configB)
TTDoSomethingMakerProperty(ConfigC, configC)
@end
总结
推荐↓↓↓
长
按
关
注
以上是关于如何优雅地解决 Objective-C 不支持方法默认参数的问题的主要内容,如果未能解决你的问题,请参考以下文章