OBJECTIVE-C Foundation Framework Practice--nsdictionary (i) __foundation

Source: Internet
Author: User

A dictionary is a special type of OC, and its function is very powerful. is an important part of foundation. Let's Learn:

#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) {@autoreleasepool {//INSERT Co De here .../* Dictionary: 1. Memory is not contiguous; 2. Use key and value to correspond (key value); 3.KVC: Key-value code;//Create Party
    Type 1; nsdictionary *dict = [nsdictionary dictionarywithobject:@ "1" forkey:@ "a"];
    
    NSLog (@ "%@", dict);
    Create way 2:nsarray *arrvalue = [[Nsarray alloc] initwithobjects:@ "1", @ "2", @ "3", @ "4", nil];
    Nsarray *arrkeys = [[Nsarray alloc] initwithobjects:@ "a", @ "B", @ "C", @ "D", nil];
    Nsdictionary *dict1 = [nsdictionary dictionarywithobjects:arrvalue Forkeys:arrkeys];
    
   
    
    NSLog (@ "%@", Dict1);
    Create Way 3: Simple to create a way;//@{}; a dictionary;!!!!!
    Nsdictionary *dict3 = @{@ "1": @ "a", @ "2": @ "B"};
    
    
    NSLog (@ "%@", dict3);
    Length: int count = (int) dict3.count;
    
    
    
    NSLog (@ "Count =%d", count);
    Gets the value according to the key nsstring *str = [Dict3 objectforkey:@ "2"];
    NSLog (@ "Key value =%@", str); You can also use the following method: NSString *str2 = [Dict3 valueforkey:@ "2"];
    
    
    NSLog (@ "Key value =%@", str2);
    Gets all the values in the dictionary; (value) Nsarray *ARR5 = [[Nsarray alloc] initwithobjects:@ "1", @ "2", @ "3", nil];
    Nsarray *arrallvalue = [Dict3 objectsforkeys:arr5 notfoundmarker:@ "None"];
    
    NSLog (@ "All values Method 1 =%@", arrallvalue);
    There is also a way to get all the values; Nsarray *arrallvalue2 = [Dict3 allvalues];
    
    
    NSLog (@ "All values Method 2 =%@", arrAllValue2);
    Gets all the keys in the dictionary; (key) Nsarray *arrallkey = [Dict3 AllKeys];
    
    
    NSLog (@ "All keys =%@", Arrallkey);
      Traversal array;//need to use key to traverse; for (NSString *key in dict3) {nsstring *value = [Dict3 Objectforkey:key];
      
    NSLog (@ "key =%@,value =%@", key,value);
    ///using iterators for traversal; nsenumerator *en = [Dict3 keyenumerator];
    ID key = nil;
      while (key = [en nextobject]) {nsstring *str = [Dict3 Objectforkey:key];
      
    NSLog (@ "iterator gets value =%@", str);
} return 0; }

The output results are as follows:

.


GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.

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.