Detailed NSNumber usage in Objective-C

Source: Internet
Author: User

In Objective-c, there is an int data type. Why do we need to use the numeric object NSNumber? This is because many classes (such as NSArray) require the use of objects, whereas int is not an object.
NSNumber is a numeric object. We can use the NSNumber object to create and initialize different types of numeric objects.

For example:

 

[Cpp]View plaincopy
  1. # Import <Foundation/Foundation. h>
  2.   
  3. NSNumber * myNumber, * floatNumber, * intNumber, * number1;
  4.   
  5. // Create an integer object
  6. IntNumber = [NSNumber numberWithInteger: 123];
  7. NSLog (@ "% I", [intNumber integerValue]);
  8.   
  9. // Create a long object
  10. MyNumber = [NSNumber numberWithLong: 0 xababab];
  11. NSLog (@ "% lx", [myNumber longValue]);
  12.   
  13. // Create a char object
  14. MyNumber = [NSNumber numberWithChar: 'K'];
  15. NSLog (@ "% c", [myNumber charValue]);
  16.   
  17. // Create a float object
  18. FloatNumber = [NSNumber numberWithFloat: 123.00];
  19. NSLog (@ "% f", [floatNumber floatValue]);
  20.   
  21. // Create a double object
  22. MyNumber = [NSNumber numberWithDouble: 112233e + 15];
  23. NSLog (@ "% lg", [myNumber doubleValue]);
  24.   
  25. // Determine whether the values of the two objects are equal
  26. If ([intNumber isw.tonumber: floatNumber] = YES ){
  27. NSLog (@ "equal value ");
  28. } Else {
  29. NSLog (@ "The value is not equal ");
  30. }  
  31.   
  32. // Compare the values of two objects
  33. If ([intNumber compare: myNumber] = NSOrderedAscending ){
  34. NSLog (@ "small number on the left ");
  35. } Else {
  36. NSLog (@ "the number on the left ");
  37. }  
  38.   
  39. // Initialize the instance
  40. Number1 = [[NSNumber alloc] initWithInt: 1000];
  41. NSLog (@ "% d", [number1 intValueunsigned];
  42. [Number1 release];


For each basic type, the class method assigns an NSNumber object to it and sets it to the specified value. These methods start with numberWith, followed by the type, such as numberWithLong.
In addition, you can also use the instance method to set the specified value for the NSNumber object previously allocated, all of which start with initWith, such as initWithLong.

 

For example:

 

Create and initialize class methods Instance initialization method Value instance method
NumberWithChar: InitWithChar: CharValue
NumberWithShort: InitWithShort: ShortValue
... ... ...

 

Detailed NSNumber usage in Objective-C

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.