Nsdictionary usage and common methods (start, add elements, delete elements, modify element values, etc.)

Source: Internet
Author: User
Dictionary dictionary is a kind of collection class.
Collections: Array, dictionary dictionary, set; arrays are sequential, and elements can be repeated; dictionaries are not sequential, keys are unique, values are repeatable, set is unordered, and cannot be duplicated.
A dictionary is a set of data that exists in the form of a key-value pair;
The key to the dictionary is unique;
The value of the dictionary is repeatable;
The key value of the dictionary is one by one corresponding, that is, how many keys, there are many values;
The key value pairs in the dictionary are not in order;
For example: ID card & name, ID card is the key, name is value.

Dictionaries are divided into variable dictionaries and variable dictionaries.
Attention:
Key values are object types and cannot be base values;

In particular, the keys are generally string object settings;


Immutable dictionary
//initialization
nsdictionary *dict001 = [[Nsdictionary alloc] initwithobjectsandkeys:@ "Zhang Shaofeng", @ "name", @ "male", @ " Sex ", nil];
NSLog (@ "\ n dict001 =%@ \ n", dict001);

Attribute
//1 count
nsinteger count = dict001.count;
NSLog (@ "\ n count =%ld \ n", count);

2 All keys
nsarray *keyarray = Dict001.allkeys;
NSLog (@ "\ n");
For (NSString *string in Keyarray)
{
	NSLog (@ "string =%@", string);
}

3 All values
nsarray *valuearray = dict001.allvalues;
NSLog (@ "\ n");
For (NSString *string in ValueArray)
{
	NSLog (@ "string =%@", string);
}

Dictionary operation
//Get key corresponding value
nsstring *value = [dict001 objectforkey:@ "name"];
NSLog (@ "\ n");
NSLog (@ "name =%@", value);

Variable dictionary
nsmutabledictionary *dict002 = [Nsmutabledictionary dictionarywithdictionary:dict001];
NSLog (@ "\ n dict002 =%@ \ n", dict002);

When adding objects
//1 SetObject, the value must be a Non-empty object type, and if the value is a non object type, the error
[dict002 setobject:@ "University than Industry" forkey:@ "education"];
[dict002 setobject:nil forkey:@ "Education"]; Compile an error
NSLog (@ "\ dict002 =%@ \ n", dict002);

2 SetValue, regardless of the value is not the object type, will not be an error, but the object will not be added to the dictionary
[dict002 setvalue:@ "Senior electrician" forkey:@ "job"];
[dict002 setvalue:nil forkey:@ "job"]; Can be compiled normally, but the object is not added to the dictionary
NSLog (@ \ dict002 =%@ \ n, dict002);

Replaces the value of the specified key
[dict002 setvalue:@ "IOS Dve" forkey:@ "job"];
NSLog (@ "\ n dict002 =%@ \ n", dict002);

Deletes the value corresponding to the specified key
//1 deletes the value corresponding to the specified key
[dict002 removeobjectforkey:@ "Job"];
NSLog (@ "\ n dict002 =%@ \ n", dict002);

2 Delete the specified key array corresponding to the value
nsarray *keys = @[@ "Education", @ "sex"];
[dict002 Removeobjectsforkeys:keys];
NSLog (@ "+\n dict002 =%@ \ n", dict002);

3 Delete all key values
[dict002 removeallobjects];
NSLog (@ "-\n dict002 =%@ \ n", dict002);

Reinitialize value
[dict002 setdictionary:dict001];
NSLog (@ "\ n dict002 =%@ \ n", dict002);





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.