About objectAtIndex's return value
NSLog(@" --- object id = %ld --- ",
(long) [mp_list objectAtIndex : 0]);
target_coordinate_2D = [[mp_list objectAtIndex : 0] coordinate];
// Some test code here which verifies that target_coordinate_2D
// gets assigned a valid "coordinate" value.
[mapView addAnnotation : [mp_list objectAtIndex : 0]];
Consider the above 3 statements :
According to documentation, the NSArray's method "objectAtIndex" should return an object. My understanding is the value returned is a numeric pointer to the object.
But what I got is :
--- object id = 0 ---
'NSInvalidArgumentException', reason: '-[... addObject:]: attempt to insert nil'
My questions are :
1
Why "object id" is nil ? Since target_coordinate_2D got assigned a valid value, [mp_list objectAtIndex : 0] should indeed be a valid object with a valid "coordinate" property.
2
On the last statement, I wish to add the object in mp_list to mapView. The NS Exception thrown by t开发者_如何学编程he simulator is consistent of the fact that "[mp_list objectAtIndex : 0]" is indeed nil. So there must be something I have missed about NSArray. What should be done in order to assign an object to an array from another array ?
Hope that somebody knowledgable in this area can help ...
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html
It sounds like mp_list
itself is nil
, which is why [mp_list objectAtIndex:0]
is returning nil
(because messaging nil
returns nil
).
Not sure if this is right, but in your NSLog, you're printing out the object as a long? From what I know, you can only put objects in NSArray, you can't put primitive types in them. So it might be something about the data you're putting in your array.
精彩评论