Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development."— Presentation transcript:

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

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

3 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

4 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

5 Object examples Light Bulb -state? -behavior ? Bank Account -state? -behavior? Car -state? -behavior ?

6 Class: Light Bulb State – lit or not (on or off) Behavior – turn on – turn off – check whether lit

7 Class: Bank Account State – balance Behavior – deposit – withdraw – inquire balance

8 Class: Car State – distance travelled – gas left Behavior – drive – load gas – check gas level – check odometer

9 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

10 Interface for the Car class (Car.h) #import @interface Car : NSObject @property double gasLeft; @property int distanceTravelled; -(void) driveDistance:(int) dist; -(void) loadGas:(double) gas; @end

11 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

12 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);

13 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

14 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

15 Syntax Property declaration – @property(,…) ; Method declaration (no arguments) – -( ) ; Method declaration (one argument) – -( ) :( ) ;

16 Syntax Object creation/instantiation – = [ alloc]; Referring to a property of an object – From within the class: self. – For an object variable:. Invoking methods: – [ ]; – [ : ];

17 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

18 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

19 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]);

20 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 _ (e.g., _gasLeft) instead of self. (e.g. self.gasLeft), when referring to the properties

21 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; }

22 Implementing initialization methods Make sure to place method declarations of 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

23 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


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

Similar presentations


Ads by Google