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 Wednesday 2-3:30, DH 1046 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 SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 2 Event of the Week SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 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 SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 4 Current Roster Victor Acuna Ryan Artecona Gbenga Badipe Joel Baranowski Peter Chang Joan Chao SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 5 Current Roster (2) Alex Chiu Heaven Chen Lingo Dai Weibo He Sahil Hingorani Zhehao Li SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 6 Current Roster (3) Abdul Nimeri Bill Robertson Frank Salinas Tyler Siegert Austin Witt Matthew Zhao SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 7 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 SEC - 9/5/12 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 8 COMP 446 (tentative) Syllabus Lecture 1 - 8/22/12 - Introduction 1, 2 #2 – Function Calculator Friday, 9/7 6:00 am (Thursday 30:00) Watch before: Assignment: Assignment due: 3, 4 (demo part) #3 – Graphing Universal Calculator Tuesday, 9/18 6:00 am (Monday 30:00) Lecture 4 - 9/12/12 - Assignment 3 issues and questions from videos Watch before: Assignment: Assignment due: Lecture 3 - 9/05/12 - Assignment 2 issues and questions from videos None #1 - Calculator Friday, 8/31 6:00 am (Thursday 30:00) Lecture 2 - 8/29/12- Assignment 1 issues and questions from videos Watch before: Assignment: Assignment due: Watch before: Assignment: Assignment due: 4 (post demo), 5, 6 Continue with #3 – Graphing Universal Calculator Tuesday, 9/18 6:00 am (Monday 30:00) Lecture 5 - 9/19/12 - Assignment 3 issues and questions from videos Watch before: Assignment: Assignment due: SEC - 9/5/12 Watch 7 before class, 8 after class #4 – Fiickr Top Places Friday, 9/28 6:00 am (Thursday 30:00) COMP 446 / ELEC 446 - Week 3 9 COMP 446 (tentative) Syllabus Lecture 6 - 9/26/12 - Assignment 4 issues and questions from videos Watch before: Assignment: Assignment due: 10 (after Tab Bar), 11, 12 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) Friday, 10/12 6:00 am (Thursday 30:00) Lecture 8 - 10/10/12 - Assignment 6 issues and SQL 8, 9 very beginning of 10 #5 – Fiickr Map Places Friday, 10/5 6:00 am (Thursday 30:00) Lecture 7 - 10/03/12 - Assignment 5 issues and questions from videos Watch before: Assignment: Assignment due: Watch before: Assignment: Assignment due: 13, 14 Final Project Proposal Tuesday, 10/16 6:00 am (Monday 30:00) Lecture 9 - 10/17/12 - iOS 6 Watch before: Assignment: Assignment due: SEC - 9/5/12 Optional 15, 16 iOS 6 app Friday, 10/26 6:00 am (Thursday 30:00) COMP 446 / ELEC 446 - Week 3 10 COMP 446 (tentative) Syllabus Lecture 10 - 10/24/12 – HTML 5 Assignment: Assignment due: Continue working on Final Project Wednesday, 11/28 2:00pm Assignment: Assignment due: Continue working on Final Project Wednesday, 11/28 2:00pm Lecture 14 - 11/21/12 – No class, but continue on final project Continue working on Final Project Wednesday, 11/28 2:00pm Lecture 13 - 11/14/12 - Individual meetings on Final Project Assignment: Assignment due: Lecture 12 - 11/7/12- Android vs. iOS vs. Windows Phone group debate Optional 17, 18 Major Final Project Feature Friday, 11/2 6:00 am (Thursday 30:00) Lecture 11 - 10/31/12 - TBD Watch before: Assignment: Assignment due: Assignment: Assignment due: Continue working on Final Project Wednesday, 11/28 2:00pm Lecture 15 - 11/28/12 - Final Presentations Assignment: Assignment due: SEC - 9/5/12 Document Final Project By time assigned for COMP 446 final (there is NO final exam) COMP 446 / ELEC 446 - Week 3 11 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 SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 12 Assignment #1 Comments SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 13 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 SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 14 All Those Symbols ; @ + // - . {} # [] ! () += * % ** == : ^ SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 15 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 { [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 16 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 obj points 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 = [obj methodWhichReturnsAnInt]; // i will be zero if obj is nil Be careful if the method returns a C struct. Return value is undefined. CGPoint p = [obj getLocation]; // p will have an undefined value if obj is nil SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 17 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) { } SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 18 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 / super is calling instance self means “my implementation” super means “my superclass’s implementation” SEC - 9/5/12 Starts with a plus sign + (id) alloc; + (Ship *)motherShip; + (NSString *)stringWithFormat:... Creation & Utility Methods Calling syntax [Class method] Ship *ship = [Ship motherShip]; NSString *resultString = [NSString stringWithFormat:@“%g”, result]; [[ship class] doSomething]; self / super is this class self means “this class’s class methods” super means “this class’s superclass’s class methods” COMP 446 / ELEC 446 - Week 3 19 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; SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 20 Object Typing @interface Vehicle - (void)move; @end @interface Ship : Vehicle - (void)shoot; @end Ship *s = [[Ship alloc] init]; [s shoot]; [s move]; SEC - 9/5/12 Vehicle *v = s; [v shoot]; id obj = ...; [obj shoot]; [obj someMethodNameThatNoObjectAnywhereRespondsTo]; NSString *hello = @”hello”; [hello shoot]; Ship *helloShip = (Ship *)hello; [helloShip shoot]; COMP 446 / ELEC 446 - Week 3 21 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 ([obj respondsToSelector:@selector(shoot)]) { [obj shoot]; } else if ([obj respondsToSelector:@selector(shootAt:)]) { [obj shootAt:target]; } SEL is the Objective-C “type” for a selector SEL shootSelector = @selector(shoot); SEL shootAtSelector = @selector(shootAt:); SEL moveToSelector = @selector(moveTo:withPenColor:); SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 22 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 SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 23 Foundation Framework Value and collection classes User defaults Archiving Notifications Undo manager Tasks, timers, threads File system, pipes, I/O, bundles SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 24 NSObject Root class Implements many basics SEC - 9/5/12 Memory management Introspection Object equality COMP 446 / ELEC 446 - Week 3 25 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 SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 26 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!”; SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 27 Format Strings Similar to printf, but with %@ added for objects NSString *aString = @”Johnny”; NSString *log = [NSString stringWithFormat: @”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 SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 28 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 = [myString stringByAppendingString:@” world!”]; fullString would be set to Hello world! SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 29 NSString Common NSString methods - (BOOL)isEqualToString:(NSString *)string; - (BOOL)hasPrefix:(NSString *)string; - (int)intValue; - (double)doubleValue; Example: NSString *myString = @”Hello”; NSString *otherString = @”449”; if ([myString hasPrefix:@”He”]) { // will make it here } if ([otherString intValue] > 500) { // won’t make it here } SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 30 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]; [newString appendString:@”Hi”]; [newString appendFormat:@”, my favorite number is: %d”, [self favoriteNumber]]; SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 31 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 SEC - 9/5/12 Immutable collections can be shared without side effect Prevents unexpected changes Mutable objects typically carry a performance overhead COMP 446 / ELEC 446 - Week 3 32 NSArray Common NSArray methods + arrayWithObjects:(id)firstObj, ...; // nil terminated!!! - (unsigned)count; - (id)objectAtIndex:(unsigned)index; - (unsigned)indexOfObject:(id)object; NSNotFound returned for index if not found NSArray *array = [NSArray arrayWithObjects:@”Red”, @”Blue”, @”Green”, nil]; if ([array indexOfObject:@”Purple”] == NSNotFound) { NSLog (@”No color purple”); } Be careful of the nil termination!!! SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 33 NSMutableArray NSMutableArray subclasses NSArray So, everything in NSArray Common NSMutableArray Methods + (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]; SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 34 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 } SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 35 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]; [colors setObject:@”Orange” forKey:@”HighlightColor”]; SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 36 NSSet Unordered collection of objects Common NSSet methods + setWithObjects:(id)firstObj, ...; // nil terminated - (unsigned)count; - (BOOL)containsObject:(id)object; NSOrderedSet SEC - 9/5/12 Faster searching Does not inherit from NSSet COMP 446 / ELEC 446 - Week 3 37 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; SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 38 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]); } SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 39 NSNumber In Objective-C, you typically use standard C number types NSNumber is 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; SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 40 Other Classes NSData / NSMutableData Arbitrary sets of bytes NSDate / NSCalendarDate Times and dates NSValue SEC - 9/5/12 Object store for object pointers and other values like C structures, int, float, char, etc. COMP 446 / ELEC 446 - Week 3 41 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 NSArray is a Property List if all its members are too So an NSArray of NSString is a Property List. So is an NSArray of NSArray as long as those NSArray’s members are Property Lists. An NSDictionary is one only if all keys and values are too An NSArray of NSDictionarys whose 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. [plist writeToFile:(NSString *)path atomically:(BOOL)]; // plist is NSArray or NSDictionary SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 42 NSUserDefaults Lightweight storage of Property Lists. It’s basically an NSDictionary that 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 [[NSUserDefaults standardUserDefaults] setArray:rvArray forKey:@“RecentlyViewed”]; Sample methods: - (void)setDouble:(double)aDouble forKey:(NSString *)key; - (NSInteger)integerForKey:(NSString *)key; // NSInteger is a typedef to 32 or 64 bit int - (void)setObject:(id)obj forKey:(NSString *)key; // obj must 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! [[NSUserDefaults standardUserDefaults] synchronize]; SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 43 Assignment #2 Questions SEC - 9/5/12 COMP 446 / ELEC 446 - Week 3 44