我有一个自定义的NSObject类,让我们调用人员,还有一个来自CloudMade RMMarker.h的名为RMMarker的类。RMMarker类有一个名为data的属性,它是一个NSObject。我知道通过这样设置数据:
People *aPeople = [[People alloc] init];
marker.data = aPeople;如何读取存储在数据中的数据?我只知道事实并非如此
People *aPeople = [[People alloc] init];
aPeople = marker.data;正确的方法是什么呢?
谢谢,菲利普
发布于 2011-03-31 21:55:03
因为您知道marker.data的类型是People *,所以可以简单地将其转换为正确的类型:
People *aPeople = (People *)marker.data;请注意,只有当marker.data实际上是一个People *对象(或它的一个子类)时,这才能正常工作。当然,您可以添加一个assertion test
People *aPeople = (People *)marker.data;
NSAssert([aPeople isKindOfClass:[People class]], "oops! Wrong type!");有关使用NSAssert()的重要细节,请阅读:What are assertions or NSAssert good for in practice?
发布于 2011-03-31 21:59:21
根据我从RMMarker源中看到的信息,数据ivar将只保留您的People对象。因此,如果您想稍后引用People对象,只需执行以下操作:
People *aPeople = (People *)marker.data;https://stackoverflow.com/questions/5500928
复制相似问题