Objective-c Learning-Digital Object NSNumber

Source: Internet
Author: User

For objective-c array Nsarray can only save objects, cannot access the base data type, such as Integer int, float float, double-precision floating-point double, char Char, but objective-c in order to be able to save the base data type to an array, There is a type called NSNumber, which is called a numeric object. (Some of the words are not very accurate, you will find the right word to be modified)

Use the following:

// convert integer int to nsnumber type

nsnumber * number_int = [[nsnumber alloc] initwithint:+];

// convert character char to nsnumber type

nsnumber * Number_char = [[nsnumber alloc] initwithchar:' A '];

// convert floating-point float to nsnumber type

nsnumber * number_float = [[nsnumber alloc] initwithfloat:2.0f];

// convert double-precision floating-point double to nsnumber type

nsnumber * number_double = [[nsnumber alloc] initwithdouble:5.6 ]; 

// beginners want to use keyboard input to assign values, in fact the principle is the same, for example: converting floating-point type C to nsnumber type

Char c ;

scanf ("%c", &c);

nsnumber * number_free = [[nsnumber alloc] Initwithchar: c];

// convert nsnumber type to integer int

int inumber = [Number_int intvalue];

// convert nsnumber type to character char

char cnumber = [Number_char charvalue];

// convert nsnumber type to float float

float fnumber = [number_float floatvalue];

// convert nsnumber type to Double-precision floating-point type

double dnumber = [number_double doublevalue];

/*

[number isequaltonumber: (NSNumber *)] A function is a method that compares two nsnumber types

The return value of the BOOL type is only two, Yes (that is, 1) and NO (that is, 0) .

the ascall code of ' a ' is

the value of the Number_int object is

The value of the Number_char object is ' a ', and the comparison is equal, because ' a ' is

*/

BOOL issame = [Number_int isequaltonumber: Number_char];

Objective-c Learning-Digital Object NSNumber

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.