C スタイルのベクトルを取り、それを NSMutable 配列オブジェクトに変換しようとしています。
関数は次のとおりです。
+(NSMutableArray*)arrayFromPoints:(vector<Point2f>&)points
{
NSMutableArray* pointArray = [[NSMutableArray alloc] init];
for (int i=0;i<points.size();i++)
{
Point2f point = points[i];
JBPoint* point2 = [[JBPoint alloc]initWithX:point.x andY:point.y];
[pointArray addObject:point2];
}
return pointArray;
}
カスタム ポイント クラス:
@implementation JBPoint
float _x;
float _y;
-(id) initWithX:(float)x andY:(float)y
{
if (self=[super init])
{
_x = x;
_y=y;
}
return self;
}
-(float)x{ return _x;}
-(float)y {return _y;}
@end
テスト出力:
for (JBPoint* pnt in array)
{
NSLog(@"%f, %f", pnt.x, pnt.y);
}
配列を出力することを除いて、毎回最後の値が得られます! 誰かが理由を知っていますか?
おそらく同じオブジェクトを指していると思いましたが、メモリアドレスが異なります。