37
Objective-C Programming Irving iOS Jumpstart

Irving iOS Jumpstart Meetup - Objective-C Session 2

  • View
    304

  • Download
    0

Embed Size (px)

DESCRIPTION

Session 2 of Objective-C for the Irving iOS Jumpstart meetup. This evening we learned about creating UITableView's and programmatically setting the contents of their UITableViewCell's.

Citation preview

Page 1: Irving iOS Jumpstart Meetup - Objective-C Session 2

Objective-C ProgrammingIrving iOS Jumpstart

Page 2: Irving iOS Jumpstart Meetup - Objective-C Session 2

Objective-C Programming

March 11, 2014

Page 3: Irving iOS Jumpstart Meetup - Objective-C Session 2

Overview of Today

• Quick 5 Realms Overview

• Objective-C Messaging

• Simple Programmatic UITableView Example

• Realistic UITableView Example

Page 4: Irving iOS Jumpstart Meetup - Objective-C Session 2

5 Realmsfor Learning iOS Development

Download full 5 Realms presentation at: http://www.slideshare.net/irving-ios-jumpstart/5-realms

Page 5: Irving iOS Jumpstart Meetup - Objective-C Session 2

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.

Page 6: Irving iOS Jumpstart Meetup - Objective-C Session 2

Why 5 Realms?

Knowing which realm you're having trouble in really helps in finding the solution and getting unstuck in

your learning efforts.

Page 7: Irving iOS Jumpstart Meetup - Objective-C Session 2

5 Realms

Design Patterns Objective-C

iOS SDK OOP

Xcode IDE

Page 8: Irving iOS Jumpstart Meetup - Objective-C Session 2

Objective-C Messaging

Objective-C

Page 9: Irving iOS Jumpstart Meetup - Objective-C Session 2

Defining Classes in Objective-C

• Created via .h file and .m file

• All classes inherit from NSObject or another class

Objective-C

Page 10: Irving iOS Jumpstart Meetup - Objective-C Session 2

@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

Page 11: Irving iOS Jumpstart Meetup - Objective-C Session 2

@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

Page 12: Irving iOS Jumpstart Meetup - Objective-C Session 2

Objective-C Methods• Class method

+ (return_type)methodName

• Instance method

- (return_type)methodName

• Distinguished by + or -

Objective-C

Page 13: Irving iOS Jumpstart Meetup - Objective-C Session 2

Objective-C Methods- (void)addHotels { NSArray *hotels = [NSArray arrayWithObjects:@"Aloft", @"The W", @"Hilton", nil]; ourHotels = [NSArray arrayWithArray:hotels]; }

- (return_type)methodName

Objective-C

Page 14: Irving iOS Jumpstart Meetup - Objective-C Session 2

Method With Arguments

• this method has one argument

- (void)listOurHotelsUsingTitle:(NSString *)title { NSLog(@"%@: %@", title, ourHotels); }

Objective-C

Page 15: Irving iOS Jumpstart Meetup - Objective-C Session 2

Method With Arguments

• including sample method call below

- (void)listOurHotelsUsingTitle:(NSString *)title

{

NSLog(@"%@: %@", title, ourHotels);

}

![self listOurHotelsUsingTitle:@"Favorite Hotels"];

Objective-C

Page 16: Irving iOS Jumpstart Meetup - Objective-C Session 2

Foundation Framework

• NSString - the Foundation Framework class used to create string objects!

NSString *city = @"Irving";

iOS SDK

Page 17: Irving iOS Jumpstart Meetup - Objective-C Session 2

Foundation Framework

NSString *myStory = [NSString stringWithFormat:@"The brown fox lives in %@", city];

//The brown fox lives in Irving

iOS SDK

Page 18: Irving iOS Jumpstart Meetup - Objective-C Session 2

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

Page 19: Irving iOS Jumpstart Meetup - Objective-C Session 2

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

Page 20: Irving iOS Jumpstart Meetup - Objective-C Session 2

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

Page 21: Irving iOS Jumpstart Meetup - Objective-C Session 2

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

Page 22: Irving iOS Jumpstart Meetup - Objective-C Session 2

Foundation Framework

• NSArray shortcut (objective-c literals)!

NSArray *hotels = @[@"Aloft", @"The W", @"Hilton"];

• Cleaner and familiar syntax

iOS SDK

Page 23: Irving iOS Jumpstart Meetup - Objective-C Session 2

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

Page 24: Irving iOS Jumpstart Meetup - Objective-C Session 2

Foundation Framework

- (void)addHotelsWithRatings { NSDictionary *hotels = [NSDictionary dictionaryWithObjectsAndKeys:@"5 star", @"Aloft", @"4 star", @"W", @"5 star", @"Hilton", nil];

!

NSLog(@"%@", hotels); }

iOS SDK

Page 25: Irving iOS Jumpstart Meetup - Objective-C Session 2

Foundation Framework

• NSDictionary shortcut (objective-c literals)!

NSDictionary *hotels = @{@"Aloft":@"5 star", @"W":@"4 star", @"Hilton":@"5 star"};

• Cleaner syntax

iOS SDK

Page 26: Irving iOS Jumpstart Meetup - Objective-C Session 2

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

Page 27: Irving iOS Jumpstart Meetup - Objective-C Session 2

Mutable ObjectsNSString (immutable)

NSMutableString (mutable)

NSArray

NSMutableArray

NSDictionary

NSMutableDictionary

iOS SDK

Page 28: Irving iOS Jumpstart Meetup - Objective-C Session 2

Mutable Objects• NSMutableString Example

NSMutableString *iCanChange = [NSMutableString stringWithString:@"Hello..."];

NSLog(@"%@", [iCanChange stringByAppendingString:@"world!"]);

// Hello...world!

iOS SDK

Page 29: Irving iOS Jumpstart Meetup - Objective-C Session 2

Mutable Objects

Download samples from github

!

https://github.com/jamesderry

iOS SDK

Page 30: Irving iOS Jumpstart Meetup - Objective-C Session 2

Programmatic UITableView

Crosses several realms of IOS understanding.

!

!

!

Design Patterns Objective-C

iOS SDK OOP

Xcode IDE

Page 31: Irving iOS Jumpstart Meetup - Objective-C Session 2

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

Page 32: Irving iOS Jumpstart Meetup - Objective-C Session 2

Programmatic UITableView

• Design Patterns

• Model - View - Controller (MVC) pattern

• Delegate pattern

• Datasource pattern

Design Patterns

Page 33: Irving iOS Jumpstart Meetup - Objective-C Session 2

Programmatic UITableView

• Objective-C features

• Protocols

• Classes (UITableView and UITableViewCell)

• Dot syntax for cell properties

Objective-C

Page 34: Irving iOS Jumpstart Meetup - Objective-C Session 2

Programmatic UITableView

• iOS SDK Features

• UIViewController (and view controller life cycle)

• UIView

• UITableView and UITableViewCell

iOS SDK

Page 35: Irving iOS Jumpstart Meetup - Objective-C Session 2

Programmatic UITableView Exercise

Create simple table view app

Page 36: Irving iOS Jumpstart Meetup - Objective-C Session 2

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

Page 37: Irving iOS Jumpstart Meetup - Objective-C Session 2

Thank You!

• Credits

• Wikipedia

• Programming in Objective-C by Stephen Kochan

(any errors or omissions are probably mine)