ios 如何使用for循环将对象添加到NSArray?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7846201/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:58:56  来源:igfitidea点击:

How to add objects to an NSArray using for loop?

iphoneobjective-ciosnsarray

提问by Madan Mohan

I want to add the [NSDecimalNumber numberWithInt:i]to an array using for loop.

我想[NSDecimalNumber numberWithInt:i]使用 for 循环将其添加到数组中。

It is hardcoded :

它是硬编码的:

 NSArray *customTickLocations = [NSArray arrayWithObjects: [NSDecimalNumber numberWithInt:1],[NSDecimalNumber numberWithInt:2],[NSDecimalNumber numberWithInt:3],[NSDecimalNumber numberWithInt:4],[NSDecimalNumber numberWithInt:5],[NSDecimalNumber numberWithInt:6],[NSDecimalNumber numberWithInt:7],[NSDecimalNumber numberWithInt:8],[NSDecimalNumber numberWithInt:9],[NSDecimalNumber numberWithInt:10],[NSDecimalNumber numberWithInt:11],[NSDecimalNumber numberWithInt:12],nil];

I want like this, but I can add only one object here....

我想要这样,但我只能在这里添加一个对象....

for (int i=0; i<totalImagesOnXaxis; i++)
{
    customTickLocations = [NSArray arrayWithObject:[NSDecimalNumber numberWithInt:i]];
}

Please help me out of this, Thanks in Advance, Madan

请帮我解决这个问题,提前致谢,马丹

回答by EmptyStack

NSArrayis immutable. Use the mutable version, NSMutableArray.

NSArray不可变的。使用可变版本NSMutableArray

回答by justin

NSMutableArray * customTickLocations = [NSMutableArray new];
for (int idx = 0; idx < 12; ++idx) {
    [customTickLocations addObject:[NSDecimalNumber numberWithInt:idx]];
}

...

回答by Warewolf

you cannot add Objects at run time to NSArray.For adding or removing objects at Run time you have to use NSMutableArray.

您不能在运行时将对象添加到 NSArray。要在运行时添加或删除对象,您必须使用 NSMutableArray。

NSMutableArray *mutableArray=[[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
    [mutableArray addObject:[NSDecimalNumber numberWithInt:i]];
}

回答by beryllium

NSMutableArray *customTickLocations = [NSMutableArray array];
for (int i=0; i<totalImagesOnXaxis; i++)
{
    [customTickLocations addObject:[NSDecimalNumber numberWithInt:i]];
}

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray

NSMutableArray 类声明了管理可修改对象数组的对象的编程接口。这个类在继承自 NSArray 的基本数组处理行为中添加了插入和删除操作

NSMutableArray Class Reference

NSMutableArray 类参考

回答by Radu

NSMutableArray *customTickLocations = [[NSMutableArray alloc] init];

for(int i = 0; i<WhateverNoYouWant;i++)
{
  NSDecimalNumber * x = [NSDecimalNumber numberWithInt:i]
  [customTickLocations addObject:x]
}

回答by simon_smiley

I found that using this technique is a great way to easily add a few more elements to an NSArray, this was the answer I was looking for when I came to this thread so am posting it as it is a great easy addition.

我发现使用这种技术是一种将更多元素轻松添加到 NSArray 的好方法,这是我来到此线程时正在寻找的答案,因此我将其发布,因为它是一个非常简单的添加。

If I want to add a new array to my current array

如果我想在当前数组中添加一个新数组

currentArray = [currentArray arrayByAddingObjectsFromArray: newArray]; 

回答by AmyNguyen

NSArrayadd object like this:

NSArray像这样添加对象:

NSArray *arr = @["1","2","3","4"];

I think NSArraycan not addObjectlike NSMutableArray. You should try it:

我想NSArray不能addObject喜欢NSMutableArray。你应该试试看:

NSMutableArray *mulArr = [NSMutableArray new];
[mulArr addObject:[NSDecimalNumber numberWithInt:number]];