私はこれで髪を引き裂いています..何が悪いのかわかりません。
2 つの NSInteger 値と、NSLog で使用する文字列表現を格納するための非常に単純な Coordinates2D クラスを作成しました。このコードは、最新バージョンの xCode にバンドルされている iOS 4.3 iPad シミュレーターで実行しています。
何らかの理由で、initX:Y: コンストラクターに渡された整数値が失われます。以下のコードは、Coordinates2D と、任意の float 値を元の形式で出力し、int としてキャストし、NSInteger としてキャストしてから Coordinates2D オブジェクト内にキャストするコードを提供します。
私と同じように、Coordinates2D コンストラクター内で値が失われることがわかります。NSLog の 'coords.x' 引数は、その値がメモリ内で失われたことを示すランダムな大きな整数として出力されます。
なぜこれが起こるのか、誰かが私を助けることができますか? 何が間違っているのかわかりません。
どうもありがとう!
Coordinates2D.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);