Objective-C 处理整数值

发布于 2024-09-27 05:55:44 字数 917 浏览 4 评论 0原文

我对如何在 Objective C 中处理整数感到困惑。

如果我定义以下内容:

NSInteger i = 6;
NSLog(@"%d", i);

我希望它将 6 打印到控制台。

但是我在一个对象中有一个 NSInteger ,它显然是由指针引用的,所以我得到了非常不同的结果。

例如:

@interface Section : NSObject {
NSInteger Id;
}

@property (nonatomic, assign) NSInteger Id;

请假设这已在实现中综合。

我创建对象,设置其值并再次访问它,如下所示:

Section *section = [[Section alloc] init];
section.Id = 6;

NSMutableArray *sections = [[NSMutableArray alloc] init];
[sections addobject:section];

Section *sectionB = [setions objectAtIndex:0];
NSLog(@"%d",  sectionB.Id);

这具有打印内存地址(即 5447889 这样的数字)的奇怪效果。为什么我不能直接获取该值?

我尝试过使用:

NSInteger sid = [[section Id]integerValue];

但随后收到警告无效的接收器类型'NSInteger',有时会收到错误程序收到信号:“EXC_BAD_ACCESS”。

我真的很想知道如何正确处理整数或任何与此相关的值。

非常感谢

I am getting confused with how to handle Integers in Objective C.

If I define the following:

NSInteger i = 6;
NSLog(@"%d", i);

I expect it to print 6 to the console.

however I have an NSInteger within an object which is obviously reference by a pointer so I get very difference results.

For example:

@interface Section : NSObject {
NSInteger Id;
}

@property (nonatomic, assign) NSInteger Id;

Please assume this has been synthesized in the implementation.

I create the object set its value and access it again as follows:

Section *section = [[Section alloc] init];
section.Id = 6;

NSMutableArray *sections = [[NSMutableArray alloc] init];
[sections addobject:section];

Section *sectionB = [setions objectAtIndex:0];
NSLog(@"%d",  sectionB.Id);

This has the strange effect of printing the memory address ie a number like 5447889. Why can I not just get the value?

I have tried using:

NSInteger sid = [[section Id]integerValue];

But I then get the warning Invalid receiver type 'NSInteger' and sometime get an error Program received signal: “EXC_BAD_ACCESS”.

I would really like to know how to handle Integers, or any values for that matter properly.

Many Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

够钟 2024-10-04 05:55:44

看起来您正在访问未初始化的内存; 5447889 看起来不像指针值——指针通常是字对齐的,而 5447889 不是字对齐的。

也许您可以剪切并粘贴您的实际代码。其中存在拼写错误,例如 addobject 而不是 addObject,以及 setions 而不是 sections

如果您保持简单并执行 NSLog(@"%d", section.Id) 来跳过数组的混乱,它会起作用吗?

It looks like you're accessing uninitialized memory; 5447889 doesn't look like a pointer value—pointers are usually word-aligned, and 5447889 isn't word aligned.

Maybe you could cut and paste your actual code. This has typos such as addobject instead of addObject and setions instead of sections.

Does it work if you keep things simple and do NSLog(@"%d", section.Id) to skip messing with the array?

挽袖吟 2024-10-04 05:55:44

关于奇怪的值,请参阅多米尼克·库尼的回答。

关于 EXC_BAD_ACCESS: [[section Id]integerValue]; 不起作用,因为它随后尝试将 NSInteger 解释为对象并尝试向其发送消息 integerValue ,这是行不通的。它是一个整数,而不是一个对象。

Regarding the strange values, see Dominic Cooney's answer.

Regarding the EXC_BAD_ACCESS: [[section Id]integerValue]; doesn't work because it then tries to interpret the NSInteger as an object and tries to send the message integerValue to it, which can't work. It's an integral number, not an object.

许一世地老天荒 2024-10-04 05:55:44

我想我找到了自己问题的答案。将 NSInteger 的值设置为 6 就可以了。我遇到的问题是我使用以下命令将其设置为从 Json 字符串返回的值:

NSInteger i = [jsonResult objectForKey:@"Id"];

应该是:

NSInteger i = [[jsonResult objectForKey:@"Id"] integerValue];

我还没有对此进行测试,但根据 DarkDust 所说的关于整数值采用对象而不是整数的说法是有意义的。

感谢您的意见。

Think I found the answer to my own question. Setting the value of an NSInteger to a value of say 6 is fine. The problem I have is I am setting it to the value returned from a Json string using the following:

NSInteger i = [jsonResult objectForKey:@"Id"];

which should be:

NSInteger i = [[jsonResult objectForKey:@"Id"] integerValue];

I have not tested this but makes sense based on what DarkDust said about integerValue taking an object and not an Integer.

Thanks for your input.

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