读书人

why NSNumber didnamp;#x27;t show corre

发布时间: 2013-01-11 11:57:35 作者: rapoo

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]);

读书人网 >Iphone

热点推荐