views:

62

answers:

1

I am grappling with adding an NSData object to a NSMutable array. The code is executing fine but it is not adding the objects in the array.The code is as follows:

NSData * imageData = UIImageJPEGRepresentation(img, 1.0);

int i=0;

    do{

        if([[tempArray objectAtIndex:i] isEqual:imageData])
        {
            [tempArray removeObjectAtIndex:i];
        }
        else 
        {
            [tempArray addObject:imageData];
            //NSLog(@"ANURAG %@",[tempArray objectAtIndex:0]);
        }
    }while(i<[tempArray count]) ;

The NSLog statement shows the object added is null however the value of imageData is not null.

I have defined the tempArray as a static memeber of the class in which this code is written.

Is it because of the size of the data object as it is the data of an image?

+2  A: 

Do you initialize tempArray before this code section? You cannot add objects to an uninitialized array.

tempArray = [[NSMutableArray alloc] init];
Tom S
Thanks a lot Tom.But now the problem is the application is getting crashed on adding the imageData to an array.Is the size of data object must be limited???The exception isTerminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
anurag
Because you do `[[tempArray objectAtIndex:i] isEqual:imageData]` when the array is empty.
JeremyP
@JeremyPYes Jeremy u were right.Thanks
anurag