1
基本上我有2個功能相似的方法。唯一的區別是不同的類容器。我試圖實現的是統一這兩種方法,並以某種方式使容器變得動態。2重複方法的代碼重構
這裏有2種方法:
-(NSMutableArray*) parseRequest:(NSArray*)elements {
NSMutableArray *currentStruct = [NSMutableArray array];
for (id element elemets) {
// This is where the difference is
FriendRequest *friend = [[FriendRequest alloc] init];
if(nickname != nil) {
friend.nickname = [element objectAtIndex:0];
}
[currentStruct addObject:friend];
[friend release];
}
return currentStruct;
}
二:
-(NSMutableArray*) parseRequest:(NSArray*)elements {
NSMutableArray *currentStruct = [NSMutableArray array];
for (id element elemets) {
// This is where the difference is
Friend *friend = [[Friend alloc] init];
if(nickname != nil) {
friend.nickname = [element objectAtIndex:0];
}
[currentStruct addObject:friend];
[friend release];
}
return currentStruct;
}