私は次のクラスを持っています:
@interface MyObj : NSObject {
NSInteger _x;
NSInteger _y;
....
}
@property(nonatomic, readonly) NSInteger x;
@property(nonatomic, readonly) NSInteger y;
....
- (id)initWithX:(NSInteger)posX Y:(NSInteger)posY;
と実装:
@implementation MyObj
@synthesize x = _x;
@synthesize y = _y;
- (id)initWithX:(NSInteger)posX Y:(NSInteger)posY{
self = [super init];
_x = posX;
_y = posY;
return self;
}
MyObj の配列を次のように初期化しようとしています。
for (NSInteger y=0; y<5; ++y) {
NSMutableArray *rowContent = [[NSMutableArray alloc] init];
for (NSInteger x=0; x<5; ++x) {
MyObj *myObj = [[MyObj alloc] initWithX:x Y:y];
....
[rowContent addObject:myObj];
最初の繰り返しで、x=0、y=0 の場合、myObj は期待どおりに作成されましたが、関数 initWithX で x=1、y=0 の場合、「posX」引数の値が 1065353216 であることがわかります。したがって、ivar で無効な myObj オブジェクトを取得します。 _x = 1065353216. なぜそうなるのか理解できませんか?