我想为一些工作代码创建一个更短更优雅的版本。
仅供参考我必须在现有数组中添加元素,然后删除一个,然后重复。 (遗憾的是,我不能添加多个元素,然后删除多个元素)。
工作代码:
NSMutableArray *destinationArray;
destinationArray = [[NSMutableArray alloc] init];
NSMutableArray *originArray = [[NSMutableArray alloc]init];
// Copy one specific array element from originArray to destinationArray
// Note: assume originArray and destinationArray populated at this point
int originIndex = var1-var2+1;
[destinationArray addObject:originArray[originIndex]];
// Remove one specific array element from destinationArray
[destinationIndex removeAllIndexes]; // clear index if used previously
[destinationIndex addIndex:var3-var4-1];
[destinationArray removeObjectsAtIndexes:destinationIndex];
我更喜欢以下版本的某些版本:
[destinationArray addObject:originArray[var1-var2+1]];
[destinationArray removeObjectsAtIndexes:[var3-var4-1]];
有没有办法让这个较短的版本有效? (给出预期的标识符错误)
非常感谢你的帮助......
答案 0 :(得分:1)
为什么不这样做:
NSMutableArray *destinationArray = [[NSMutableArray alloc] init];
NSMutableArray *originArray = [[NSMutableArray alloc]init];
// somewhere in here objects are added to "originArray"
[destinationArray addObject:originArray[var1-var2+1]];
[destinationArray removeObjectAtIndex:var3-var4-1];