我想創建一個更短,更優雅的版本的一些工作代碼。Objective C removeObjectsAtIndexes願望清潔代碼
僅供參考我必須添加一個元素到現有的數組,然後刪除一個,然後重複。 (不幸的是我不能添加多個元素,然後刪除多個元素)。
工作代碼:
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]];
有沒有辦法讓這個短版的工作? (給出預期的標識符錯誤)
感謝很多的幫助...
你的實現是你問什麼不同。你的代碼不會工作,它會崩潰,因爲你沒有在'originArray'中添加任何對象,並且你試圖從空數組中獲取對象。 –
@Bhumeshwerkatre閱讀代碼中的評論。 – rmaddy
爲什麼人們會對這個問題投票?問題很明顯,它顯示了相關的代碼,並且顯示了一些努力。人們還想要什麼? – rmaddy