Objective-c dictionary object, objective-c dictionary

Source: Internet
Author: User
Tags allkeys

Objective-c dictionary object, objective-c dictionary

The NSDictionary in oc serves the same purpose as the dictionary class in java and provides a combination of "key-value" pairs. For example, a dictionary class is used to store the Student name and student ID. The number is a key (unique) and the name is a value. Its methods include:

  

 

The following uses an example to describe its usage:

1) Build a dictionary

  

 1 #import <Foundation/Foundation.h> 2  3 int main(int argc , const char *argv[]){ 4     @autoreleasepool{ 5         NSDictionary *dicti1 = [NSDictionary     dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil]; 6         NSEnumerator *keynum = [dicti1 keyEnumerator]; 7         for(NSString *key in keynum) 8             NSLog(@"key = %@ , value = %@",key,[dicti1 objectForKey:key]); 9         10         NSLog(@"----------------------------");11         NSDictionary *dicti2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil];12         keynum = [dicti2 keyEnumerator];13         for(NSString *key in keynum)14             NSLog(@"key = %@ , value = %@",key,[dicti2 objectForKey:key]);15         16         NSLog(@"----------------------------");17         NSDictionary *dicti3 = @{@"1":@"zhangsan",@"2":@"lisi",@"3":@"wangwu"};18         keynum = [dicti3 keyEnumerator];19         for(NSString *key in keynum)20             NSLog(@"key = %@  , value = %@",key,[dicti3 objectForKey:key]);21         22         NSLog(@"------------------------------");23         NSArray *keyarray = @[@"1",@"2",@"3"];24         NSArray *valuearray = @[@"zhangsan",@"lisi",@"wangwu"];25         NSDictionary *dicti4 = [NSDictionary dictionaryWithObjects:valuearray forKeys:keyarray];26         keynum = [dicti4 keyEnumerator];27         for(NSString *key in keynum)28             NSLog(@"key = %@ , value = %@",key,[dicti4 objectForKey:key]);29         30     }31     return 0;32 }

2) dictionary Traversal

  

 1 #import <Foundation/Foundation.h> 2  3 int main(int argc , const char *argv[]){ 4     @autoreleasepool { 5         NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil]; 6         NSEnumerator *number = [dicti keyEnumerator]; 7         for(NSString *key in number) 8             NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]); 9         10         NSLog(@"----------------------------");11         NSArray *keyarray = [dicti allKeys];12         for(NSString *key in keyarray)13             NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]);14         15         NSLog(@"-----------------------------");16         for(NSString *key in  dicti)17             NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]);18         19         NSLog(@"-----------------------------");20         [dicti enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {21             NSLog(@"key = %@  ,value = %@",key,obj);22         }];23     }
    return 0;24 }

3) File Reading

 1 #import <Foundation/Foundation.h> 2  3 int main(int argc , const char *argv[]){ 4     @autoreleasepool { 5         NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangnwu",@"3",nil]; 6          7         NSString *filename = @"dicti.txt"; 8         BOOL iswrite = [dicti writeToFile:filename atomically:YES]; 9         if(iswrite)10             NSLog(@"write to file ok");11         else12             NSLog(@"write to file error");13         14         NSDictionary *dicti2 = [NSDictionary dictionaryWithContentsOfFile:filename];15         NSLog(@"dicti2 = %@",dicti2);16         17     }
    return 0;18 }

4) Search

  

1 # import <Foundation/Foundation. h> 2 3 int main (int argc, const char * argv []) 4 {5 @ autoreleasepool {6 NSDictionary * dicti = [NSDictionary dictionaryWithObjectsAndKeys: @ "zhangsan ", @ "1", @ "lisi", @ "2", @ "wangwu", @ "3", @ "zhangsan", @ "4", nil]; 7 8 NSArray * keyarray = [dicti allKeys]; // search for all key values 9 NSLog (@ "% @", keyarray); 10 11 NSArray * valuearray = [dicti allValues]; // query all values 12 NSLog (@ "% @", valuearray); 13 14 NSString * value = [dicti objectForKey: @ "1"]; // search for 15 NSLog (@ "value = % @", value); 16 17 value = dicti [@ "2"]; 18 NSLog (@ "value = % @", value); 19 20 NSArray * keyarray2 = @ [@ "1", @ "2", @ "3"]; // search for 21 NSArray * vlauearray2 = [dicti objectsForKeys: keyarray2 notFoundMarker: @ "nil"]; 22 NSLog (@ "vlauearray2 = % @", vlauearray2 ); 23 24 NSArray * keyarray3 = [dicti allKeysForObject: @ "zhangsan"]; 25 NSLog (@ "keyarray3 = % @", keyarray3); 26 27 NSDictionary * dicti2 = [dicti keywords: keyarray2]; 28 NSLog (@ "dicti2 = % @", dicti2); 29} 30 return 0; 31}

5) sort

  

 1 #import <Foundation/Foundation.h> 2  3  4 int main(int argc,char **argv){ 5     @autoreleasepool { 6         NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"2",@"lisi",@"3",@"wangwu",@"1",@"zhangsan",@"4",nil]; 7         NSLog(@"dicti = %@",dicti); 8          9         NSDictionary *dicti2 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@1,@"lisi",@2,@"wangu",@3, nil];10         NSLog(@"dicti2 = %@",dicti2);11         12         NSArray *array = [dicti2 keysSortedByValueUsingSelector:@selector(compare:)];13         for(NSNumber *key in array)14             NSLog(@"key = %@,value = %@",key,dicti2[key]);15     }16     return 0;17 }

2. A variable Dictionary (NSDictionary) exists in the same dictionary object. Here we will illustrate its usage through an example:

  

 

Code example:

  

1 # import <Foundation/Foundation. h> 2 3 4 int main (int argc, char ** argv) {5 @ autoreleasepool {6 NSMutableDictionary * mdicti = [NSMutableDictionary Syntax: @ "zhangsan", @ "1 ", @ "lisi", @ "2", @ "wangwu", @ "3", nil]; 7 8 [mdicti setValue: @ "zhaoliu" forKey: @ "4"]; // if the key does not exist, add it. If the key exists, modify 9 NSLog (@ "mdicti = % @", mdicti); 10 11 [mdicti setValue: @ "zzz" forKey: @ "1"]; 12 NSLog (@ "mdicti = % @", mdicti ); 13 14 mdicti [@ "5"] = @ "bbb"; 15 NSLog (@ "mdicti = % @", mdicti); 16 17 NSDictionary * dicti = [NSDictionary dictionaryWithObjectsAndKeys: @ "aaa", @ "11", @ "bbb", @ "12", @ "ccc", @ "13", nil]; 18 [mdicti addEntriesFromDictionary: dicti]; 19 NSLog (@ "dicti = % @", mdicti); 20 21 [mdicti removeObjectForKey: @ "1"]; // Delete 22 NSLog (@ "mdicti = % @", mdicti); 23 24 NSArray * array = @ [@ "11", @ "12 ", @ "13"]; 25 [mdicti removeObjectsForKeys: array]; 26 NSLog (@ "mdicti = % @", mdicti); 27 28 [mdicti removeAllObjects]; // delete all 29 nslogs (@ "% @", mdicti); 30} 31 return 0; 32}

 

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.