230 likes | 417 Views
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. Definition of a program, revisited.
E N D
Introduction to Objective-Cand 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
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
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
Object examples • Light Bulb • state? • behavior? • Car • state? • behavior? • Bank Account • state? • behavior?
Class: Light Bulb • State • lit or not (on or off) • Behavior • turn on • turn off • check whether lit
Class: Bank Account • State • balance • Behavior • deposit • withdraw • inquire balance
Class: Car • State • distance travelled • gas left • Behavior • drive • load gas • check gas level • check odometer
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
Interface for the Car class (Car.h) #import <Foundation/Foundation.h> @interface Car : NSObject @property double gasLeft; @property intdistanceTravelled; -(void) driveDistance:(int) dist; -(void) loadGas:(double) gas; @end
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
Using the Car class #import "Car.h" ... NSLog(@"Test code for a car object"); Car *myCar= [Car alloc]; [myCarloadGas:50.0]; [myCardriveDistance: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);
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
Solution: specify property attributes in .h and .m files • In .h file, replace property declarations with: • @property(readonly) double gasLeft;@property(readonly) intdistanceTravelled; • In .m file, add the following after #import line: • @interface Car()@property(readwrite) double gasLeft;@property(readwrite) intdistanceTravelled;@end • Notice that direct update of properties are no longer allowed
Syntax • Property declaration • @property(<attribute>,…) <type> <name>; • Method declaration (no arguments) • -(<type>)<name>; • Method declaration (one argument) • -(<type>)<name>:(<type>)<name>;
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>];
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
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
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”, [myCardistanceTravelledInMiles]);
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
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; }
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
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