我正在尝试重构两种方法,并遇到了一些问题。方法非常相似,如下所示:
- (void)methodOne:(NSString *)id idValue:(NSString *)idValue foo:(NSString *)bar parcel:(NSString)parcel anotherId:(NSString *) anotherIdValue:(NSString *)anotherIdValue{
NSDictionary *params = @{id: idValue,
bar: parcel,
anotherId: anotherIdVaule};
[self requestWithParams:params
method:POST];
}
- (void)methodTwo:(NSString *)id idValue:(NSString *)idValue foo:(NSString *)bar parcel:(NSString)parcel {
NSDictionary *params = @{id: idValue,
bar: parcel};
[self requestWithParams:params
method:POST];
}
我该怎么做?
答案 0 :(得分:1)
一种方法是从第二种方法调用第一种方法,就像从其他初始值设定项中调用指定的初始值设定项一样:
- (void)methodOne:(NSString *)id idValue:(NSString *)idValue foo:(NSString *)bar parcel:(NSString)parcel anotherId:(NSString *) anotherIdValue:(NSString *)anotherIdValue{
NSMutableDictionary *params = [@{id: idValue, bar: parcel} mutableCopy];
if (anotherId != nil && anotherIdVaule != nil) {
[params setObject:anotherIdVaule forKey:anotherId];
}
[self requestWithParams:params
method:POST];
}
- (void)methodTwo:(NSString *)id idValue:(NSString *)idValue foo:(NSString *)bar parcel:(NSString)parcel {
[self methodOne:id idValue:idValue foo:bar parcel:parcel anotherId:nil anotherIdValue:nil];
}