我有以下课程:
@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 按预期创建,但是当 x=1, y=0 在函数 initWithX 中我看到 'posX' 参数值为 1065353216。所以我用 ivar 得到了无效的 myObj 对象_x = 1065353216。我不明白为什么会这样?