gpt4 book ai didi

ios - 简单 NSArray - 将对象置于 NSArray 前面

转载 作者:行者123 更新时间:2023-12-01 17:22:31 26 4
gpt4 key购买 nike

NSarray 的简单问题。我将对象存储在 NSMuteableArray 中。[obj1, obj2, obj3]

如果选择了一个对象,我想把它放在数组的前面。 IE。如果选择了 obj3,则:

[obj3, obj1, obj2]

下面的方法是否有效或复制了 obj3?另外,这可以使线程安全吗?

[myMutableArray insertObject:obj3 atIndex:0];

最佳答案

假设您需要保留其他元素的顺序,您需要删除然后插入有问题的对象:

NSMutableArray *array = ... // array with objects
NSInteger index = ... // index of object to move to the front
id obj = array[index];
[array removeObjectAtIndex:index];
[array insertObject:obj atIndex:0];

关于ios - 简单 NSArray - 将对象置于 NSArray 前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27872197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com