iOS / Objective-C:NSInteger在创建Coordinates2D对象时就迷路了

|| 我正在为此扯头发。我不知道出了什么问题。 我创建了一个非常简单的Coordinates2D类来存储两个NSInteger值,以及用于NSLog的字符串表示形式。我正在与最新版本的xCode捆绑在一起的iOS 4.3 iPad模拟器中运行此代码。 由于某些原因,传递给initX:Y:构造函数的整数值会丢失。以下代码提供了Coordinates2D和一些代码,这些代码以原始形式打印任意浮点值,转换为int,转换为NSInteger,然后在Coordinates2D对象内部。 您应该像我一样看到,该值在Coordinates2D构造函数中丢失了; NSLog中的\'coords.x \'参数打印为随机的大整数,表示其值已在内存中丢失。 谁能帮我看看为什么会这样吗?我看不到我在做什么错。 非常感谢! 坐标2D.h
#import <Foundation/Foundation.h>
@interface Coordinates2D : NSObject {
    NSInteger x,y;
    NSString *asString;
}
@property (nonatomic) NSInteger x,y;
@property (nonatomic, retain) NSString *asString;
-(void)updateStringRepresentation;
-(id)initX:(NSInteger)x Y:(NSInteger)y;
@end
坐标2D.m
#import \"Coordinates2D.h\"
@implementation Coordinates2D
@synthesize x,y,asString;

-(id)initX:(NSInteger)x_ Y:(NSInteger)y_ {
    NSLog(@\"coords: %i, %i\",x_,y_);
    if ((self = [super init])) {
        self.x = x_;
        self.y = y_;
        NSLog(@\"Coordinates stored %i as %i\",x_,self.x);
        [self updateStringRepresentation];
    }
    return self;
}
/*
-(void)setX:(NSInteger)newX {
    x = newX;
    [self updateStringRepresentation];
}

-(void)setY:(NSInteger)newY {
    y = newY;
    [self updateStringRepresentation];
}
*/
-(void)updateStringRepresentation {
    self.asString = [NSString stringWithFormat:@\"%i,%i\",x,y];
}

-(void)dealloc {
    [asString release];
    [super dealloc];
}
@end
问题示例:
Coordinates2D *coords = [[Coordinates2D alloc] initX:(NSInteger)(202.566223/200.00) Y:0.0f];
NSLog(@\"202.566223/200.00 = %f, as int:%i, as NSInteger:%i, as Coordinates2D:%i\",
      202.566223/200.00, (int)(202.566223/200.00), (NSInteger)(202.566223/200.00), coords.x);
    
已邀请:
我前一阵子读过,永远不要在init方法中使用属性访问器。     
我想您要更改: @属性(非原子)NSInteger x,y; 至: @属性(非原子的,赋值)NSInteger x,y; 这应该可以修复错误,因为当我将其放入xcode项目中时,它可以正常运行。     

要回复问题请先登录注册