9

I declared a NSArray object in .h file as

@property (nonatomic, assign) NSArray  *scnArray;

and in .h file under - (void)viewDidLoad I created three different NSArray objects as

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

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

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

3
  • You're adding the Objects from obj2 and obj3 to scnArray but you ARE not getting (or storing anywhere) the array resulting from the merge of the arrays... Have a look at my answer... Commented Apr 2, 2012 at 10:02
  • Your property should also be strong or retain, not assign. Commented Apr 2, 2012 at 11:26
  • Thanks Mike, changing the property as strong works great. Thanks all for your inputs. Commented Apr 2, 2012 at 15:57

2 Answers 2

7

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).

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

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

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)...

Sign up to request clarification or add additional context in comments.

Comments

2

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

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.

1 Comment

I used this earlier. But I access this as NSArray *caseArray = [scnArray objectAtIndex:1]; I am keep getting EXE_BAD_ACCESS error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.