23
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Embed Size (px)

Citation preview

Page 1: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Introduction to Objective-Cand Xcode (Part 2)

FA 175Intro to Mobile App Development

Page 2: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Agenda

• Object-oriented programming• OOP in Objective-C– Classes– Instances and objects– Properties and methods

Page 3: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Definition of a program, revisited

• Traditional definition of a program: sequence of instructions to be executed on a computer

• Under the object-oriented programming (OOP) paradigm: a program, when it executes, is a collection of interacting objects

• Programming in the OOP paradigm means specifying what data are in these objects and how these objects behave

Page 4: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

So… what is an object?

• An object is a thing that has type, identity, state, and behavior– Type: it belongs to a class of similar things– Identity: it is an instance distinct from other

objects– State: it has a set of properties that take on values– Behavior: it can act or carry out methods

Page 5: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Object examples

Light Bulb-state?-behavior?

Bank Account-state?-behavior?

Car-state?-behavior?

Page 6: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Class: Light Bulb

• State– lit or not (on or off)

• Behavior– turn on– turn off– check whether lit

Page 7: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Class: Bank Account

• State– balance

• Behavior– deposit– withdraw – inquire balance

Page 8: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Class: Car

• State– distance travelled– gas left

• Behavior– drive– load gas– check gas level– check odometer

Page 9: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Objective-C:interface versus implementation

• .h file contains an interface declaring properties and methods of a class– the interface is the public façade of an object

• .m file contains an implementation of the class– code for the methods plus other “private” data– the implementation contains details encapsulated

within the object, hidden from users• To create files in Xcode while project is open,

File->New->File, then choose Obective-C class

Page 10: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Interface for the Car class (Car.h)#import <Foundation/Foundation.h>

@interface Car : NSObject

@property double gasLeft;@property int distanceTravelled;

-(void) driveDistance:(int) dist;-(void) loadGas:(double) gas;

@end

Page 11: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Implementation (Car.m)#import "Car.h"@implementation Car

-(void) driveDistance:(int) dist{ self.distanceTravelled =

self.distanceTravelled + dist; double gasUsed = dist/8.5; self.gasLeft = self.gasLeft - gasUsed;}-(void) loadGas:(double) gas{ self.gasLeft = self.gasLeft + gas;}

@end

Page 12: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Using the Car class

#import "Car.h"...

NSLog(@"Test code for a car object"); Car *myCar = [Car alloc]; [myCar loadGas:50.0]; [myCar driveDistance:10]; NSLog(@"gas: %6.2f, distance: %d", myCar.gasLeft, myCar.distanceTravelled); [myCar driveDistance:94]; [myCar loadGas:10.0]; NSLog(@"gas: %6.2f, distance: %d", myCar.gasLeft, myCar.distanceTravelled);

Page 13: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Encapsulation and direct data update

• Oftentimes, it is appropriate for properties to be “readonly” and updated only through appropriate methods

• Example:– gasLeft property should be updated only as a

result of driveDistance or loadGas– Try adding: myCar.gasLeft = 100.0; at the end of

the code, and then print myCar.gasLeft

Page 14: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Solution: specify property attributes in .h and .m files

• In .h file, replace property declarations with:– @property(readonly) double gasLeft;

@property(readonly) int distanceTravelled;• In .m file, add the following after #import line:– @interface Car()

@property(readwrite) double gasLeft;@property(readwrite) int distanceTravelled;@end

• Notice that direct update of properties are no longer allowed

Page 15: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Syntax

• Property declaration– @property(<attribute>,…) <type> <name>;

• Method declaration (no arguments)– -(<type>)<name>;

• Method declaration (one argument)– -(<type>)<name>:(<type>)<name>;

Page 16: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Syntax

• Object creation/instantiation– <var-name> = [<class-name> alloc];

• Referring to a property of an object– From within the class: self.<property-name>– For an object variable:

<var-name>.<property-name>• Invoking methods:– [<var-name> <method-name>];– [<var-name> <method-name>:<expression>];

Page 17: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Naming conventions

• Variable names and method names– Camel case: begin with small letter, capitalize first

letters of succeeding words– Examples: distanceTravelled, myCar, loadGas

• Class names– Capitalize first letters of all words within the name– Examples: BankAccount, Car, LightBulb

Page 18: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

void

• The methods loadGas and driveDistance have the following signatures– -(void)driveDistance:(int) dist– -(void)loadGas:(double) gas

• Here, void means “no return value”• Some methods return a value• Example: add a method to the Car class with

the following signature– -(double) distanceTravelledInMiles

Page 19: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Returning a value from a method

• In Car.h:– -(double) distanceTravelledInMiles;

• In Car.m– -(double) distanceTravelledInMiles

{ double miles = self.distanceTravelled*0.62; return miles;}

• In your test code,– NSLog(@”my car travelled %6.2 miles”,

[myCar distanceTravelledInMiles]);

Page 20: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Initialization

• It is common to provide initialization code intended for a newly created object– Set initial values for properties

• By convention, these methods should begin with the word init

• Also by convention (and for reasons too technical to discuss at this point),use _<propertyname> (e.g., _gasLeft)instead of self.<propertyname> (e.g. self.gasLeft),when referring to the properties

Page 21: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

init method examples-(id) init{ self = [super init]; _gasLeft = 0; _distanceTravelled = 0; return self;}-(id) initWithGas:(double) amt{ self = [super init]; _gasLeft = amt; _distanceTravelled = 0; return self;}

Page 22: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Implementing initialization methods

• Make sure to place method declarationsof init and initWithGas in Car.h

• Method implementations should be in Car.m• In your test code, add the following:– Car *car2 = [[Car alloc] init];

Car *car3 = [[Car alloc] initWithGas:20.0];[car3 driveDistance:15];// some code to print gas levels of car2 & car3

Page 23: Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development

Summary

• Introduction to Object-Oriented Programming• Objective-C class creation in Xcode

(Car class: Car.h, Car.m)• OOP concepts tackled– classes, objects, properties, methods, initialization

• Naming conventions