DOC PREVIEW
CU-Boulder CSCI 5448 - ADVANCED IOS

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 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 46 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 46 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 46 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 46 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 46 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 46 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 46 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 46 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© Kenneth M. Anderson, 2011ADVANCED IOSCSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGNLECTURE 20 — 03/17/20111Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Goals of the LectureCover additional topics related to iOS programmingAdditional Objective-C 2.0 techniquesTa bl e a n d N av i g a t i o n c o n t r o l l e rCore DataMapKit: MapView and Core LocationRetrieving Data from the Web2Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Credit Where Credit Is DueMy examples are drawn from two booksThe excellent “More iPhone 3 Development: Tackling iPhone SDK 3” by Dave Mark and Jeff LaMarcheDon’t worry, the examples still work with iOS 4.x“Beginning iOS 4 Application Development in Full Color” by Wei-Meng LeeBoth are useful and highly recommended; I will not be providing extensive coverage of the examples, just highlights3Wednesday, March 23, 2011© Kenneth M. Anderson, 2011More Objective-C 2.0 TechniquesPropertiesUpdate on best practice surrounding propertiesCategorieshow to extend existing classes without subclassing themProtocolsObjective-C’s version of Java’s Interface4Wednesday, March 23, 2011© Kenneth M. Anderson, 2011PropertiesBack in Lecture 13, I presented an example of specifying properties for Objective-C classesproperties were used to auto-generate getter and setter methods for publicly visible attributesnote: default visibility in Objective-C is protected, you can use @private and @public to change that default, if neededBut the example I gave had some duplication in it (and developers hate duplication!)5Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Example#import <Foundation/Foundation.h>@interface Greeter : NSObject { NSString *greetingText;}@property (nonatomic, retain) NSString *greetingText;@endThe duplication lies with “NSString *greetingText”; why do we have to say this twice?6Wednesday, March 23, 2011© Kenneth M. Anderson, 2011The Answer: We Don’t!#import <Foundation/Foundation.h>@interface Greeter : NSObject {}@property (nonatomic, retain) NSString *greetingText;@endObjective-C will auto-generate the attribute definition for us This feature has been available for some time but couldn’t be used in iOS programming until recently due to a technical problem with the iOS simulator7Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Best Practice: property name ≠ att name#import "Greeter.h"@implementation Greeter@synthesize greetingText=_greetingText;@endFurthermore, best practice dictates that when you synthesize the property, you specify that the attribute name be different than the publicly available property nameNow, external code only uses the property and internal code can clearly indicate when it is using the property or the attribute8Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Use of PropertyWhen you have a property defined, you can access it using a syntax similar to accessing attributes in JavaGreeter *g = [[Greeter alloc] init];g.greetingText = @“Howdy”NSLog(@”%@, Ken!”, g.greetingText);In line 2, we are invoking the setter; In line 3, we are invoking the getterInternal to the Greeter class, we can use the property via self.greetingText or the attribute with _greetingText9Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Objective-C Categories (I)Have you ever been in a situation where you’re using a class provided by a library and you say“I wish this class had a method that did …”In most languages, if you want to add a method to an existing class, say java.lang.String, you would need to create a subclass: “class MyString extends String”Warning: Abandon All Hope, Ye Who Enter Here!This approach is fraught with perilIn Objective-C, you don’t have to subclass: just use a category10Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Objective-C Categories (II)Objective-C Categories let you re-open a class definition and add a new method!The original class will then act as if it had that method all along!Yo u r n e w m e t h o d i s o f t e n i m p l e m e n t e d u s i n g j u s t t h e publicly available methods of the original class and so you don’t require any special knowledge of the original class to add the new method11Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Objective-C Categories (IV)To c r e a t e a c a t e g o r y, yo u u s e t h e f o l l o w i n g s y n t a x@interface ExistingClass (NameOfCategory)<method signatures>@end@implementation ExistingClass (NameOfCategory)<method defs>@end12Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Objective-C Categories (V)Example of extending built-in NSArray class@interface NSArray (NestedArrays)- (NSInteger) countOfNestedArray:(NSUInteger)pos;@end13Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Objective-C Categories (VI)Example of extending built-in NSArray class@implementation NSArray (NestedArrays)- (NSInteger) countOfNestedArray:(NSUInteger)pos { NSArray *subArray = [self objectAtIndex:section]; return [subArray count];}@end14Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Objective-C Categories (VII)Now, you simply include the category in new code and NSArray will act as if it always had the method countOfNestedArray: (!!!)#import "NSArray-NestedArrays.h"NSArray *foo = <code to get an array>NSInteger subarray_count = [foo countOfNestedArray:2];NSLog(@“%d items in subarray”, subarray_count);15Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Protocols (I)Protocols are Objective-C’s version of Java’s InterfacesThey allow you to define a type that is guaranteed to implement a particular set of methodsA class can be declared as “conforming” to a particular protocolYo u c a n t h e n r e f e r t o a l l o b j e c t s t h a t c o n f o r m t o a protocol in a uniform mannerProtocols are typically used to define the interface of a delegate16Wednesday, March 23, 2011© Kenneth M. Anderson, 2011Protocols (II)To d e fi n e a p r o t o c o l , yo u u s e t h e f o l l ow i n g s y n t a x@protocol ProtocolName <method signatures>@endTo c o n f o r m t o a p r o t o c o l , yo u u s e t h e f o l l ow i n g s y n t a x@interface MyClass <ProtocolName1, ProtocolName2> …@end17The compiler will then make sure that you implement the methods of the protocolWednesday, March 23, 2011© Kenneth M. Anderson, 2011Protocols (III)To d e c l a r e a v a r i a bl e o r p a r a m e t e r t


View Full Document

CU-Boulder CSCI 5448 - ADVANCED IOS

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download ADVANCED IOS
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 ADVANCED IOS 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 ADVANCED IOS 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?