cocoa for web developers

41
Introduction to OS X development with Cocoa …and how web developers can cheat George Brocklehurst http://georgebrock.com @georgebrock on Twitter

Upload: georgebrock

Post on 15-Jan-2015

4.291 views

Category:

Technology


2 download

DESCRIPTION

My talk at BarCamp London 5: An introduction to Cocoa development and how web developers can cheat. Source code to go with this presentation is available here: http://georgebrock.com/conferences/barcamplondon5

TRANSCRIPT

Page 1: Cocoa for Web Developers

Introduction to OS X development with Cocoa

…and how web developers

can cheat

George Brocklehurst

http://georgebrock.com

@georgebrock on Twitter

Page 2: Cocoa for Web Developers

Before we start…

• Have you done any…– HTML?– Javascript?– PHP?– Any object oriented programming?– C?

Page 3: Cocoa for Web Developers

What is Cocoa?

• Framework for Mac OS X and iPhone

• Heavily MVC focused

• Usually written in Objective-C

• Can also be written in Python or Ruby

Page 4: Cocoa for Web Developers

Getting Started

• You will need: Apple’s Xcode Tools

• Available from:– http://developer.apple.com/technology/xco

de.html– Your OS X install DVDs

Page 5: Cocoa for Web Developers

Objective C Syntax: Classes

• Declaring a class interface (.h file)

@interface MyClass : NSObject

{

int anInteger;

}

- (void)doStuff;

@end

Page 6: Cocoa for Web Developers

Objective C Syntax: Classes

• Declaring a class interface (.h file)

@interface MyClass : NSObject

{

int anInteger;

}

- (void)doStuff;

@end

Interface only here!

Page 7: Cocoa for Web Developers

Objective C Syntax: Classes

• Declaring a class interface (.h file)

@interface MyClass : NSObject

{

int anInteger;

}

- (void)doStuff;

@end

Class name

Page 8: Cocoa for Web Developers

Objective C Syntax: Classes

• Declaring a class interface (.h file)

@interface MyClass : NSObject

{

int anInteger;

}

- (void)doStuff;

@end

Parent class

Page 9: Cocoa for Web Developers

Objective C Syntax: Classes

• Declaring a class interface (.h file)

@interface MyClass : NSObject

{

int anInteger;

}

- (void)doStuff;

@end

Members

Page 10: Cocoa for Web Developers

Objective C Syntax: Classes

• Declaring a class interface (.h file)

@interface MyClass : NSObject

{

int anInteger;

}

- (void)doStuff;

@endMethods

Page 11: Cocoa for Web Developers

Objective C Syntax: Classes

• Implementing a class (.m file)

#import “MyClass.h”

@implementation MyClass- (void)doStuff

{

// Do stuff here

}

@end

Page 12: Cocoa for Web Developers

Objective C Syntax: Classes

• Implementing a class (.m file)

#import “MyClass.h”

@implementation MyClass- (void)doStuff

{

// Do stuff here

}

@end

Interface

Page 13: Cocoa for Web Developers

Objective C Syntax: Classes

• Implementing a class (.m file)

#import “MyClass.h”

@implementation MyClass- (void)doStuff

{

// Do stuff here

}

@end

Start class implementation

Page 14: Cocoa for Web Developers

Objective C Syntax: Classes

• Implementing a class (.m file)

#import “MyClass.h”

@implementation MyClass

- (void)doStuff

{

// Do stuff here

}

@end Methods with bodies

Page 15: Cocoa for Web Developers

Objective C Syntax: Methods

-(void)myMethod:(int)arg

In other languages, might be:

function myMethod(arg)

Page 16: Cocoa for Web Developers

Objective C Syntax: Methods

-(void)myMethod:(int)arg

Can be either:

+ for a class method

- for an instance method

Method scope

Page 17: Cocoa for Web Developers

Objective C Syntax: Methods

-(void)myMethod:(int)arg

Can be any valid data type, including:

void returns nothing

id a pointer to an object of any class

NSString* a pointer to an NSStringBOOL a boolean (TRUE, FALSE, YES or NO)

Return type

Page 18: Cocoa for Web Developers

Objective C Syntax: Methods

-(void)myMethod:(int)arg

Colons precede arguments, but are considered part of the method name

Method name

Page 19: Cocoa for Web Developers

Objective C Syntax: Methods

-(void)myMethod:(int)arg

Come after or within the method name

For multiple arguments:-(void)myMethod:(int)arg andAlso:(int)arg2

(Method name is “myMethod:andAlso:”)

Argument type Argument name

Page 20: Cocoa for Web Developers

Objective C Syntax: Methods

-(void)myMethod:(int)arg1 andAlso:(int)arg2;

How to call this method:[myObject myMethod:10 andAlso:20];

In other languages this might be:myObject->myMethod(10, 20); //ormyObject.myMethod(10, 20);

Page 21: Cocoa for Web Developers

Objective C Syntax: Properties

• New short hand in Objective-C 2.0 (Xcode 3 / Leopard)

• Access class members without writing getters and setters

• Convenient, but nasty difficult to remember syntax

Page 22: Cocoa for Web Developers

Objective C Syntax: Properties

In the class interface (with methods):@property(copy, readwrite) NSString *propertyName;

In the class implementation:@synthesize propertyName;

To access:myInstance.propertyName = @”Foo”;

Page 23: Cocoa for Web Developers

Objective C Syntax: Properties

In the class interface (with methods):@property(copy, readwrite) NSString *propertyName;

In the class implementation:@synthesize propertyName;

To access:myInstance.propertyName = @”Foo”;

Storage method: assign, retain

or copy

Page 24: Cocoa for Web Developers

Objective C Syntax: Properties

In the class interface (with methods):@property(copy, readwrite) NSString *propertyName;

In the class implementation:@synthesize propertyName;

To access:myInstance.propertyName = @”Foo”;

Access permissions: readwrite or readonly

Page 25: Cocoa for Web Developers

Objective C Syntax: Properties

In the class interface (with methods):@property(copy, readwrite) NSString *propertyName;

In the class implementation:@synthesize propertyName;

To access:myInstance.propertyName = @”Foo”;

Can add other things:

nonatomic, getter=… and setter=…

Page 26: Cocoa for Web Developers

Objective C Syntax: Properties

In the class interface (with methods):@property(copy, readwrite) NSString *propertyName;

In the class implementation:@synthesize propertyName;

To access:myInstance.propertyName = @”Foo”;

Property variable declaration, must also be declared

as a class member

Page 27: Cocoa for Web Developers

Objective C Syntax: Properties

In the class interface (with methods):@property(copy, readwrite) NSString *propertyName;

In the class implementation:@synthesize propertyName;

To access:myInstance.propertyName = @”Foo”;

Tell Objective-C precompiler to make getter &

setter

Page 28: Cocoa for Web Developers

Objective C Syntax: Properties

In the class interface (with methods):@property(copy, readwrite) NSString *propertyName;

In the class implementation:@synthesize propertyName;

To access:myInstance.propertyName = @”Foo”;

Page 29: Cocoa for Web Developers

Objective C Syntax: Properties

In the class interface (with methods):@property(copy, readwrite) NSString *propertyName;

In the class implementation:@synthesize propertyName;

To access:myInstance.propertyName = @”Foo”;

Sensible syntax! Yay!

Page 30: Cocoa for Web Developers

Objective C Syntax: Selectors

SEL callback = NULL;

callback = @selector(myMethod:andAlso:);

- Like function pointers- Useful for callback type behavior

Page 31: Cocoa for Web Developers

Objective C Syntax: Selectors

SEL callback = NULL;

callback = @selector(myMethod:andAlso:);

- Like function pointers- Useful for callback type behavior

Data type for selectors

Page 32: Cocoa for Web Developers

Objective C Syntax: Selectors

SEL callback = NULL;

callback = @selector(myMethod:andAlso:);

- Like function pointers- Useful for callback type behavior

Macro to create selector

Page 33: Cocoa for Web Developers

Objective C Syntax: Selectors

SEL callback = NULL;

callback = @selector(myMethod:andAlso:);

- Like function pointers- Useful for callback type behavior

Page 34: Cocoa for Web Developers

Objective C Syntax: Strings

NSString *myString;

myString = @”This is a string”;

- Note the @ prefix- Tells the Objective-C precompiler to

instantiate an NSString instead of just creating a C string (aka. A nasty character array)

Page 35: Cocoa for Web Developers

Managing Memory

• Easier than vanilla C, honest!

• Reference counting

• Increment with retain, decrement with release

• Rule of thumb: release anything you alloc, copy or retain

Page 36: Cocoa for Web Developers

Managing Memory: Example

NSString *str;

str = [NSString alloc]; //1

[str retain]; //2

[str release]; //1

[str release], str = nil; //0

Page 37: Cocoa for Web Developers

Managing Memory: Autorelease

[str autorelease];

This will automatically release an object when it’s finished with

Another rule of thumb:[NSThing thingWith:…]

returns an autoreleased instance[[NSThing alloc] initWith:…]

needs manually releasing

Page 38: Cocoa for Web Developers

Wasn’t there something about cheating?!

• WebKit is part of Cocoa• Can build your UI with HTML, CSS and

Javascript and only use Objective-C when you really need to

Page 39: Cocoa for Web Developers

Demo

Page 40: Cocoa for Web Developers

I Can Haz Questions?

Page 41: Cocoa for Web Developers

K, Thx, Bai!

George Brocklehursthttp://georgebrock.com

@georgebrock on Twitter