DOC PREVIEW
SDSU CS 696 - Collection Classes

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 696 Mobile Application DevelopmentFall Semester, 2010Doc 5 Collection ClassesSep 9, 2010Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.References2The Objective-C 2.0 Programming Language, http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163Collections Programming Topics for Core Foundation, http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFCollections/CFCollections.htmlCollections Programming Topics Data Management: Data Types & Collections, http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Collections.html%23//apple_ref/doc/uid/10000034-BBCFIHFHFoundation Classes3Base Types and Objects4 NSNumber * example = [NSNumber numberWithInt:10]; NSLog(@"int %i", [example intValue]); NSLog(@"float %f", [example floatValue]); example = [NSNumber numberWithFloat:5.632]; NSLog(@"int %i", [example intValue]); NSLog(@"float %f", [example floatValue]);Some NSNumber Methods5 + numberWithBool: + numberWithChar: + numberWithDouble: + numberWithFloat: + numberWithInt: + numberWithInteger: + numberWithLong: + numberWithLongLong: + numberWithShort: + numberWithUnsignedChar: + numberWithUnsignedInt: + numberWithUnsignedInteger: + numberWithUnsignedLong: + numberWithUnsignedLongLong: + numberWithUnsignedShort: – boolValue – charValue – decimalValue – doubleValue – floatValue – intValue – integerValue – longLongValue – longValue – shortValue – unsignedCharValue – unsignedIntegerValue – unsignedIntValue – unsignedLongLongValue – unsignedLongValue – unsignedShortValueMutable & Immutable Collection Classes6Mutable versions are subsets of immutable versionImmutableMutableNSStringNSMutableStringNSArrayNSMutableArrayNSDictionaryNSMutableDictionaryNSIndexSetNSMutableIndexSetNSSetNSMutableSetNSCountedSetNSArray7 NSArray *sample; NSDate *aDate = [NSDate date]; NSValue *aValue = [[NSNumber alloc] initWithInt:5]; NSString *aString = @"a string"; sample = [NSArray arrayWithObjects: aDate, aValue, aString, nil]; [aValue release]; NSDate * result =[sample objectAtIndex: 0]; NSLog(@"date: %@", result); NSInteger size = [sample count];NSArray8Index starts at zeroOnly holds objectsCollections retain objects when addedEnumerating9 NSEnumerator *enumerator = [sample objectEnumerator]; id anObject; while (anObject = [enumerator nextObject]) { NSLog(@"object:%@", anObject); } //Fast enumeration for (id element in sample){ NSLog(@"object:%@", element); } NSEnumerator *reverse = [sample reverseObjectEnumerator]; for (id element in reverse){ NSLog(@"object:%@", element); }Note the pointer10 sample = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; for (NSString * element in sample){ NSLog(@"object:%@", element); }Yes you need the nil11[NSArray arrayWithObjects:@"a", @"b", @"c"]; // Runtime errorEnumerating with Blocks12NSString *findMe=@"c";[anArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){ if([obj localizedCaseInsensitiveCompare: findMe] == NSOrderedSame){NSLog(@"Object Found: %@ at index: %i",obj, index); *stop=YES;}}];From Collections Programming TopicsFiltering & Predicates13 NSArray * words = [NSArray arrayWithObjects: @"dog", @"cat", @"mat", nil]; NSPredicate *filterForA = [NSPredicate predicateWithFormat:@"SELF contains[c] 'a'"]; NSArray * containsA = [words filteredArrayUsingPredicate: filterForA]; //containsA == (cat, mat) NSValue *small = [NSNumber numberWithInt:1]; NSValue *medium = [NSNumber numberWithInt:10]; NSValue *large = [NSNumber numberWithInt:100]; NSArray * numbers = [NSArray arrayWithObjects: large, small, medium, nil]; NSPredicate *range = [NSPredicate predicateWithFormat:@"SELF > 5 AND SELF < 20"]; NSArray * result = [numbers filteredArrayUsingPredicate: range]; // result == (10)For more information see "Introduction to Predicates Programming Guide"Person & Name class for examples14@interface Person : NSObject { }@property (retain) Name* name;@property int age;+ (Person*) first: (NSString *) firstName last: (NSString *) lastName age: (int) age;@end@interface Name : NSObject { }@property (copy) NSString* first;@property (copy) NSString* last;+ (Name*) first: (NSString*) first last: (NSString*) last;Sorting with Selectors15 Person* joe = [Person first: @"Joe" last: @"Spade" age: 10]; Person* sally = [Person first: @"Sally" last: @"Adam" age: 5]; Person* shah = [Person first: @"Shah" last: @"Chen" age: 8]; NSArray* people = [NSArray arrayWithObjects:joe,sally,shah,nil]; NSArray * sorted = [people sortedArrayUsingSelector:@selector(age)];Sorting with Functions16 Person* joe = [Person first: @"Joe" last: @"Spade" age: 10]; Person* sally = [Person first: @"Sally" last: @"Adam" age: 5]; Person* shah = [Person first: @"Shah" last: @"Chen" age: 8]; NSArray* people = [NSArray arrayWithObjects:joe,sally,shah,nil]; NSArray * sorted = [people sortedArrayUsingFunction:ageSort context:NULL];NSInteger ageSort(id personA, id personB, void *context) { int ageA = [personA age]; int ageB = [personB age]; if (ageA < ageB) return NSOrderedAscending; else if (ageA > ageB) return NSOrderedDescending; else return NSOrderedSame;}Key-Value Coding17 Person* joe = [Person first: @"Joe" last: @"Spade" age: 10]; Person* sally = [Person first: @"Sally" last: @"Adam" age: 5]; Person* shah = [Person first: @"Shah" last: @"Chen" age: 8]; NSArray* people = [NSArray arrayWithObjects:joe,sally,shah,nil]; NSNumber * sum = [list valueForKeyPath:@"@sum.age"]; NSNumber * max = [list valueForKeyPath:@"@max.age"]; NSArray * firstNames = [list valueForKeyPath:@"@unionOfObjects.name.first"];NSMutableArray18 NSMutableArray * names = [NSMutableArray arrayWithCapacity:3]; [names addObject:@"Joe"]; [names addObject:@"Shah"]; [names addObject:@"Nguyen"]; NSLog(@"names:%@", names); NSArray * one = [NSArray arrayWithObject:@"Joe"]; NSArray * two = [one arrayByAddingObject:@"Shah"]; NSArray * three = [two arrayByAddingObject:@"Nguyen"]; NSLog(@"three:%@",


View Full Document

SDSU CS 696 - Collection Classes

Download Collection Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Collection Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Collection Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?