Objective c 从NSMutableArray中的自定义类获取值
目前,我在NSMutableArray(称为Objective c 从NSMutableArray中的自定义类获取值,objective-c,nsmutablearray,Objective C,Nsmutablearray,目前,我在NSMutableArray(称为q)和SiteEvents类中有一些点对象。我正在尝试使用下面的代码访问这些值,它只返回X:0.0 Y:0.0。如何获得x和y值 NSLog(@"X: %f Y: %f",[(SitePoint*)[[SiteEvents q] objectAtIndex:i]x], [(SitePoint*)[[SiteEvents q] objectAtIndex:i]y]); 编辑 上述方法一直行之有效
q
)和SiteEvents
类中有一些点对象。我正在尝试使用下面的代码访问这些值,它只返回X:0.0 Y:0.0。如何获得x和y值
NSLog(@"X: %f Y: %f",[(SitePoint*)[[SiteEvents q] objectAtIndex:i]x],
[(SitePoint*)[[SiteEvents q] objectAtIndex:i]y]);
编辑
上述方法一直行之有效。错误是由于未在
SiteEvents
类中合成q
。编辑:我想我可能误解了您的问题
假设您将SiteEvents作为顶级对象,其中包含一个NSMutableArray点,则需要执行以下操作:
NSMutableArray* points = [siteEventInstance getPoints]; //if you're exposing the point list via a method.
float x = [points objectAtIndex: i].x;
旧的响应 如果您有一个SitePoint对象数组,则可以通过如下方式在数组中建立索引来访问SitePoint对象:
SitePoint* s = (SitePoint*) [q objectAtIndex:i];
float x = ((SitePoint*) [q objectAtIndex:i]).point.X; //if point is a property or a field
float x = [((SitePoint*) [q objectAtIndex:i]) getPoint].X; //If the point object is exposed via a method.
然后需要到达“s”内的点对象。这将根据您在SitePoint中显示点对象的方式而有所不同
float x = s.point.X; //If point is a property or a field do this
float x = [s getPoint].X; //If the point object is exposed via a method.
您可以这样组合上述所有概念(正如您尝试的那样):
SitePoint* s = (SitePoint*) [q objectAtIndex:i];
float x = ((SitePoint*) [q objectAtIndex:i]).point.X; //if point is a property or a field
float x = [((SitePoint*) [q objectAtIndex:i]) getPoint].X; //If the point object is exposed via a method.
注意:我写这篇文章时没有打开x代码;-) “q”位于名为SiteEvents的自定义对象中,因此我尝试了SitePoint*s=(SitePoint*)[[SiteEvents q]objectAtIndex:I];但是当我尝试调用s的方法时,它没有任何作用..当我用[SiteEvents q]替换q并在SiteEvents类中合成“q”时,旧的响应有效。。。我不认为你需要在Mac上进行合成,我认为那只是iOS的事情。