首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Iphone >

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

2012-12-14 
why NSNumber didn't show correct numberSorry,my company mac is English version,so I have to sp

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被管理员删除

热点排行