Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

iphone - What's the difference between NSNumber and NSInteger?

What's the difference between NSNumber and NSInteger? Are there more primitives like these that I should know about? Is there one for floats?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

NSNumber is a class, not a primitive, and is used when you need to put raw numbers into dictionaries, arrays, or otherwise encapsulate them. NSInteger, NSUInteger, CGFloat, etc are simple types and correspond (on 32-bt systems like the iPhone) to int, unsigned int and float.

As a general rule, if you need to store a number somewhere, use NSNumber. If you're doing calculations, loops, etc, use NSInteger, NSUInteger or CGFloat.

You can wrap an NSInteger into an NSNumber:

NSNumber *aNumber = [NSNumber numberWithInteger:21];

... and get it back:

NSInteger anInteger = [aNumber integerValue];

You can find out more info here: http://iosdevelopertips.com/cocoa/nsnumber-and-nsinteger.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...