For understanding the concept of accessing the array object, here i came up with the example which explains clearly why its crashing when you access the object at index in array.
I'm not going to pointout where you did mistakes. understand the concept and based on that change the code whereever you have like [yourArray objectAtIndex:10]
.
Non Software example:
You have bag(NSMutableArray) which has 10 balls labeled from (0,1,2...9), but how would you find out the 11th ball. Not Possible!.
1.First you should have bag in your hand readily.([[NSMutableArray alloc] init]
)
2.Then try to access it.
3.Your problem here is you are looking for first ball(0th labeled) 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
NSMutableArray *array = [[NSMutableArray alloc]init]");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…