xcode check if there is aobjectAtIndex or check array length
I c开发者_Python百科ant find this any where, I may be searching the wrong terms or words but I just need to know how to check if an array is a certain length:
if ([scores objectAtIndex:3]){
//code
}
This comes up with an error and crashes if the array isnt this long yet, but surly this should just check if there is an index, and if not move on??
How to I check this without the app crashing??
count
method of NSArray returns the number of objects in the array. If [myArray count]
returns n
then valid indexes are 0
to n - 1
. There is no automatic move on if the index is not valid. Before trying to access an index you need to make sure that the index is valid.
if ([scores count] >= 4) { id obj = [scores objectAtIndex:3]; }
精彩评论