Getting started with objective-C

Class - Revision
(Lecture Notes)
Copyright © 2011 Four Directions Limited
All rights reserved
Objective of this Revision
1. Demonstrate how to create class in Obejctive-C
programming language.
2. How to modify variable inside class using Property.
3. What is the relationship between Property and Setter,
Getter.
4. How to add Functions to class.
5. How to use Functions and Property.
6. How to use protocol
Class and Object
1. We will create a Thermometer Class.
2. Use this Class as a template, we will alloc a Thermometer
Object called thermometer.
3. It will contain a scale, a degree and a name variable.
4. We can then get the Fahrenheit, Kelvin from Thermometer
object and NSLog them to console.
5. We can also design The Thermometer Class so that it know how
to print out it's data to console.
6. Finally, we will create a protocol and add a delegate in
Thermometer class which will notify other object when critical
temperature reached.
Class and Object
1. Open a new project and called it ThermometerTest (Refer to
Lesson4-5 Tutorial Notes P.2)
2. Right click on ThermometerTestViewController → New File
→ Under IOS Tab, choose Cocoa Touch → Objective-C class
→ Next → Set subclass to NSObject → Next → Change the
file name to Thermometer → Save
3. There should be two files added. Thermometer.h and
Thermometer.m
4. Use alloc, init to create a Thermometer object.
e.g. Thermometer* thermometer = [Thermometer alloc] init];
Class and Object - Code
1. Inside Thermometer.h add three variable, degree, scale and
name.
@interface Thermometer : NSObject {
float degree;
char scale; //either Celcius - c, Fahrenheit - f, Kelvin - K
NSString* name;
}
2. Create getter and setter functions for variable degree.
In .h
- (float) degree;
- (void) setDegree:(float) degree_;
In .m
- (float) degree{
return degree;
}
- (void) setDegree:(float) degree_{
degree = degree_;
}
Class and Object - Code
3. Use Getter and Setter to get and set the degree.
Thermometer* thermometer = [[Thermometer alloc] init];
[thermometer setDegree:30.0f];
NSLog(@"Current Degree: %f",[thermometer degree]);
4. Comment the getter and setter by adding // before the code
and add property for variable degree.
@interface Thermometer : NSObject {
float degree;
char scale; //either Celcius - c, Fahrenheit - f, Kelvin - K
NSString* name;
}
@property (nonatomic)float degree;
@property (nonatomic)char scale;
@property (nonatomic,retain)NSString* name;
Class and Object - Code
5. Inside Thermometer.m, use @Synthesize to create getter
and setter for the property.
@synthesize degree;
@synthesize scale;
@synthesize name;
6. We will initialize the variable in init function.
- (id) init{
self = [super init];
if(self != nil){
scale = 'c';
degree = 0;
name = @"temperature";
}
return self;
}
;
Class and Object - Code
7. Implement the function to convert degree between different
scale.
- (void) setScale:(char)scale_{
if(scale_ == 'c'){
if(scale == 'k'){
degree = degree - 273.15;
}
else if(scale == 'f'){
degree = (degree - 32.0)/1.8;
}
}
else if(scale_ == 'f'){
if(scale == 'c'){
degree = degree*1.8 + 32.0;
}
else if(scale == 'k'){
degree = (degree - 273.15)*1.8 + 32.0;
}
}
else if(scale_ == 'k'){
if(scale == 'c'){
degree = degree + 273.15;
}
else if(scale == 'f'){
degree = (degree - 32.0)/1.8 + 273.15;
}
}
scale = scale_;
Class and Object - Code
8. Add a functions to print the information about this thermometer.
In .h
- (void) printDescription;
In .m
- (void) printDescription{
NSLog(@"Temperature of %@ in scale %c is %f",name,scale,degree);
}
Class and Object - Code
9. In viewDidLoad of ThermometerTestViewController.m, we
will add following code to test the Thermometer class.
Thermometer* thermometer = [[Thermometer alloc] init];
thermometer.degree = 30;
[thermometer printDescription];
[thermometer setScale:'f'];
[thermometer printDescription];
[thermometer setScale:'k'];
[thermometer printDescription];
Output:
2011-06-05 12:53:00.566 ThermometerTest[6956:207] Temperature of temperature in scale c is
30.000000
2011-06-05 12:53:00.569 ThermometerTest[6956:207] Temperature of temperature in scale f is
86.000000
2011-06-05 12:53:00.569 ThermometerTest[6956:207] Temperature of temperature in scale k is
303.149994
Protocol
1. Protocol is an interface define what a Class need to
implement if the class declare itself implement the protocol.
2. A class will make its own protocol if it has event dispatch
after certain event happened.
3. A class contain a variable implementing the protocol which
want to receive the event of the class, we will call this variable
delegate.
4. We will create an protocol to inform any object who is care
about the critical temperature of Thermometer class. If the
thermometer reached a certain degree, it will notify the object.
Protocol - Code
1. Create a protocol for Thermometer class.
@protocol ThermometerEventHandler
- (void) didCriticalTemperatureReached;
@end
2. Add an two variable to .h and declare their property
float criticalTemperature;
id<ThermometerEventHandler> delegate;
3. Don't forget to create their property and add @synthesize in .m.
@property (nonatomic)float criticalTemperature;
@property (nonatomic,assign)id<ThermometerEventHandler> delegate;
@synthesize criticalTemperature;
@synthesize delegate;
Protocol - Code
4. Modify the setDegree functions to inform delegate that the
critical temperature reached.
- (void) setDegree:(float)degree_{
degree = degree_;
if(degree >= criticalTemperature){
if(delegate != nil){
[delegate didCriticalTemperatureReached];
}
}
}
5. Modify the setScale function to make sure the critical
temperature will change according to scale.
if(scale_ == 'c'){
if(scale == 'k'){
degree = degree - 273.15;
criticalTemperature = criticalTemperature - 273.15;
}
else if(scale == 'f'){
degree = (degree - 32.0)/1.8;
criticalTemperature = (criticalTemperature - 32.0)/1.8;
}
}
Protocol - Code
6. In ThermometerTestViewController.h, import “Thermometer.h”
and implement ThermometerEventHandler.
@interface ThermometerTestViewController : UIViewController <ThermometerEventHandler>{
7. In ThermometerTestViewController.m, add following functions.
- (void) didCriticalTemperatureReached{
NSLog(@"Warning Critical Temperature %f%c Reached", thermometer.criticalTemperature,
thermometer.scale);
}
8. Modify the code in ThermometerTestViewController to following.
thermometer = [[Thermometer alloc] init];
thermometer.delegate = self;
thermometer.criticalTemperature = 70;
thermometer.degree = 30;
[thermometer printDescription];
...
…..
thermometer.degree = 360;