Post on 21-Oct-2014
description
Objective-C ProgrammingIrving iOS Jumpstart
Objective-C Programming
March 11, 2014
Overview of Today
• Quick 5 Realms Overview
• Objective-C Messaging
• Simple Programmatic UITableView Example
• Realistic UITableView Example
5 Realmsfor Learning iOS Development
Download full 5 Realms presentation at: http://www.slideshare.net/irving-ios-jumpstart/5-realms
Why 5 Realms?
To the beginning iOS developer the formalities of classes, frameworks, design patterns, and learning an all new integrated development
environment can be overwhelming.
Why 5 Realms?
Knowing which realm you're having trouble in really helps in finding the solution and getting unstuck in
your learning efforts.
5 Realms
Design Patterns Objective-C
iOS SDK OOP
Xcode IDE
Objective-C Messaging
Objective-C
Defining Classes in Objective-C
• Created via .h file and .m file
• All classes inherit from NSObject or another class
Objective-C
@interface @implementation
• @interface in an objective-c .h file
• @implementation in an objective-c .m file
• serves to separate the interface of a class from its implementation.
Objective-C
@property @synthesize
• Objective-C properties
• @property directive is used to create properties on the class.
• Properties are accessed (read and set) via automagically created setters and getters.
• In Xcode 5, optionally use @synthesize to give instance variable a custom name
Objective-C
Objective-C Methods• Class method
+ (return_type)methodName
• Instance method
- (return_type)methodName
• Distinguished by + or -
Objective-C
Objective-C Methods- (void)addHotels { NSArray *hotels = [NSArray arrayWithObjects:@"Aloft", @"The W", @"Hilton", nil]; ourHotels = [NSArray arrayWithArray:hotels]; }
- (return_type)methodName
Objective-C
Method With Arguments
• this method has one argument
- (void)listOurHotelsUsingTitle:(NSString *)title { NSLog(@"%@: %@", title, ourHotels); }
Objective-C
Method With Arguments
• including sample method call below
- (void)listOurHotelsUsingTitle:(NSString *)title
{
NSLog(@"%@: %@", title, ourHotels);
}
![self listOurHotelsUsingTitle:@"Favorite Hotels"];
Objective-C
Foundation Framework
• NSString - the Foundation Framework class used to create string objects!
NSString *city = @"Irving";
iOS SDK
Foundation Framework
NSString *myStory = [NSString stringWithFormat:@"The brown fox lives in %@", city];
//The brown fox lives in Irving
iOS SDK
Foundation Framework
• NSString comparison
if ([city isEqualToString:@"Irving"]) {
NSLog(@"The two strings are the same.");
} else {
NSLog(@"The two strings are different.");
} //The two strings are the same.
iOS SDK
Foundation Framework
• NSString comparison
if ([city isEqualToString:myStory]) {
NSLog(@"The two strings are the same.");
} else {
NSLog(@"The two strings are different.");
} //The two strings are different.
iOS SDK
Foundation Framework
• NSArray - the Foundation Framework class used to create array objects!
NSArray *hotels = [NSArray arrayWithObjects:@"Aloft", @"The W", @"Hilton", nil];
• An array is a set of ordered data items
iOS SDK
Foundation Framework
NSArray *hotels = [NSArray arrayWithObjects:@"Aloft", @"The W", @"Hilton", nil];
• array index begins at zero, so an array with 3 elements are indexed 0, 1, and 2
NSLog(@"The hotel at index 1 in this array is %@", [hotels objectAtIndex:1]); // The W
iOS SDK
Foundation Framework
• NSArray shortcut (objective-c literals)!
NSArray *hotels = @[@"Aloft", @"The W", @"Hilton"];
• Cleaner and familiar syntax
iOS SDK
Foundation Framework
• NSDictionary - the Foundation Framework class used to store key and value pairs!
NSDictionary *hotels = [NSDictionary dictionaryWithObjectsAndKeys:@"5 star", @"Aloft", @"4 star", @"W", @"5 star", @"Hilton", nil];
iOS SDK
Foundation Framework
- (void)addHotelsWithRatings { NSDictionary *hotels = [NSDictionary dictionaryWithObjectsAndKeys:@"5 star", @"Aloft", @"4 star", @"W", @"5 star", @"Hilton", nil];
!
NSLog(@"%@", hotels); }
iOS SDK
Foundation Framework
• NSDictionary shortcut (objective-c literals)!
NSDictionary *hotels = @{@"Aloft":@"5 star", @"W":@"4 star", @"Hilton":@"5 star"};
• Cleaner syntax
iOS SDK
Foundation Framework
• Mutable objects are changeable after creation.
• Immutable objects cannot change once they are created.
• Therefore additional methods exist on mutable objects
iOS SDK
Mutable ObjectsNSString (immutable)
NSMutableString (mutable)
NSArray
NSMutableArray
NSDictionary
NSMutableDictionary
iOS SDK
Mutable Objects• NSMutableString Example
NSMutableString *iCanChange = [NSMutableString stringWithString:@"Hello..."];
NSLog(@"%@", [iCanChange stringByAppendingString:@"world!"]);
// Hello...world!
iOS SDK
Mutable Objects
Download samples from github
!
https://github.com/jamesderry
iOS SDK
Programmatic UITableView
Crosses several realms of IOS understanding.
!
!
!
Design Patterns Objective-C
iOS SDK OOP
Xcode IDE
Programmatic UITableView
• Xcode
• Storyboard - we'll build the view controller storyboard containing a tableview
• IBOutlet - we'll use to grab a handle to the tableview for manipulation in objective-c code
Xcode IDE
Programmatic UITableView
• Design Patterns
• Model - View - Controller (MVC) pattern
• Delegate pattern
• Datasource pattern
Design Patterns
Programmatic UITableView
• Objective-C features
• Protocols
• Classes (UITableView and UITableViewCell)
• Dot syntax for cell properties
Objective-C
Programmatic UITableView
• iOS SDK Features
• UIViewController (and view controller life cycle)
• UIView
• UITableView and UITableViewCell
iOS SDK
Programmatic UITableView Exercise
Create simple table view app
Programmatic UITableView Exercise
Common Pitfalls
• forgetting to match the prototype cell identifier name to the name used in your code
• forgetting to set the tableview datasource and delegate
Thank You!
• Credits
• Wikipedia
• Programming in Objective-C by Stephen Kochan
(any errors or omissions are probably mine)