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);
没有找到相关结果
已邀请:
2 个回复
盟犯涩沟都
艰管垮淮