iOS/Objective-C:NSInteger 在简单的 Coordinates2D 对象创建过程中丢失

发布于 2024-11-02 00:10:31 字数 1829 浏览 3 评论 0原文

我为此抓狂......我不知道出了什么问题。

我创建了一个非常简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-11-09 00:10:31

我不久前读到,你永远不应该在 init 方法中使用属性访问器。

I read awhile ago that you should never use the property accessor in init methods.

三人与歌 2024-11-09 00:10:31

我想你想改变:

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文