Basic OC framework-dictionary type and basic oc framework dictionary

Source: Internet
Author: User
Tags allkeys

Basic OC framework-dictionary type and basic oc framework dictionary

Category: NSDictionary (unchangeable data dictionary) and NSMutableDictionary (variable data dictionary)

Composition:

(1) A data set composed of Key-Value pairs
(2) Any object (id type) can be used as a key, but it is generally a string
(3) The elements in the dictionary are unordered, and the elements with the same keys cannot be repeated (4) You can use keys to quickly enumerate 1. immutable data dictionary (1) Creation and initialization of the dictionary
1 // 1. Use one or more key-value pairs to initialize a dictionary object: value, creation, value, key ,..., nil sequence 2 NSDictionary * dict01 = [[NSDictionary alloc] initWithObjectsAndKeys: @ "key0", @ "value0", @ "key1", @ "value1", @ "key2 ", @ "value2", nil]; 3 NSLog (@ "dict01: % @", dict01); 4 NSDictionary * dict1 = [NSDictionary dictionaryWithObjectsAndKeys: @ 123, @ "key0 ", @ 456, @ "key1", @ 678, @ "key2", nil]; 5 NSLog (@ "dict1: % @", dict1 ); 6 // 2. initialize a dictionary with two arrays, one key array and one value array. The two arrays must be of the same size; otherwise, Crash 7 NSArray * keyArray = @ [@ "name0 ", @ "name1", @ "name2"]; 8 NSArray * valueArray = @ [@ "hong", @ "wang", @ "zhao"]; 9 NSDictionary * dict02 = [[NSDictionary alloc] keys: keyArray forKeys: valueArray]; 10 NSDictionary * dict2 = [NSDictionary keys: valueArray forKeys: keyArray]; 11 NSLog (@ "dict02: % @ ", dict02); 12 NSLog (@" dict2: % @ ", dict2 ); 13 14 // 3. Use one data dictionary to initialize another. 15 NSDictionary * dict03 = [[NSDictionary alloc] initWithDictionary: dict1]; 16 NSDictionary * dict3 = [NSDictionary dictionaryWithDictionary: dict1]; 17 NSLog (@ "dict03: % @", dict03); 18 NSLog (@ "dict3: % @", dict3 );

 

(2) create and save dictionary objects-Files

// 1. Save the dictionary object content to the file [dict3 writeToFile: @ "/Users/apple/Desktop/tesk.txt" atomically: YES]; // 2. read data from the previously saved file to the dictionary object NSDictionary * dict4 = [NSDictionary dictionaryWithContentsOfFile: @ "/Users/apple/Desktop/tesk.txt"]; NSLog (@ "dict4: % @", dict4 );

(3) access to dictionary object Elements

1 // 1. Obtain all keys and values of the dictionary respectively. 2 NSArray * allkeys = [dict1 allKeys]; 3 NSLog (@ "allkeys: % @", allkeys ); 4 NSArray * allvalues = [dict1 allValues]; 5 NSLog (@ "allvalues: % @", allvalues); 6 7 // 2. Get the corresponding value object through the specified key, without this key. return nil 8 NSNumber * num1 = [dict1 objectForKey: @ "key1"]; 9 NSNumber * num2 = [dict1 objectForKey: @ "key5"]; 10 NSLog (@ "% @", num1); 11 NSLog (@ "% @", num2 ); 12 13 // 3. Obtain the number of key pairs in the dictionary. 14 NSUInteger paircount = [dict2 count]; 15 NSLog (@ "% lu", paircount );

(4) dictionary Traversal

1     for (id key in dict1) {2         id value=[dict1 objectForKey:key];3         NSLog(@"%@=%@",key,value);4     }
21:17:53. 933 NSDictionary [5465: 226991] dict01: {value0 = key0; value1 = key1; value2 = key2;} 21:17:53. 934 NSDictionary [5465: 226991] dict1: {key0 = 123; key1 = 456; key2 = 678;} 21:17:53. 934 NSDictionary [5465: 226991] dict02: {hong = name0; wang = name1; zhao = name2;} 21:17:53. 934 NSDictionary [5465: 226991] dict2: {name0 = hong; name1 = wang; name2 = zhao;} 21:17:53. 935 NSDictionary [5465: 226991] dict03: {key0 = 123; key1 = 456; key2 = 678;} 21:17:53. 935 NSDictionary [5465: 226991] dict3: {key0 = 123; key1 = 456; key2 = 678;} 21:17:53. 937 NSDictionary [5465: 226991] dict4: {key0 = 123; key1 = 456; key2 = 678;} 21:17:53. 938 NSDictionary [5465: 226991] allkeys :( key1, key0, key2) 21:17:53. 938 NSDictionary [5465: 226991] allvalues :( 456,123,678) 21:17:53. 938 NSDictionary [5465: 226991] num1: 4562015-04-07 21:17:53. 938 NSDictionary [5465: 226991] num2 :( null) 21:17:53. 938 NSDictionary [5465: 226991] Number of key-value pairs: 32015-04-07 21:17:53. 938 NSDictionary [5465: 226991] key1 = 4562015-04-07 21:17:53. 938 NSDictionary [5465: 226991] key0 = 1232015-04-07 21:17:53. 938 NSDictionary [5465: 226991] key2 = 678Running result analysis

Ii. variable data dictionary

// Variable data dictionary Initialization

NSMutableDictionary * mDict1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @ "aaa", @ "name0", @ "bbb", @ "name1", @ "ccc", @ "name2 ", nil];

NSLog (@ "% @", mDict1 );

// 1. Add the element to NSMutableDictionary (Note: if the key value in the key-value pair to be added exists in the key value, add it; otherwise overwrite it)

[MDict1 setObject: @ "ddd" forKey: @ "name3"];

[MDict1 setObject: @ "eee" forKey: @ "name1"];

NSLog (@ "add: % @", mDict1 );

// 2. view the number of all elements:

NSLog (@ "% lu", mDict1.count );

NSNumber * num3 = [mDict1 objectForKey: @ "name1"];

NSLog (@ "% @", num3 );

NSNumber * num4 = [mDict1 objectForKey: @ "name10"];

NSLog (@ "% @", num4 );

// 3. Add other NSMutableDictionary

[MDict1 setValuesForKeysWithDictionary: dict1];

NSLog (@ "++ % @", mDict1 );

// 4. Delete based on the key value

[MDict1 removeObjectForKey: @ "name1"];

NSLog (@ "--- % @", mDict1 );

// 5. Delete All

[MDict1 removeAllObjects];

NSLog (@ "% @", mDict1 );

 

 

22:12:10. 717 NSDictionary [5827: 248322] add: {name0 = aaa; name1 = eee; name2 = ccc; name3 = ddd;} 22:12:10. 718 NSDictionary [5827: 248322] 42015-04-07 22:12:10. 718 NSDictionary [5827: 248322] eee2015-04-07 22:12:10. 718 NSDictionary [5827: 248322] (null) 22:12:10. 719 NSDictionary [5827: 248322] ++ {key0 = 123; key1 = 456; key2 = 678; name0 = aaa; name1 = eee; name2 = ccc; name3 = ddd;} 22:12:10. 719 NSDictionary [5827: 248322] --- {key0 = 123; key1 = 456; key2 = 678; name0 = aaa; name2 = ccc; name3 = ddd;} 22:12:10. 719 NSDictionary [5827: 248322] {}Running result analysis

 

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.