我已将 NSString 标记为 NSMutable 数组。然后,我可以使用 NSSortDescriptor 按字符串的长度对该数组进行排序。然后我可以从最长的单词开始对某些单词执行方法。
这就是我的问题所在。我现在需要恢复数组的原始排序顺序,以便可以将数组运行回编辑过的字符串。
Best Answer-推荐答案 strong>
我的回答基本上和史蒂夫的一样,只是我会用另一种方式来做——保留原始数组,创建一个排序后的副本,逐步完成排序后的副本进行处理,然后将其丢弃。所以,例如
NSArray *yourOriginalArray = ...whatever...;
// do processing
for(id object in [yourOriginalArray sortedArrayUsing<whatever means>])
{
// if you're doing processing that doesn't change the identity of
// the object then there's no need to worry about this, but supposing
// you want to adjust the identity (eg, the source array is full of
// immutable objects and you want to replace them) then...
NSUInteger originalIndex = [yourOriginalArray indexOfObjectbject];
id replacementObject = [self mutatedFormOfbject];
[yourOriginalArray replaceObjectAtIndexriginalIndex
withObject:replacementObject];
// of course, this is only if yourOriginalArray is mutable
}
关于objective-c - 如何恢复 NSMutableArray 的原始排序顺序?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/10255285/
|