nsnumber 超出范围问题

发布于 10-12 05:06 字数 922 浏览 9 评论 0原文

我试图使用 NSNumber *percent 来获取整数值 percentint,但它总是使其超出范围。Nslog日志是这样的:

整数num的值为4

整数num的值与NsNumber一起 78910432

我的代码是这样的:

在我的头文件中:

int percentint;  
NSNumber *percent;
@property(nonatomic,retain) NSNumber *percent; //tried without using this too

在我的 .m 文件中:

@synthesize percent; //tried without using this too

percentint=4;

NSLog(@"The value of integer num is %i", percentint);

percent= [NSNumber numberWithInt:percentint];

percent= [[NSNumber alloc] initWithInt:percentint]; //tried without using this too. 

[percent autorelease]; //tried without using this too.

NSLog(@"The value of integer num is with NsNumber %i", percent);

I am trying to make NSNumber *percent to get the integer value percentint but it keeps making it out of scope.. The Nslog logs are like this:

The value of integer num is 4

The value of integer num is with NsNumber
78910432

My code is this:

In my header file:

int percentint;  
NSNumber *percent;
@property(nonatomic,retain) NSNumber *percent; //tried without using this too

In my .m file:

@synthesize percent; //tried without using this too

percentint=4;

NSLog(@"The value of integer num is %i", percentint);

percent= [NSNumber numberWithInt:percentint];

percent= [[NSNumber alloc] initWithInt:percentint]; //tried without using this too. 

[percent autorelease]; //tried without using this too.

NSLog(@"The value of integer num is with NsNumber %i", percent);

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

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

发布评论

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

评论(3

乄_柒ぐ汐2024-10-19 05:06:43

尝试:

percentint=4;
NSLog(@"The value of integer num is %i", percentint);
self.percent= [NSNumber numberWithInt:percentint];
NSLog(@"The value of integer num is in array %@", self.percent);
NSLog(@"The value of integer num is in array %d", [self.percent intValue]);

保留@synthesize百分比;在。

Try:

percentint=4;
NSLog(@"The value of integer num is %i", percentint);
self.percent= [NSNumber numberWithInt:percentint];
NSLog(@"The value of integer num is in array %@", self.percent);
NSLog(@"The value of integer num is in array %d", [self.percent intValue]);

leaving the @synthesize percent; in.

一腔孤↑勇2024-10-19 05:06:43

用户@Felz 是正确的。为了获取 NSNumber,您必须通过方法调用来检索它:

int percentInt = 95;
NSNumber *percent = [NSNumber numberWithInt:percentInt];
int myint = [percent intValue];

您所做的是打印出 percent< 的指针地址/code>

NSLog(@"Percent Value: %d",[percent intValue]);
NSLog(@"Percent Address: 0x%X",percent);

请记住,NSNumber *percent 表示百分比是一个指针,而不是一个值。

User @Felz is correct. in order to get the value of a NSNumber, you must retrieve it with a method call:

int percentInt = 95;
NSNumber *percent = [NSNumber numberWithInt:percentInt];
int myint = [percent intValue];

What you did instead, was print out the pointer address for percent

NSLog(@"Percent Value: %d",[percent intValue]);
NSLog(@"Percent Address: 0x%X",percent);

Remember that NSNumber *percent means percent is a pointer not a value.

つ可否回来2024-10-19 05:06:43

就像其他用户暗示的那样, NSNumber 不是一个整数,因为您试图在最后一个 NSLog 中调用它。要从 NSNumber 中获取 int 值,请使用[percent intValue]

另一个注意事项:您不需要两次初始化百分比。第一次调用 numberWithInt: 就像执行分配/初始化/释放。

另外,在使用完对象之前切勿释放它。

Like the other user insinuated, NSNumber is not an integer as you are trying to call it in the last NSLog. to get the int value out of a NSNumber, use [percent intValue].

Another side note: you don't need to initialize percent twice. The first call numberWithInt: is like doing an alloc/init/release.

Also, never release an object before you are done with it.

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