Introduction to the usage of NSNumber and nsdictionary in Objective-c _ios

Source: Internet
Author: User
Tags allkeys numeric

Common methods of NSNumber

Why use a numeric object NSNumber when you have a data type for int in objective-c? This is because many classes, such as Nsarray, require the use of objects, and int is not an object.
NSNumber is a digital object we can use the NSNumber object to create and initialize different types of numeric objects.
NSNumber
+ (NSNumber *) Numberwithint: (int) value;
+ (NSNumber *) Numberwithdouble: (double) value;
-(int) intvalue;
-(double) doublevalue;
..................... (For each base type, the class method assigns a NSNumber object to it and sets it to the specified value, which starts with Numberwith, followed by the type, such as Numberwithfloat,numberwithlong, Numberwithinteger ...)
After packing, the following methods are taken:

Here, take int for a demo:

Copy Code code as follows:

void Number () {
Wrapping 10 of int type into a NSNumber object
NSNumber *number = [NSNumber numberwithint:10];
NSLog (@ "number=%@", number);

Nsmutablearray *array = [Nsmutablearray array];
Add values to an array
[Array Addobject:number];

Removed or a NSNumber object, does not support automatic unpack (that is, not automatically converted to int type)
NSNumber *number1 = [array lastobject];

Convert NSNumber to int type
int num = [Number1 intvalue];
NSLog (@ "num=%i", num);
}

Nsdictionary some common usage

Copy Code code as follows:

Nsarray * skyaarrays = [Nsarray arraywithobjects:@ "a Sky No. 1th", @ "a Sky 2nd", @ "a Sky 3rd", Nil];
Nsarray * skybarrays = [Nsarray arraywithobjects:@ "B Sky No. 1th", @ "B Sky 2nd", @ "B Sky 3rd", Nil];
Nsarray * skycarrays = [Nsarray arraywithobjects:@ "C Sky No. 1th", @ "C Sky 2nd", @ "C Sky 3rd", Nil];

Nsarray * Skyarray = [Nsarray arraywithobjects:skyaarrays,skybarrays,skycarrays, Nil];

All key in the dictionary
Nsarray * keys = [Nsarray arraywithobjects:@ "name", @ "sex", @ "age", nil];
All the value in the dictionary corresponding to the key
Nsarray * values = [Nsarray arraywithobjects:@ "Liuhui", @ "male", [Nsnumbernumberwithint:36],nil];
Create a Dictionary object Method 1
Nsdictionary * Mydic = [[Nsdictionary alloc]initwithobjects:values Forkeys:keys];
NSLog (@ "My dic =%@", mydic);
Create a Dictionary object Method 2
Nsdictionary * Yourdic = [[Nsdictionary alloc] initwithobjectsandkeys:skyaarrays,@ "A", skybarrays,@ "B", skyCArrays,@ "C" ", nil];
NSLog (@ "Your dic =%@", yourdic);

NSLog (@ "%@", [Yourdic objectforkey:@ "A"]);
-(Nsarray *) AllKeys; Return is the Nsarray type, easy to use Objectatindex to remove a key
NSLog (@ "%@", [Yourdic AllKeys]);
NSLog (@ "%@", [Yourdic allvalues]);

Add data (SetObject generally do not have a key to add, with the same name of the key and in this way, will be overwritten), Note: ID key appears in pairs
[Mutabledictionary setobject:@ "Good lucky" forkey:@ "why"];
[Mutabledictionary setobject:@ "Bye Bye" forkey:@ "how"];


Delete the data for the specified key value
[Mutabledictionary removeobjectforkey: ...];
Delete all data
[Mutabledictionary removeallobjects];

Common traversal of dictionaries (unordered)
for (int i =0 i < [yourdic count]; i++) {
NSLog (@ "key = value <====>%@ =%@" "[[Yourdic AllKeys] objectatindex:i],[yourdic objectforkey:[[yourdic AllKeys] OBJECTATINDEX:I]]);
}

Fast traversal of the dictionary fetched out obj must be key
for (id obj in yourdic) {
NSLog (@ "%@", obj);
ID value = [yourdic objectforkey:obj];
NSLog (@ "%@", value);
}

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.