当前位置: 代码迷 >> Iphone >> why NSNumber didn't show correct number解决办法
  详细解决方案

why NSNumber didn't show correct number解决办法

热度:23   发布时间:2016-04-25 06:15:42.0
why NSNumber didn't show correct number
Sorry,my company mac is English version,so I have to speak English in CSDN even though I'm Chinese. 
Here is my code.

A.h
#import <Cocoa/Cocoa.h>


@interface A : NSObject {
NSNumber* i;

}
@property (retain) NSNumber* i;

@end

#import "A.h"


@implementation A
@synthesize i;

-(void)dealloc{
[i release];
[super dealloc];
}

#import <Foundation/Foundation.h>
#import "A.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

A *a=[[A alloc] init];

NSNumber* i=[[NSNumber alloc] initWithInt:123];

[a setI:i];

    NSLog(@"i is %d",[a i]);
    [pool drain];
    return 0;
}


I expected "i is 123" in console,but I only got "i is 109952".Why


------最佳解决方案--------------------
[a i]返回的是NSNumber指针,你用%d输出的是当然是整型的指针。
对于所有的NSObject对象,都用%@输出:
 NSLog(@"i is %@",[a i]);
或者你想用%d,那就将对用值转成int类型:
    NSLog(@"i is %d",[a i].intValue);
------其他解决方案--------------------
2楼正解,或者你可以使用NSLog(@"i is %d",(int)[[a i] value]);
------其他解决方案--------------------
谢谢大家,6个字
------其他解决方案--------------------
该回复于2012-12-08 21:37:24被管理员删除
  相关解决方案