关于objectAtIndex的返回值
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]];
考虑上面的 3 个语句:
根据文档,NSArray 的方法“objectAtIndex”应该返回一个对象。我的理解是返回的值是指向对象的数字指针。
但我得到的是:
--- object id = 0 ---
'NSInvalidArgumentException', reason: '-[... addObject:]: attempt to insert nil'
我的问题是:
1
为什么“对象 ID”为零?由于 target_coordinate_2D 被分配了一个有效值, [mp_list objectAtIndex : 0] 确实应该是具有有效“坐标”的有效对象 财产。
2
在最后一句话中,我希望将mp_list中的对象添加到mapView中。国民服役例外 模拟器抛出的错误与“[mp_list objectAtIndex : 0]”这一事实是一致的 确实为零。所以关于 NSArray 肯定有一些我错过的东西。应该是什么 完成以便将一个对象从另一个数组分配给一个数组?
希望这方面的知识渊博的人可以提供帮助...
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 the 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 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来
mp_list
本身是nil
,这就是为什么[mp_list objectAtIndex:0]
返回nil
(因为消息传递nil
返回nil
)。It sounds like
mp_list
itself isnil
, which is why[mp_list objectAtIndex:0]
is returningnil
(because messagingnil
returnsnil
).不确定这是否正确,但是在您的 NSLog 中,您将对象打印为 long 吗?据我所知,你只能将对象放入 NSArray 中,不能将原始类型放入其中。所以这可能与您放入数组中的数据有关。
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.