`
cenphoenix
  • 浏览: 161568 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

How to Use Objective-C Dictionaries

阅读更多

How to Use Dictionaries

Like an array, a dictionary is a list of objects. However, dictionaries make it
easier to organize data by providing “keys” that make it a snap to find the
things you put in there. These are sometimes referred to as a hash or a
hash-table.

Instantiate a dictionary

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; 
Add objects to a dictionary indexed by keys

[dictionary setObject:@"A Book about the Letter A" forKey:@"A"];
[dictionary setObject:@"A Book about the Letter B" forKey:@"B"];
[dictionary setObject:@"A Book about the Letter C" forKey:@"C"];

Retrieve an object from a dictionary with a key

NSLog([dictionary objectForKey:@"B"]);
Release a dictionary

[dictionary release]; 

Cool – hope you enjoyed this snippet of da codes! Once you get use to the square brackets and verbose parameter prefixes Objective-C starts to feel like the programming languages that you are used to.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics