13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3: Foundational APIs 59Remember that sets do not allow duplicate object references, so any objects that were alreadyin the set and are added to a mutable set will still only appear once in the set.Developers with a background in mathematics may find sets interesting; sets in the Foundationframework behave similarly to sets in mathematics. This brings us to the final methods forchanging the contents of a mutable set, shown in Table 3-6.Table 3-6. Mutable Set MethodsSet OperationIntersection of two setsDifference of two setsUnion of two setsInstance MethodinsertsectSet:minusSet:unionSet:Listing 3-29 demonstrates the power of mutable sets; while sets are not found in many APIs,they are an efficient and useful data structure to keep in your <strong>Objective</strong>-C toolbox.Listing 3-29. Mutable SetsNSMutableSet *mutableSet = [NSMutableSet setWithObjects:@"apple", @"bananas", @"milk", nil];[mutableSet intersectSet:[NSSet setWithObject:@"apple"]];//mutableSet contains only @"apple" now[mutableSet unionSet:[NSSet setWithObjects:@"bread", @"jam", @"apple", nil];//mutableSet now contains @"apple", @"bread", and @"jam"[mutableSet minusSet:[NSSet setWithObject:@"bread"]];// mutableSet now contains @"apple" and @"jam"[mutableSet minusSet:[NSSet setWithObject:@"pasta"]];// mutableSet is unchanged; it still contains @"apple" and @"jam"DictionariesThe final member of the collections library in Foundation is NSDictionary. A dictionary is a listof key-value pairs. Keys are typically NSString instances and the values are id. Dictionaries areoften used to pass a variable number of optional parameters to an API, such as Core Text. Theyare also used heavily with NSNotification and NSError.Keys within a dictionary must be unique. Creating a dictionary with two identical keys will causea runtime error.Creating a dictionary is possible with both the allocate-and-initialize and convenience classmethods. Table 3-7 details the common dictionary creation methods, although this is not acomplete list.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!