简体   繁体   English

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

[英]How to add objects to an NSArray using for loop?

I want to add the [NSDecimalNumber numberWithInt:i] to an array using for loop.我想使用 for 循环将[NSDecimalNumber numberWithInt:i]到数组中。

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请帮我解决这个问题,提前致谢,马丹

NSArray is immutable . NSArray不可变的 Use the mutable version, NSMutableArray .使用可变版本NSMutableArray

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

...

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]];
}
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. NSMutableArray 类声明了管理可修改对象数组的对象的编程接口。 This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray这个类在继承自 NSArray 的基本数组处理行为中添加了插入和删除操作

NSMutableArray Class Reference NSMutableArray 类参考

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

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

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]; 

NSArray add object like this: NSArray像这样添加对象:

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

I think NSArray can not addObject like NSMutableArray .我认为NSArray不能像NSMutableArray那样addObject You should try it:你应该试试看:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM