shaffooo
shaffooo

Reputation: 1646

Error in using NSMutableArray

I am trying to populate an NSMutableArray here named "data" with NSArrays. When I try to retrieve I get EXC_BAD_ACCESS. Here is my code for poulating

.h

@property (nonatomic, retain) NSMutableArray* data;

.m

@synthesize data;

in viewDidLoad

self.data = [NSMutableArray array];
NSArray* ar1 = [[NSArray arrayWithObjects: @"text1", @"text2", @"text3", @"text4", nil] autorelease];
[self.data addObject:ar1];

Now in other method I am trying to get the inner NSArray back:

NSArray* sItem = [NSArray array]; 
sItem = (NSArray*)[self.data objectAtIndex:0];

if (sItem)
{
    if([sItem isKindOfClass:[NSArray class]])/////ERROR LINE
    {
        NSLog(@"Its an Array.");
    }
}

Upvotes: 0

Views: 115

Answers (1)

nielsbot
nielsbot

Reputation: 16031

In -viewDidLoad, you are over-releasing ar1. It is returned already autoreleased. You know this because you obtained the array via arrayWithObjects:, not by alloc/init.

The pattern is:

NSArray * temporaryArray = [ [ [ NSArray alloc ] initWithObjects:obj_0, ..., obj_n, nil ] autorelease ]

or

NSArray * temporaryArray = [ NSArray arrayWithObjects:obj0, ..., obj_n, nil ] ;

You should switch your project to ARC--it will handle this for you.

EDIT:

You can also rewrite

NSArray* sItem = [NSArray array]; 
sItem = (NSArray*)[self.data objectAtIndex:0];
if (sItem) {
    if([sItem isKindOfClass:[NSArray class]]){ /////ERROR LINE
         NSLog(@"Its an Array.");
    }

as

NSArray* sItem = nil ;
sItem = [self.data objectAtIndex:0];
if( [ sItem isKindOfClass:[ NSArray class ] ] )
{
    NSLog(@"Its an Array.");
}

or even

NSArray* sItem = [self.data objectAtIndex:0];
if( [ sItem isKindOfClass:[ NSArray class ] ] )
{
    NSLog(@"Its an Array.");
}

Notes about your code:

  1. You are allocating a new NSArray and assigning it to the pointer sItem. This array is immediately discarded when you assign another value to sItem. Just initialize sItem to nil instead.
  2. You don't need to check if (sItem)... Sending -isKindOfClass: (or any message in fact) to nil will always return NO/nil/0.

Finally, I often check for the presence of an array by doing something like:

if ( sItem.length > 0 ) { /* yes, valid, non-empty array */ }

This shortcut is especially good for strings.

Upvotes: 3

Related Questions