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.
分享到:
相关推荐
Pitre gives you a solid grounding in key Swift language concepts—including variables, constants, types, arrays, and dictionaries—before he shows you how to use Swift’s innovative Xcode integrated ...
Enter the Swift future of ...Learn how to use generics to create highly reusable code Learn the new access controls mechanism in Swift Get up to speed quickly to remain relevant and ahead of the curve.
Find out how Swift communicates with Cocoa’s C and Objective-C APIs Once you master the fundamentals, you'll be ready to tackle the details of iOS app development with author Matt Neuburg's companion...
• Find out how Swift communicates with Cocoa s C and Objective-C APIs Once you master the fundamentals, you ll be ready to tackle the details of iOS app development with author Matt Neuburg s ...
If you're self-taught programmers in any language who wants to gain a solid understanding of data structures and how to use them to solve real-world problems in your day-to-day development work, then ...