[Objective-] common methods for handling NSNumber numeric objects in OC

Source: Internet
Author: User

I have learned basic data types before. However, these basic urban data types are not objects. Therefore, messages cannot be sent to them.

And sometimes they need to be processed as objects. Instead, we need to put basic data types into the collection (only objects can be stored in Cocoa,

Can not store basic data types), then we need to convert the basic type to a digital object. OC provides the data object "NSNumber"

Type "Wrap" into an object, so that we can process the basic data type.

The following is an example:

 

//// Main. m // FoundationDemo1 // Created by hmjiangqq on 14-1-23. // Copyright (c) 2014 hmjiangqq. all rights reserved. // # import
 
  
Int main (int argc, const char * argv []) {@ autoreleasepool {// insert code here... NSLog (@ "Hello, World! "); /* ============== NSNumber ===========================* // create an NSNumber object NSNumber * monthNumber = [[NSNumber alloc] initWithInt: 10]; NSNumber * lengthNumber = [[NSNumber alloc] initWithFloat: 10.8]; NSLog (@ "month = % @ \ n", monthNumber ); NSLog (@ "length = % @ \ n", lengthNumber); // restore to the basic data type int month = [monthNumber intValue]; float length = [lengthNumber floatValue]; NSLog (@ "month = % d \ n", month); NSLog (@ "length = %. 1f \ n ", length ); /* ================== NSString =========================================*/} return 0 ;}
 
The above only uses two packaging and restoration methods in NSNumber. There are many similar methods in NSNumber:

 

 

- (id)initWithChar:(char)value;- (id)initWithUnsignedChar:(unsigned char)value;- (id)initWithShort:(short)value;- (id)initWithUnsignedShort:(unsigned short)value;- (id)initWithInt:(int)value;- (id)initWithUnsignedInt:(unsigned int)value;- (id)initWithLong:(long)value;- (id)initWithUnsignedLong:(unsigned long)value;- (id)initWithLongLong:(long long)value;- (id)initWithUnsignedLongLong:(unsigned long long)value;- (id)initWithFloat:(float)value;- (id)initWithDouble:(double)value;- (id)initWithBool:(BOOL)value;- (id)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);- (id)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
The opposite method is as follows:

 

 

- (char)charValue;- (unsigned char)unsignedCharValue;- (short)shortValue;- (unsigned short)unsignedShortValue;- (int)intValue;- (unsigned int)unsignedIntValue;- (long)longValue;- (unsigned long)unsignedLongValue;- (long long)longLongValue;- (unsigned long long)unsignedLongLongValue;- (float)floatValue;- (double)doubleValue;- (BOOL)boolValue;- (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0);- (NSUInteger)unsignedIntegerValue NS_AVAILABLE(10_5, 2_0);
Of course, you can also use the static method of NSNumber to create objects. The method is as follows:

 

 

+ (NSNumber *)numberWithChar:(char)value;+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;+ (NSNumber *)numberWithShort:(short)value;+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;+ (NSNumber *)numberWithInt:(int)value;+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;+ (NSNumber *)numberWithLong:(long)value;+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;+ (NSNumber *)numberWithLongLong:(long long)value;+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;+ (NSNumber *)numberWithFloat:(float)value;+ (NSNumber *)numberWithDouble:(double)value;+ (NSNumber *)numberWithBool:(BOOL)value;+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.