ios 如何在 NSArray 的第一个索引处添加对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13854576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add object at first index of NSArray
提问by Joker
I want to add @"ALL ITEMS" object at the first index of NSARRAY.
我想在 NSARRAY 的第一个索引处添加 @"ALL ITEMS" 对象。
Initially the Array has 10 objects. After adding, the array should contains 11 objects.
最初 Array 有 10 个对象。添加后,该数组应包含 11 个对象。
采纳答案by Shad
First of all, NSArray need to be populated when it is initializing. So if you want to add some object at an array then you have to use NSMutableArray. Hope the following code will give you some idea and solution.
首先,NSArray在初始化的时候需要被填充。所以如果你想在数组中添加一些对象,那么你必须使用 NSMutableArray。希望下面的代码能给你一些想法和解决方案。
NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0", nil];
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
[mutableArray addObject:@"ALL ITEMS"];
[mutableArray addObjectsFromArray:array];
The addObject method will insert the object as the last element of the NSMutableArray.
addObject 方法会将对象作为 NSMutableArray 的最后一个元素插入。
回答by arthankamal
you can't modify NSArray
for inserting and adding. you need to use NSMutableArray
. If you want to insert object at specified index
你不能修改NSArray
插入和添加。你需要使用NSMutableArray
. 如果要在指定索引处插入对象
[array1 insertObject:@"ALL ITEMS" atIndex:0];
[array1 insertObject:@"ALL ITEMS" atIndex:0];
In Swift 2.0
在 Swift 2.0
array1.insertObject("ALL ITEMS", atIndex: 0)
回答by Chris Conover
I know that we have six answers for insertObject, and one for creating a(n) NSMutableArray array and then calling addObject, but there is also this:
我知道我们有六个关于 insertObject 的答案,一个用于创建 (n) NSMutableArray 数组然后调用 addObject,但也有这个:
myArray = [@[@"ALL ITEMS"] arrayByAddingObjectsFromArray:myArray];
I haven't profiled either though.
不过我也没有介绍。
回答by Sumit Mundra
Take a look at the insertObject:atIndex: method of the NSMutableArray class.To add an object to the front of the array, use 0 as the index:
看一下NSMutableArray类的insertObject:atIndex:方法。在数组前面添加一个对象,使用0作为索引:
[myMutableArray insertObject:myObject atIndex:0];
回答by Rajneesh071
NSArrayis immutable array you can't modify it in run time. Use NSMutableArray
NSArray是不可变数组,您不能在运行时修改它。使用NSMutableArray
[array insertObject:@"YourObject" atIndex:0];
回答by Prateek Bhanot
NSArray is immutable but you can use insertObject: method of NSMutableArray class
NSArray 是不可变的,但你可以使用 insertObject: NSMutableArray 类的方法
[array insertObject:@"all items" atIndex:0];
回答by Vineet Singh
As you are allready having 10 objects in your array,and you need to add another item at index 11...so,you must try this.... hope this helps..
由于您的数组中已经有 10 个对象,并且您需要在索引 11 处添加另一个项目......所以,你必须尝试这个......希望这会有所帮助......
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithCapacity:11];
[yourArray insertObject:@"All Items" atIndex:0];
回答by Shashank Kulshrestha
NSArray is not dyanamic to solve your purpose you have to use NSMutableArray. Refer the following method
NSArray 不能动态解决您必须使用 NSMutableArray 的目的。参考以下方法
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
回答by user3491090
Apple documents says NSMutableArrayMethods
苹果文件说NSMutableArray方法
[temp insertObject:@"all" atIndex:0];
回答by Deepak Tagadiya
Swift 3:
斯威夫特 3:
func addObject(){
var arrayName:[String] = ["Name1", "Name2", "Name3"]
arrayName.insert("Name0", at: 0)
print("---> ",arrayName)
}
Output:
---> ["Name0","Name1", "Name2", "Name3"]