iOS/Objective-C:NSInteger 在简单的 Coordinates2D 对象创建过程中丢失
我为此抓狂......我不知道出了什么问题。
我创建了一个非常简单的 Coordinates2D 类来存储两个 NSInteger 值,以及与 NSLog 一起使用的字符串表示形式。我在与最新版本的 xCode 捆绑在一起的 iOS 4.3 iPad 模拟器中运行此代码。
由于某种原因,传递给 initX:Y: 构造函数的整数值丢失了。下面的代码提供了 Cooperatives2D 和一些代码,用于以其原始形式打印任意浮点值、转换为 int、转换为 NSInteger,然后在 Cooperatives2D 对象内。
您应该像我一样看到,该值在 Coordinates2D 构造函数中丢失了; NSLog 中的“coords.x”参数被打印为随机的大整数,表明其值已在内存中丢失。
谁能帮我看看为什么会发生这种情况?我看不出我做错了什么。
非常感谢!
Coords2D.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
Coordinates2D.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);
I'm tearing my hair out over this.. I've no idea what's going wrong.
I've created a very simple Coordinates2D class to store two NSInteger values, as well as a string representation for use with NSLog. I'm running this code in the iOS 4.3 iPad simulator bundled with the latest version of xCode.
For some reason the integer values passed to the initX:Y: constructor gets lost. The code below provides Coordinates2D and some code to print an arbitrary float value in its original form, cast as an int, cast as an NSInteger, and then inside the Coordinates2D object.
You should see, as I do, that the value gets lost inside the Coordinates2D constructor; the 'coords.x' argument in NSLog is printed as a random, large integer indicating its value has been lost in memory.
Can anyone help me see why this happens? I can't see what I'm doing wrong.
Many thanks!
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
Coordinates2D.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
Example of problem:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不久前读到,你永远不应该在 init 方法中使用属性访问器。
I read awhile ago that you should never use the property accessor in init methods.
我想你想改变:
@property (nonatomic) NSInteger x,y;
到:
@property(非原子,分配)NSInteger x,y;
这应该可以修复错误,因为当我将其放入 xcode 项目时它运行良好。
I think you want to change:
@property (nonatomic) NSInteger x,y;
to:
@property (nonatomic, assign) NSInteger x,y;
That should fix the error, as it runs fine when I put it in an xcode project.