440 likes | 560 Views
Wednesday 2-3:30, DH 1046. COMP 446 / ELEC 446 Mobile Device Applications. Scott Cutler Professor in the Practice of Computer Technology Department of Computer Science Department of Electrical and Computer Engineering cutler@rice.edu 9/25/12. Agenda. Events of the Week Syllabus
E N D
Wednesday 2-3:30, DH 1046 COMP 446 / ELEC 446 Mobile Device Applications Scott Cutler Professor in the Practice of Computer Technology Department of Computer Science Department of Electrical and Computer Engineering cutler@rice.edu 9/25/12
Agenda • Events of the Week • Syllabus • Review of Assignment 1 • Next week’s assignment and class • Some more on Objective C • Questions on Assignment #2 COMP 446 / ELEC 446 - Week 3
Event of the Week COMP 446 / ELEC 446 - Week 3
Events of the Week • HTC has 'no intention' to settle with Apple, say reports • Apple Requests U.S. Sales Ban on Eight Samsung Devices • Apple's Victory vs. Samsung Is Patently Meaningless • Nokia teases September 5 event, says 'things are about to change‘ • Amazon taps Nokia for Kindle Fire maps, report says • Amazon's Appstore expands to five European countries • Kindle Fire 'sold out' as new model looms • Samsung goes all in with Windows 8 in new line of devices • Windows 8, Windows Phone 8 launch dates revealed • Windows Phone 8 Gets a Chance to Shine • Windows Server 2012 launches as cornerstone of Microsoft's 'Cloud OS' • HP, Dell, Lenovo, Others Pack Tablet Market at IFA 2012 • Google, Apple CEOs Talk While Their Patent Battles Rage: Report • Google Street View Brings 'Punch Buggy' Game Online • 'Eastwooding' meme sweeps across Twitter COMP 446 / ELEC 446 - Week 3
Current Roster • Victor Acuna • Ryan Artecona • Gbenga Badipe • Joel Baranowski • Peter Chang • Joan Chao COMP 446 / ELEC 446 - Week 3
Current Roster (2) • Alex Chiu • Heaven Chen • Lingo Dai • Weibo He • SahilHingorani • Zhehao Li COMP 446 / ELEC 446 - Week 3
Current Roster (3) • Abdul Nimeri • Bill Robertson • Frank Salinas • Tyler Siegert • Austin Witt • Matthew Zhao COMP 446 / ELEC 446 - Week 3
Optional Sessions and Support • Monday office hours with TA Richard Latimer. 4:00pm Mudd • Additional session Thursday, 9pm on 8/30 and 9/6. • Ability to email questions to me at any time. • Can email Richard Latimer as well. • Level of support will vary over semester • High level of support at the beginning. • Support decreasing each project as over time as knowing where to look or how to debug is a very important skill for you to learn. • Richard instructed to respond conceptually – not just provide solutions. COMP 446 / ELEC 446 - Week 3
COMP 446 (tentative) Syllabus • Lecture 1 - 8/22/12 - Introduction • Watch before: None • Assignment: #1 - Calculator • Assignment due: Friday, 8/31 6:00 am (Thursday 30:00) • Lecture 2 - 8/29/12- Assignment 1 issues and questions from videos • Watch before: 1, 2 • Assignment: #2 – Function Calculator • Assignment due: Friday, 9/7 6:00 am (Thursday 30:00) • Lecture 3 - 9/05/12 - Assignment 2 issues and questions from videos • Watch before: 3, 4 (demo part) • Assignment: #3 – Graphing Universal Calculator • Assignment due: Tuesday, 9/18 6:00 am (Monday 30:00) • Lecture 4 - 9/12/12 - Assignment 3 issues and questions from videos • Watch before: 4 (post demo), 5, 6 • Assignment: Continue with #3 – Graphing Universal Calculator • Assignment due: Tuesday, 9/18 6:00 am (Monday 30:00) • Lecture 5 - 9/19/12 - Assignment 3 issues and questions from videos • Watch before: Watch 7 before class, 8 after class • Assignment: #4 – Fiickr Top Places • Assignment due: Friday, 9/28 6:00 am (Thursday 30:00) COMP 446 / ELEC 446 - Week 3
COMP 446 (tentative) Syllabus • Lecture 6 - 9/26/12 - Assignment 4 issues and questions from videos • Watch before: 8, 9 very beginning of 10 • Assignment: #5 – Fiickr Map Places • Assignment due: Friday, 10/5 6:00 am (Thursday 30:00) • Lecture 7 - 10/03/12 - Assignment 5 issues and questions from videos • Watch before: 10 (after Tab Bar), 11, 12 • Assignment: 1: #6 – Fiickr Core Data - Likely to be broken into required and optional parts - Optional part required for A+2: Final Project Topic (due before next class) • Assignment due: Friday, 10/12 6:00 am (Thursday 30:00) • Lecture 8 - 10/10/12 - Assignment 6 issues and SQL • Watch before: 13, 14 • Assignment: Final Project Proposal • Assignment due: Tuesday, 10/16 6:00 am (Monday 30:00) • Lecture 9 - 10/17/12 - iOS6 • Watch before: Optional 15, 16 • Assignment: iOS 6 app • Assignment due: Friday, 10/26 6:00 am (Thursday 30:00) COMP 446 / ELEC 446 - Week 3
COMP 446 (tentative) Syllabus • Lecture 10 - 10/24/12 – HTML 5 • Watch before: Optional 17, 18 • Assignment: Major Final Project Feature • Assignment due: Friday, 11/2 6:00 am (Thursday 30:00) • Lecture 11 - 10/31/12 - TBD • Assignment: Continue working on Final Project • Assignment due: Wednesday, 11/28 2:00pm • Lecture 12 - 11/7/12- Android vs. iOS vs. Windows Phone group debate • Assignment: Continue working on Final Project • Assignment due: Wednesday, 11/28 2:00pm • Lecture 13 - 11/14/12 - Individual meetings on Final Project • Assignment: Continue working on Final Project • Assignment due: Wednesday, 11/28 2:00pm • Lecture 14 - 11/21/12 – No class, but continue on final project • Assignment: Continue working on Final Project • Assignment due: Wednesday, 11/28 2:00pm • Lecture 15 - 11/28/12 - Final Presentations • Assignment: Document Final Project • Assignment due: By time assigned for COMP 446 final (there is NO final exam) COMP 446 / ELEC 446 - Week 3
Week 4 • Watch CS193p lectures #4 (after demo), #5, #6 before class • Make progress on CS193p Assignment #3 • Try and complete before next class. Due Tuesday, 9/18 6:00 am . • Submit assignments, review lectures at www.comp446.com • Start thinking about big project • Next week’s lecture – Objective C Core Graphics COMP 446 / ELEC 446 - Week 3
Assignment #1 Comments COMP 446 / ELEC 446 - Week 3
Review of Video 3 Items • Many of the slides to follow come directly from the Fall 2011 Stanford CS193p slide deck. • Some come from earlier versions of CS193p which I think were a bit clearer than the current deck COMP 446 / ELEC 446 - Week 3
All Those Symbols • ; • + • - • { } • [ ] • ( ) • * • ** • : • @ • // • . • # • ! • += • % • == • ^ COMP 446 / ELEC 446 - Week 3
strong vs weak • strong “keep this in the heap until I don’t point to it anymore” • I won’t point to it anymore if I set my pointer to it to nil. • Or if I myself am removed from the heap because no one strongly points to me! • weak “keep this as long as someone else points to it strongly” • If it gets thrown out of the heap, set my pointer to it to nil automatically (if user on iOS 5 only). • This is not garbage collection! • It’s way better. It’s reference counting done automatically for you. • Finding out that you are about to leave the heap • A special method, dealloc, is called on you when your instance’s memory is freed from the heap. • You will rarely ever have to implement this method. It’s “too late” to do much useful here. - (void)dealloc { [[NSNotificationCenterdefaultCenter] removeObserver:self]; [super dealloc]; } COMP 446 / ELEC 446 - Week 3
nil • The value of an object pointer that does not point to anything id obj = nil; NSString *hello = nil; • Like “zero” for a primitive type (int, double, etc.) • Actually, it’s not “like” zero: it is zero. • All instance variables start out set to zero • Thus, instance variables that are pointers to objects start out with the value of nil. • Can be implicitly tested in an if statement if (obj) { } // curly braces will execute if objpoints to an object • Sending messages to nil is (mostly) okay. No code gets executed. • If the method returns a value, it will return zero. int i = [objmethodWhichReturnsAnInt]; // i will be zero if objis nil • Be careful if the method returns a C struct. Return value is undefined. CGPointp = [objgetLocation]; // p will have an undefined value if objis nil COMP 446 / ELEC 446 - Week 3
BOOL • Objective-C’s boolean “type” (actually just a typedef) • Can be tested implicitly if (flag) { } if (!flag) { } YES means “true,” NO means “false” NO == 0, YES is anything else if (flag == YES) { } if (flag == NO) { } if (flag != NO) { } COMP 446 / ELEC 446 - Week 3
Instance vs. Class Methods • Starts with a dash - (BOOL)dropBomb:(Bomb *)bomb at: (CGPoint)position from:(double)altitude; • “Normal” Instance Methods Calling syntax [<pointer to instance> method] Ship *ship = ...; // instance of a Ship destroyed = [ship dropBomb:firecracker at:dropPoint from:300.0]; • self / superis calling instance self means “my implementation” super means “my superclass’s implementation” • Starts with a plus sign + (id) alloc; + (Ship *)motherShip; + (NSString *)stringWithFormat:... • Creation & Utility Methods Calling syntax [Class method] Ship *ship = [Ship motherShip]; NSString *resultString = [NSStringstringWithFormat:@“%g”, result]; [[ship class] doSomething]; • self / superis this class self means “this class’s class methods” super means “this class’s superclass’s class methods” COMP 446 / ELEC 446 - Week 3
Instantiation • Asking other objects to create objects for you NSString’s- (NSString *)stringByAppendingString:(NSString *)otherString; NSString’s & NSArray’s- (id)mutableCopy; NSArray’s- (NSString *)componentsJoinedByString:(NSString *)separator; • Not all objects handed out by other objects are newly created NSArray’s- (id)lastObject; NSArray’s- (id)objectAtIndex:(int)index; Unless the method has the word “copy” in it, if the object already exists, you get a pointer to it. If the object does not already exist (like the 3 examples above), then you’re creating. • Using class methods to create objects NSString’s+ (id)stringWithFormat:(NSString *)format, ... UIButton’s+ (id)buttonWithType:(UIButtonType)buttonType; NSMutableArray’s+ (id)arrayWithCapacity:(int)count; NSArray’s+ (id)arrayWithObject:(id)anObject; COMP 446 / ELEC 446 - Week 3
Object Typing @interface Vehicle - (void)move; @end @interface Ship : Vehicle - (void)shoot; @end Ship *s = [[Ship alloc] init]; [s shoot]; [s move]; Vehicle *v = s; [v shoot]; id obj = ...; [obj shoot]; [objsomeMethodNameThatNoObjectAnywhereRespondsTo]; NSString*hello = @”hello”; [hello shoot]; Ship *helloShip = (Ship *)hello; [helloShip shoot]; COMP 446 / ELEC 446 - Week 3
Introspection • All objects that inherit from NSObject know these methods • isKindOfClass: returns whether an object is that kind of class (inheritance included) • isMemberOfClass: returns whether an object is that kind of class (no inheritance) • respondsToSelector: returns whether an object responds to a given method • Method testing methods take a selector (SEL) • Special @selector() directive turns the name of a method into a selector if ([objrespondsToSelector:@selector(shoot)]) { [obj shoot]; } else if ([objrespondsToSelector:@selector(shootAt:)]) { [objshootAt:target]; } • SEL is the Objective-C “type” for a selector SEL shootSelector = @selector(shoot); SEL shootAtSelector = @selector(shootAt:); SEL moveToSelector = @selector(moveTo:withPenColor:); COMP 446 / ELEC 446 - Week 3
Frameworks • A collection of pre-written and debugged code • Foundation contains base class for much in iOS SDK • MANY other frameworks available; some of which you will use for later assignments and your final project. • Frameworks help make difficult things amazingly easy COMP 446 / ELEC 446 - Week 3
Foundation Framework • Value and collection classes • User defaults • Archiving • Notifications • Undo manager • Tasks, timers, threads • File system, pipes, I/O, bundles COMP 446 / ELEC 446 - Week 3
NSObject • Root class • Implements many basics • Memory management • Introspection • Object equality COMP 446 / ELEC 446 - Week 3
NSString • General-purpose Unicode string support • Unicode is a coding system which represents all of the world’s languages • Consistently used throughout Cocoa Touch instead of “char *” • Without doubt the most commonly used class • Easy to support any language in the world with Cocoa COMP 446 / ELEC 446 - Week 3
String Constants • In C constant strings are “simple” • In ObjC, constant strings are @“just as simple” • Constant strings are NSString instances NSString*aString = @”Hello World!”; COMP 446 / ELEC 446 - Week 3
Format Strings • Similar to printf, but with %@ added for objects NSString *aString = @”Johnny”; NSString*log =[NSStringstringWithFormat: @”It’s ‘%@’”, aString]; log would be set to It’s ‘Johnny’ • Also used for logging NSLog(@”I am a %@, I have %d items”, [array className], [array count]); would log something like: I am a NSArray, I have 5 items COMP 446 / ELEC 446 - Week 3
NSString • Often ask an existing string for a new string with modifications - (NSString *)stringByAppendingString:(NSString *)string; - (NSString *)stringByAppendingFormat:(NSString *)string; - (NSString *)stringByDeletingPathComponent; • Example: NSString *myString = @”Hello”; NSString *fullString; fullString= [myStringstringByAppendingString:@” world!”]; fullString would be set to Hello world! COMP 446 / ELEC 446 - Week 3
NSString • Common NSString methods - (BOOL)isEqualToString:(NSString *)string; - (BOOL)hasPrefix:(NSString *)string; - (int)intValue; - (double)doubleValue; • Example: NSString *myString = @”Hello”; NSString *otherString = @”449”; if ([myStringhasPrefix:@”He”]) { // will make it here } if ([otherStringintValue] > 500) { // won’t make it here } COMP 446 / ELEC 446 - Week 3
NSMutableString • NSMutableString subclasses NSString • Allows a string to be modified • Common NSMutableString methods + (id)string; - (void)appendString:(NSString *)string; - (void)appendFormat:(NSString *)format, ...; NSMutableString *newString = [NSMutableString string]; [newStringappendString:@”Hi”]; [newStringappendFormat:@”, my favorite number is: %d”, [self favoriteNumber]]; COMP 446 / ELEC 446 - Week 3
Collections • Array - ordered collection of objects • Dictionary- collection of key-value pairs • Set- unordered collection of unique objects • Ordered Set- ordered collection of unique objects • Common enumeration mechanism • Immutable and mutable versions • Immutable collections can be shared without side effect • Prevents unexpected changes • Mutable objects typically carry a performance overhead COMP 446 / ELEC 446 - Week 3
NSArray • Common NSArray methods + arrayWithObjects:(id)firstObj, ...; // nil terminated!!! - (unsigned)count; - (id)objectAtIndex:(unsigned)index; - (unsigned)indexOfObject:(id)object; • NSNotFoundreturned for index if not found NSArray *array = [NSArrayarrayWithObjects:@”Red”, @”Blue”, @”Green”, nil]; if ([array indexOfObject:@”Purple”] == NSNotFound) { NSLog (@”No color purple”); } • Be careful of the nil termination!!! COMP 446 / ELEC 446 - Week 3
NSMutableArray • NSMutableArray subclasses NSArray • So, everything in NSArray • Common NSMutableArrayMethods + (NSMutableArray *)array; - (void)addObject:(id)object; - (void)removeObject:(id)object; - (void)removeAllObjects; - (void)insertObject:(id)object atIndex:(unsigned)index; NSMutableArray *array = [NSMutableArray array]; [array addObject:@”Red”]; [array addObject:@”Green”]; [array addObject:@”Blue”]; [array removeObjectAtIndex:1]; COMP 446 / ELEC 446 - Week 3
NSDictionary • Common NSDictionary methods + dictionaryWithObjectsAndKeys: (id)firstObject, ...; - (unsigned)count; - (id)objectForKey:(id)key; • nil returned if no object found for given key NSDictionary *colors = [NSDictionary dictionaryWithObjectsAndKeys:@”Red”, @”Color 1”, @”Green”, @”Color 2”, @”Blue”, @”Color 3”, nil]; NSString *firstColor = [colors objectForKey:@”Color 1”]; if ([colors objectForKey:@”Color 8”]) { // won’t make it here } COMP 446 / ELEC 446 - Week 3
NSMutableDictionary • NSMutableDictionary subclasses NSDictionary • Common NSMutableDictionary methods + (NSMutableDictionary *)dictionary; - (void)setObject:(id)object forKey:(id)key; - (void)removeObjectForKey:(id)key; - (void)removeAllObjects; NSMutableDictionary *colors = [NSMutableDictionary dictionary]; [colorssetObject:@”Orange” forKey:@”HighlightColor”]; COMP 446 / ELEC 446 - Week 3
NSSet • Unordered collection of objects • Common NSSet methods + setWithObjects:(id)firstObj, ...; // nil terminated - (unsigned)count; - (BOOL)containsObject:(id)object; • NSOrderedSet • Faster searching • Does not inherit from NSSet COMP 446 / ELEC 446 - Week 3
NSMutableSet • NSMutableSet subclasses NSSet • Common NSMutableSet methods + (NSMutableSet *)set; - (void)addObject:(id)object; - (void)removeObject:(id)object; - (void)removeAllObjects; - (void)intersectSet:(NSSet *)otherSet; - (void)minusSet:(NSSet *)otherSet; COMP 446 / ELEC 446 - Week 3
Enumeration • Consistent way of enumerating over objects in collections • Use with NSArray, NSDictionary, NSSet, etc. NSArray *array = ... ; // assume an array of People objects // old school Person *person; int count = [array count]; for (i = 0; i < count; i++) { person = [array objectAtIndex:i]; NSLog([person description]); } // new school for (Person *person in array) { NSLog([person description]); } COMP 446 / ELEC 446 - Week 3
NSNumber • In Objective-C, you typically use standard C number types • NSNumberis used to wrap C number types as objects • Subclass of NSValue • No mutable equivalent! • Common NSNumber methods + (NSNumber *)numberWithInt:(int)value; + (NSNumber *)numberWithDouble:(double)value; - (int)intValue; - (double)doubleValue; COMP 446 / ELEC 446 - Week 3
Other Classes • NSData / NSMutableData • Arbitrary sets of bytes • NSDate/ NSCalendarDate • Times and dates • NSValue • Object store for object pointers and other values like C structures, int, float, char, etc. COMP 446 / ELEC 446 - Week 3
Property List • The term “Property List” just means a collection of collections Specifically, it is any graph of objects containing only the following classes: NSArray, NSDictionary, NSNumber, NSString, NSDate, NSData • An NSArrayis a Property List if all its members are too So an NSArrayof NSStringis a Property List. So is an NSArrayof NSArrayas long as those NSArray’smembers are Property Lists. • An NSDictionaryis one only if all keys and values are too An NSArrayof NSDictionaryswhose keys are NSStrings and values are NSNumbers is one. • Why define this term? Because the SDK has a number of methods which operate on Property Lists. Usually to read them from somewhere or write them out to somewhere. [plistwriteToFile:(NSString *)path atomically:(BOOL)]; // plistis NSArrayor NSDictionary COMP 446 / ELEC 446 - Week 3
NSUserDefaults • Lightweight storage of Property Lists. • It’s basically an NSDictionarythat persists between launches of your application. • Not a full-on database, so only store small things like user preferences. • Read and write via a shared instance obtained via class method standardUserDefaults [[NSUserDefaultsstandardUserDefaults] setArray:rvArrayforKey:@“RecentlyViewed”]; • Sample methods: - (void)setDouble:(double)aDoubleforKey:(NSString *)key; - (NSInteger)integerForKey:(NSString *)key; // NSIntegeris a typedefto 32 or 64 bit int - (void)setObject:(id)objforKey:(NSString *)key; // objmust be a Property List - (NSArray *)arrayForKey:(NSString *)key; // will return nil if value for key is not NSArray • Always remember to write the defaults out after each batch of changes! [[NSUserDefaultsstandardUserDefaults] synchronize]; COMP 446 / ELEC 446 - Week 3
Assignment #2 Questions COMP 446 / ELEC 446 - Week 3