iOS Learning Note---OC language fifth day

Source: Internet
Author: User

Dictionary, array, set sort

First, the Dictionary class

Store the following data

Name: Zhang San; Sex: male; age:18

Film: Storm; playcount:10000 times; price:60 yuan

Dictionary classes are used to hold data that has a mapping relationship (Key-value pairs)

For "Name: Zhang San", Key is "name", key corresponding value is "Zhang San"

A key-value is considered to be an element (entity), and a dictionary is a container for storing key-value pairs.

Characteristics:

Unlike arrays, arrays access data by subscript, and array subscripts are unique.

The dictionary accesses elements by key. Key cannot be duplicated, value must be an object.

Key-value pairs are stored in the dictionary in unordered order.

Nsdictionary Immutable Dictionary

Once the dictionary is created, the key value cannot be changed, cannot be added, cannot be deleted, only the key or value can be read

Create Dictionary//

Nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys:@ ' Lisi ', @ ' name ', @ ' man ', @ ' sex ', @18,@ ' age ', nil];

Nsdictionary *dict = [[Nsdictionary alloc] initwithobjectsandkeys:@ "Lisi", @ "name", @ "man", @ "Sex", @18,@ "age", nil];

Subclass of Nsdictionary

NSLog (@ "%@%@", dict,[dict objectforkey:@ "sex"]);

Variable dictionary nsmutabledictionary is a subclass of nsdictionary that can be used to add, delete, or change the value of a managed key.

/Nsdictionary *dict = [Nsdictionary Dictionarywithobjectsandkeys:@"Lisi",@"name",@"Mans",@"Sex",@ -,@" Age", nil]; Nsdictionary*dict = [[Nsdictionary alloc] Initwithobjectsandkeys:@"Lisi",@"name",@"Mans",@"Sex",@ -,@" Age", nil];//Initializes an immutable dictionaryNSLog (@"%@ %@", Dict,[dict Objectforkey:@"Sex"]); Nsmutabledictionary*mdict = [Nsmutabledictionary dictionarywithcapacity:5];//Initialize a mutable dictionary[Mdict addentriesfromdictionary:dict];//add a dictionary to a mutable dictionary[Mdict SetObject:@"Zhangsan"Forkey:@"name"];//name becomes Zhangsan[Mdict SetObject:@"study"Forkey:@"Hobby"];//Add a Key-value value[Mdict Removeobjectforkey:@"name"];//Remove name from[Mdict removeallobjects];//Remove all key-value pairsNSLog (@"%@ ", mdict);

Traverse Dictionary

   Nsarray *allkeys = [mdict AllKeys];      for (int0; i< [AllKeys Count]; i++) {        *key = [AllKeys objectatindex:i];         = [mdict Objectforkey:key];        NSLog (@ "%@:%@", Key,value);    }

Second, the collection class

In mathematics there are sets of concepts such as Integer set, natural number set, and there are set classes in iOS and mathematical collections corresponding

Features: Like a set in mathematics, stored elements are not the same stored elements are unordered

The storage element must be an object type in iOS with set representation set, Nsset and Nsmutableset

Use the complex number, with the container to save, 90% cases with an array, generally with nsstring

Nsset *ssee = [Nsset setwithobjects:@"Zhangsan",@"Lisi",@"Wangwu",@"Zhaoliu",@"Xiaoer", nil];//Initializing a collection[Ssee Count];//number of collection elementsNSLog (@"%@", [Ssee Anyobject]);//randomly take an elementNSLog (@"%d", [Ssee Containsobject:@"Zhangsan"]);//determine if an element is includedNSLog (@"%@", Ssee);

Nsmutableset mutable set is a subclass of Nsset

Nscountedset is a subclass of Nsmutableset

Ability to record the number of repetitions of an element

Nscountedset *ssee1 = [Nscountedset setwithobjects:@"Zhangsan",@"Lisi",@"Wangwu",@"Zhaoliu",@"Xiaoer",@"Zhaoliu",@"Zhaoliu",@"Zhaoliu", nil]; NSLog (@"%ld", [Ssee1 Countforobject:@"Zhaoliu"]);//4

Added a count function based on set

III. Quick enumeration of collection types

For (< #type *object#> in < #collection #>) {

}

1. Object is the element object to be traversed. 2. Collection is an object of collection type: Array, dictionary, collection

Nsarray *arr1 = [Nsarray arraywithobjects:@" One",@" Both",@"three",@" Four",@"Five", nil];  for(NSString *numincharr1) {NSLog (@"%@", num); }

Nsdictionary *dict1 = [Nsdictionary Dictionarywithobjectsandkeys:@"Zhangsan",@"name",@"Mans",@"Sex",@ -,@" Age",@"study",@"Hobby", nil];  for(NSString *keyinchDict1) {NSLog (@"%@ : %@", Key,[dict1 Objectforkey:key]); }//Enumerate Dictionaries

Nsset *set1 = [Nsset setwithobjects:@"We",@"Me",@" You",@" Her",@"He",@"it", nil];  for(NSString *nameinchSet1) {NSLog (@"%@", name); }//Enumerate Collections

Four, array sorting

The sorting of the groups in OC

Nsmutablearray *arr = [Nsmutablearray arraywithobjects:@"D18",@"A55",@"C23",@"b64",@"E100",@"F80", nil]; [Arr sortusingselector: @selector (compare:)];//Sort by Ascending//for (int i = 0; I<[arr count]-1; i++) {//For (int j = 0; J<[arr count]-i-1; j + +) {//nsstring *str1 = [arr objectatindex:j];//nsstring *str2 = [arr objectatindex:j+1];//            //if ([str1 intvalue]>[str2 intvalue]) {//numbers from small to large sort//                //if ([str1 compare:str2 options:nsnumericsearch]==nsorderedascending) {//numbers from big to small//if ([str1 compare:str2]==nsorderedascending) {//sort from large to small by character type//[arr exchangeobjectatindex:j withobjectatindex:j+1];//            }//        }//    }NSLog (@"%@", arr);

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.