To understand the concept of accessing an array object, here I gave an example that explains why it crashes when accessing an object by index in the array.
I am not going to indicate where you made mistakes. understand the concept and based on this change the code that you like [yourArray objectAtIndex:10] .
Nonprofit example:
You have a bag (NSMutableArray) on which there are 10 balls marked (0,1,2 ... 9), but how do you recognize the 11th ball. Impossible !.
1. First you must have a bag in your hand. ( [[NSMutableArray alloc] init] )
2. Then try to access it.
3. Your problem is here, you are looking for the first ball (0th marked) inside the bag. iOS says sorry
//assume [array count] == 10 (0,1,2,3,4,5,...9) NSUInteger indexTobeAccessedInArray = 5;//6th object (0,1,2,3,4,5,...9) if (array && [array count]>0) { // array lives in memory and it has atleast one objects in it // 5 < 10 if (indexTobeAccessedInArray < [array count]) { //Yes NSLog(@"you can access this index:%i",indexTobeAccessedInArray); } else{ //No NSLog(@"Index %i beyond bounds for array",indexTobeAccessedInArray); } } else{ // There was no array in memory NSLog(@"sorry there is no array, you should create & initialize it first like \n NSMutableArray *array = [[NSMutableArray alloc]init]"); }
Vijay-Apple-Dev.blogspot.com
source share