Sunteți pe pagina 1din 33

Brief Intro to Objective-C

Tim Johnsen

What is Objective-C?

What is Objective-C?
Object Oriented language

What is Objective-C?
Object Oriented language C-based

What is Objective-C?
Object Oriented language C-based Compiled

What is Objective-C?
Object Oriented language C-based Compiled Dynamically-typed

What is Objective-C?
Object Oriented language C-based Compiled Dynamically-typed Reference-counted

What is Objective-C?
Object Oriented language C-based Compiled Dynamically-typed Reference-counted Used for native OS X and iOS
Development

Learning
1. Creating a Class 2. Creating Objects 3. Manipulating Objects

Creating a Class
Classes come in pairs of les
C++

ObjC

Declaration

Implementation

Creating a Class
Classes come in pairs of les
C++

ObjC

Declaration

Implementation

Creating a Class
Classes come in pairs of les
C++

ObjC

Declaration

Implementation

Creating a Class
The Declaration
// imports @interface ClassName : SuperclassName <Protocols Implemented> { // instance variables } // method declarations @end // includes class ClassName : SuperclassName, Superclass... { // instance variables // method declarations }

ObjC

C++

Creating a Class
The Declaration
// imports @interface ClassName : SuperclassName <Protocols Implemented> { // instance variables } // method declarations @end // includes class ClassName : SuperclassName, Superclass... { // instance variables // method declarations }

ObjC

C++

Creating a Class
The Declaration
// imports @interface ClassName : SuperclassName <Protocols Implemented> { // instance variables } // method declarations @end // includes class ClassName : SuperclassName, Superclass... { // instance variables // method declarations }

ObjC

C++

Creating a Class
The Declaration
// imports @interface ClassName : SuperclassName <Protocols Implemented> { // instance variables } // method declarations @end // includes class ClassName : SuperclassName, Superclass... { // instance variables // method declarations }

ObjC

C++

Objective-C does not support multiple inheritance, uses protocols

Creating a Class
The Declaration
// imports @interface ClassName : SuperclassName <Protocols Implemented> { // instance variables } // method declarations @end // includes class ClassName : SuperclassName, Superclass... { // instance variables // method declarations }

ObjC

C++

Creating a Class
The Declaration
// imports @interface ClassName : SuperclassName <Protocols Implemented> { // instance variables } // method declarations @end // includes class ClassName : SuperclassName, Superclass... { // instance variables // method declarations }

ObjC

C++

Creating a Class
Method Declarations
- (ReturnType)methodName;

ObjC

ReturnType methodName();

C++

Creating a Class
Method Declarations
- (ReturnType)methodName; - (ReturnType)methodName:(ParamType)param;

ObjC

ReturnType methodName(); ReturnType methodName(ParamType param);

C++

Creating a Class
Method Declarations
- (ReturnType)methodName; - (ReturnType)methodName:(ParamType)param;

ObjC

- (ReturnType)methodName:(ParamType)param moreMethodName: (Type)param2

ReturnType methodName(); ReturnType methodName(ParamType param); ReturnType methodName(ParamType param, Type param2);

C++

Creating a Class
Method Declarations
+ (ReturnType)methodName; + (ReturnType)methodName:(ParamType)param;

ObjC

+ (ReturnType)methodName:(ParamType)param moreMethodName: (Type)param2

static ReturnType methodName(); static ReturnType methodName(ParamType param); ReturnType methodName(ParamType param, Type param2);

C++

Static or class methods are declared using a +

Creating a Class
The Declaration - Example
#import <Foundation/Foundation.h> #import Liquid.h #import Drinkable.h // include libraries // include class headers // include a protocol

@interface Beer : Liquid <Drinkable> { NSString *name; // other objects NSString *manufacturer; float proof; // basic types } - (void)setProof:(float)newProof; - (float)proof; @end // setters // getters

Creating a Class
The Implementation

Starts with @implementation Contains method denitions

Creating a Class
The Implementation - Example
#import Beer.h @implementation Beer - (void)setProof:(float)newProof { proof = newProof; } - (float)proof { return proof; } @end // the setter // include declaration // just like C++

// the getter

Creating Objects

Instantiation In C++ and Java, the name for the constructor and destructor is always the class name
destructor always have the same name: init and dealloc allocation and initialization (alloc and init)

In Objective-C, the constructor and

Instantiation is also split into two pieces,

Creating Objects
Instantiation
Object *obj = [[Object alloc] init];

ObjC

Allocates memory for object

Object *obj = new Object;

C++

Creating Objects
Instantiation
Object *obj = [[Object alloc] init];

ObjC

Initializes object

Object *obj = new Object;

C++

Creating Objects
Instantiation
Object *obj = [[Object alloc] init];

ObjC

Initializes object

Object *obj = new Object;

C++

Developers override init as the constructor

Creating Objects
Quick Note on Memory Management Im not going to go over the equivalent of delete or free() in ObjC, this is a whole other topic. For more info visit Apples Memory Management Programming Guide

Manipulating Objects
Method Calls Method calls on Objects work a lot like the method declaration we discussed before. Theyre enclosed in square braces.
- (ReturnType)methodName:(ParamType)param; [object methodName:param];

Manipulating Objects
Method Calls - Example Set object at index in an Array
- (void)setObject:(id)object atIndex:(int)index; [anArray setObject:anObject atIndex:5];

Thanks!
Questions?

S-ar putea să vă placă și