Objective-c Dictionary, Variable Dictionary

Source: Internet
Author: User



Dictionaries are equivalent to maps in C + + STL



Dictionary Nsdictionary


1 #import <UIKit / UIKit.h>
  2 #import "AppDelegate.h"
  3
  4 int main (int argc, char * argv []) {
  5 // Create a dictionary from the @ symbol is actually a key-value pair container
  6 NSDictionary * dict = @ {@ "name": @ "zhangsan", @ "sex": @ "male"};
  7 NSLog (@ "% @", dict);
  8     
  9 // get value by key
10 NSLog (@ "% @", [dict objectForKey: @ "name"]);
11
12 // Read from the configuration file
13 NSDictionary * dict1 = [NSDictionary dictionaryWithContentsOfFile:
14 [[NSBundle mainBundle] pathForResource: @ "data" ofType: @ "plist"]];
15 NSLog (@ "% @", [dict1 objectForKey: @ "name"]);
16 NSLog (@ "% @", [dict1 objectForKey: @ "age"]);
17
18 @autoreleasepool {
19 return UIApplicationMain (argc, argv, nil, NSStringFromClass ([AppDelegate class]));
20}
twenty one } 





Variable dictionary nsmutabledictionary


1 NSMutableDictionary * dict2 = [[NSMutableDictionary alloc] init];
2 // setObject can add a key or modify the value of an existing key
3 [dict2 setObject: @ "jinpangpang" forKey: @ "name"];
4 [dict2 setObject: @ "113" forKey: @ "name"];
5 NSLog (@ "% @", [dict2 objectForKey: @ "name"]); 





Objective-c Dictionary, Variable Dictionary


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.