菜鸟教程小白 发表于 2022-12-12 20:27:18

objective-c - 如何恢复 NSMutableArray 的原始排序顺序?


                                            <p><p>我已将 NSString 标记为 NSMutable 数组。然后,我可以使用 NSSortDescriptor 按字符串的长度对该数组进行排序。然后我可以从最长的单词开始对某些单词执行方法。 </p>

<p>这就是我的问题所在。我现在需要恢复数组的原始排序顺序,以便可以将数组运行回编辑过的字符串。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我的回答基本上和史蒂夫的一样,只是我会用另一种方式来做——保留原始数组,创建一个排序后的副本,逐步完成排序后的副本进行处理,然后将其丢弃。所以,例如</p>

<pre><code>NSArray *yourOriginalArray = ...whatever...;

// do processing
for(id object in )
{
    // if you&#39;re doing processing that doesn&#39;t change the identity of
    // the object then there&#39;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 = ;

    id replacementObject = ;
    [yourOriginalArray replaceObjectAtIndex:originalIndex
                     withObject:replacementObject];
    // of course, this is only if yourOriginalArray is mutable
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 如何恢复 NSMutableArray 的原始排序顺序?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10255285/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10255285/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 如何恢复 NSMutableArray 的原始排序顺序?