xcode 如何在objective c中使用NSArray对象的NSArray?

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

How to use NSArray of NSArray object in objective c?

objective-ciosxcodecocoa

提问by user1306926

I declared a NSArrayobject in .h file as

NSArray在 .h 文件中声明了一个对象

@property (nonatomic, assign) NSArray  *scnArray;

and in .h file under - (void)viewDidLoadI created three different NSArrayobjects as

在 .h 文件中,- (void)viewDidLoad我创建了三个不同的NSArray对象作为

NSArray  *obj1 = [[NSArray alloc] initWithObjects:@"1",@"0",@"0",nil];
NSArray  *obj2 = [[NSArray alloc] initWithObjects:@"0",@"3",@"0",nil];
NSArray  *obj3 = [[NSArray alloc] initWithObjects:@"0",@"0",@"5",nil];

scnArray = [[NSArray alloc] initWithArray:obj1];
[scnArray arrayByAddingObjectsFromArray:obj2];
[scnArray arrayByAddingObjectsFromArray:obj3];

and if I access this scnArray from any other function

如果我从任何其他函数访问这个 scnArray

NSArray *caseArray = [scnArray objectAtIndex:index];//index will be 0, 1, 2...

NSArray *caseArray = [scnArray objectAtIndex:index];//索引将为 0, 1, 2 ...

I am getting BAD_ACCESS_ERROR. What is the problem here and how can I correct to use it?

我得到BAD_ACCESS_ERROR. 这里有什么问题,我该如何纠正使用它?

采纳答案by Dr.Kameleon

Try this :

尝试这个 :

NSArray  *obj1 = [[NSArray alloc] initWithObjects:@"1",@"0",@"0",nil];
NSArray  *obj2 = [[NSArray alloc] initWithObjects:@"0",@"3",@"0",nil];
NSArray  *obj3 = [[NSArray alloc] initWithObjects:@"0",@"0",@"5",nil];

scnArray = [[NSArray alloc] initWithArray:obj1];
scnArray = [scnArray arrayByAddingObjectsFromArray:obj2];
scnArray = [scnArray arrayByAddingObjectsFromArray:obj3];

The arrayByAddingObjectsFromArray:function adds all objects from array B to array A, and return the result (=the array containing both A's and B's elements).

arrayByAddingObjectsFromArray:函数将数组 B 中的所有对象添加到数组 A,并返回结果(=包含 A 和 B 元素的数组)。

So, you should simply GET that result... :-)

所以,你应该简单地得到那个结果...... :-)

IMPORTANT :scnArrayMUST be an NSMutableArrayarray, and NOT an NSArray(it's changing, right?); so make sure you edit that part too...

重要提示:scnArray必须是一个NSMutableArray数组,而不是一个NSArray(它正在改变,对吧?);所以请确保您也编辑该部分...

ALSO :NSArray *caseArray = [scnArray objectAtIndex:index];- this doesn't make any sense. Setting an array to an ELEMENT of the scnArray? It doesn't contain arrays, right? It just contains ELEMENTS of those arrays (the ones we've added)...

还:NSArray *caseArray = [scnArray objectAtIndex:index];- 这没有任何意义。将数组设置为scnArray? 它不包含数组,对吗?它只包含这些数组的元素(我们添加的那些)......

回答by JeremyP

arrayByAddingObject:does not add the new object to the receiver, it creates a completely new array and returns it. You should use something like:

arrayByAddingObject:不会将新对象添加到接收器,它会创建一个全新的数组并返回它。你应该使用类似的东西:

scnArray = [[NSArray alloc] initWithObjects: obj1, obj2, obj3, nil];

Don't forget that if you are not using ARC or GC, all of these arrays will need to be released at some point.

不要忘记,如果您不使用 ARC 或 GC,所有这些数组都需要在某个时候释放。