2-6 Lessonnsdictionaryandnsset

Source: Internet
Author: User
Tags allkeys

Nsdictionary, Immutable Dictionary

1. If you do not create, you will not be able to use

2. Internal with Key-value (key value pair) situation storage

3.key must be unique

4. is an unordered collection

5.value must be an object, key is generally nsstring

Name: Zhang San, age:18, Gender: Male

Nsdictionary *dic = [[Nsdictionary alloc] initwithobjectsandkeys:@ "Zhang San", @ "name", [NSNumber numberwithint:18], @ "age", @ " Male ", @" gender ", nil];

NSLog (@ "%@", DIC);

Get Value by key

NSString *name = [dic objectforkey:@ "name"];

NSLog (@ "%@", name);

Get all the value values

Nsarray *values = [dic allvalues];

NSLog (@ "%@", values);

Get all the key values

Nsarray *keys = [dic AllKeys];

NSLog (@ "%@", keys);

Get the number of key-value pairs

Nsuinteger count = [dic count];

NSLog (@ "%ld", count);

Nsmutabledictionary, Variable dictionary class, inherited from Nsdictionary

Create a mutable dictionary

Nsmutabledictionary *mdic = [[Nsmutabledictionary alloc] initwithcapacity:0];

To add a key-value pair

[MDic setobject:@ "Zhang San" forkey:@ "name"];

[MDic setobject:[nsnumber numberwithinteger:18] forkey:@ "age"];

[MDic setobject:@ "male" forkey:@ "gender"];

Delete a key value pair

[MDic removeobjectforkey:@ "name"];

Modifying key-value pairs

[MDic setobject:@ "female" forkey:@ "gender"];

[MDic setobject:[nsnumber numberwithinteger:15] forkey:@ "age"];

NSLog (@ "%@", mDic);

Traverse the key value of the dictionary and print the value that can correspond to

For loop implementation

Nsarray *mkeys = [MDic AllKeys];

for (int i = 0; i < [MDic count]; i++) {

NSLog (@ "%@", [Mkeys objectatindex:i]);

NSLog (@ "%@", [MDic Objectforkey:[mkeys objectatindex:i]]);

//    }

For in

1. Ensure that the element types in the array must be consistent

2. When enumerating quickly, you cannot modify the array (adding or deleting elements)

For in array, traversing each element

For (id akeys in [mDic AllKeys]) {

NSLog (@ "%@", Akeys);

NSLog (@ "%@", [MDic Objectforkey:akeys]);

}

For in dictionary, traversing each key value

For (id something in mDic) {

NSLog (@ "%@", something);

}

Traverse Dictionary Value value

For (id avalue in [mDic allvalues]) {

NSLog (@ "%@", avalue);

}

Nested use of arrays and dictionaries

Nsmutabledictionary *student1 = [[Nsmutabledictionary alloc] initwithobjectsandkeys:@ "Wang Mingyang", @ "name", [NSNumber NUMBERWITHINT:23], @ "age", @ "male", @ "gender", nil];

Nsmutabledictionary *student2 = [[Nsmutabledictionary alloc] initwithobjectsandkeys:@ "Wang Hao", @ "name", [NSNumber NUMBERWITHINT:20], @ "age", @ "male", @ "gender", nil];

Nsmutabledictionary *student3 = [[Nsmutabledictionary alloc] initwithobjectsandkeys:@ "Wei Zijian", @ "name", [NSNumber NUMBERWITHINT:28], @ "age", @ "female", @ "gender", nil];

Nsmutablearray *studentarray = [[Nsmutablearray alloc] initwithcapacity:0];

[Studentarray Addobject:student1];

[Studentarray Addobject:student2];

[Studentarray Addobject:student3];

NSLog (@ "%@", Studentarray);

Traverse an array to print student information

For (Nsdictionary *astudent in Studentarray) {

NSLog (@ "name:%@", [astudent objectforkey:@ "name"]);

NSLog (@ "age:%@", [AStudent objectforkey:@ "Age"]);

NSLog (@ "gender:%@", [AStudent objectforkey:@ "gender"]);

}

//

For (Nsmutabledictionary *stu in Studentarray) {

For (NSString *key in Stu) {

NSLog (@ "%@", [Stu Objectforkey:key]);

}

}

Nsmutablearray *namearray = [[Nsmutablearray alloc] initwithobjects:@ "Zhang San", @ "John Doe", @ "Harry", Nil];

Nsmutabledictionary *girl = [[Nsmutabledictionary alloc] initwithobjectsandkeys:@ "Dog ni", @ "name", [NSNumber NUMBERWITHINTEGER:18], @ "age", @ "female", @ "gender", NameArray, @ "candidate", Nil];

NSLog (@ "%@", girl);

Print candidate List

For (NSString *name in [Girl objectforkey:@ "candidate"]) {

NSLog (@ "%@", name);

}

Nsset nsdictionary Nsarray is a collection class

Nsset Immutable Collection Class

1. The element must be an object

2. Unordered

3. Elements are different (not the same)

Nsset *set = [[Nsset alloc] initwithobjects:@ "1 haha", @ "2 Hey", @ "3", @ "4 la la", @ "5 ah ah", @ "6 yes", nil];

NSLog (@ "%@", set);

Get the number of elements

NSLog (@ "%lu", [Set Count]);

Get an element

NSLog (@ "%@", [set Anyobject]);

Get all elements

NSLog (@ "%@", [set allobjects]);

Determines whether an object is contained in the collection

if ([Set containsobject:@ "2 hehe"]) {

NSLog (@ "contains");

}

Sub-collection

Nsset *set1 = [[Nsset alloc] initwithobjects:@ "1", @ "3", @ "4", @ "5", nil];

Nsset *set2 = [[Nsset alloc] initwithobjects:@ "3", @ "4", @ "5", nil];

if ([Set2 Issubsetofset:set1]) {

NSLog (@ "Set2 is a subset of Set1");

}

Nsmutableset, Mutable Collection class, inherited from Nsset

Nsmutableset *mset = [[Nsmutableset alloc] initwithcapacity:0];

adding elements

[MSet addobject:@ "1"];

[MSet addobject:@ "1"];

[MSet addobject:@ "1"];

[MSet addobject:@ "2"];

NSLog (@ "%@", MSet);

Delete Element

[MSet removeobject:@ "1"];

NSLog (@ "%@", MSet);

Delete all elements

[MSet removeallobjects];

NSLog (@ "%@", MSet);

Grammar sugar

1. Create a string

NSString *string = @ "IPhone";

2. Create an array (immutable)

Nsarray *array2 = @[@ "1", @ "2", @ "3"];

Create a dictionary

Nsdictionary *dic2 = @{@ "name": @ "Zhang San", @ "age": @12, @ "gender": @ "male"};

Create a numeric class

NSNumber *number = @12;

Array sorting

Nsmutablearray *array3 = [[Nsmutablearray alloc] initwithobjects:@1, @2, @30, @4, @10, nil];

Bubble sort

BOOL flag = YES;

for (int i = 0; i < [array3 Count]-1 && flag; i++) {

flag = NO;

for (int j = 0; J < [Array3 Count]-1-i; j + +) {

NSNumber *a = [Array3 objectatindex:j];

NSNumber *b = [Array3 objectatindex:j + 1];

if (a > B) {

[Array3 exchangeobjectatindex:j withobjectatindex:j + 1];

flag = YES;

}

}

}

NSLog (@ "%@", array3);

This sort of way is used to sort the names of people (Letters)

[Array3 sortedarrayusingselector: @selector (compare:)];

NSLog (@ "%@", array3);

2-6 Lessonnsdictionaryandnsset

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.