58
Objective-C for Java Developers http://bobmccune.com Tuesday, August 10, 2010

Objective-C for Java Developers - Intertech for Java Developers Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

  • Upload
    lamthuy

  • View
    215

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Objective-C for Java Developers

http://bobmccune.com

Tuesday, August 10, 2010

Page 2: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Objective-C Overview

Tuesday, August 10, 2010

Page 3: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Objective-C

• Strict superset of ANSI C• Object-oriented extensions

• Additional syntax and types

• Native Mac & iOS development language

• Flexible typing

• Simple, expressive syntax

• Dynamic runtime

The old new hotness

Tuesday, August 10, 2010

Page 4: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Why Use Objective-C?

• Key to developing for Mac & iOS platforms

• Performance• Continually optimized runtime environment

• Can optimize down to C as needed

• Memory Management

• Dynamic languages provide greater flexibility• Cocoa APIs rely heavily on these features

No Java love from Apple?

Tuesday, August 10, 2010

Page 5: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Java Developer’s Concerns

• Pointers

• Memory Management

• Preprocessing & Linking

• No Namespaces• Prefixes used to avoid collisions

• Common Prefixes: NS, UI, CA, MK, etc.

Ugh, didn’t Java solve all of this stuff

Tuesday, August 10, 2010

Page 6: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Creating Classes

Tuesday, August 10, 2010

Page 7: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Classes

• Classes define the blueprint for objects.

• Objective-C class definitions are separated into an interface and an implementation.• Usually defined in separate .h and .m files

•Defines the programming interface

•Defines the object's instance variable

•Defines the actual implementation code

•Defines one or more initializers to properly initialize and object instance

Tuesday, August 10, 2010

Page 8: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class interface

@interface BankAccount : NSObject {

}

@end

float accountBalance;NSString *accountNumber;

- (float)withDraw:(float)amount;- (void)deposit:(float)amount;

Tuesday, August 10, 2010

Page 9: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class interface

@interface BankAccount : NSObject {

}

@end

float accountBalance;NSString *accountNumber;

- (float)withDraw:(float)amount;- (void)deposit:(float)amount;

Tuesday, August 10, 2010

Page 10: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class interface

@interface BankAccount : NSObject {

}

@end

float accountBalance;NSString *accountNumber;

- (float)withDraw:(float)amount;- (void)deposit:(float)amount;

Tuesday, August 10, 2010

Page 11: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class interface

@interface BankAccount : NSObject {

}

@end

float accountBalance;NSString *accountNumber;

- (float)withDraw:(float)amount;- (void)deposit:(float)amount;

Tuesday, August 10, 2010

Page 12: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class interface

@interface BankAccount : NSObject {

}

@end

float accountBalance;NSString *accountNumber;

- (float)withDraw:(float)amount;- (void)deposit:(float)amount;

Tuesday, August 10, 2010

Page 13: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class interface

@interface BankAccount : NSObject {

}

@end

float accountBalance;NSString *accountNumber;

- (float)withDraw:(float)amount;- (void)deposit:(float)amount;

Tuesday, August 10, 2010

Page 14: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class implementation#import "BankAccount.h"

@implementation BankAccount

- (id)init { self = [super init]; return self;}

- (float)withdraw:(float)amount { // calculate valid withdrawal return amount;}

- (void)deposit:(float)amount { // record transaction}@end

Tuesday, August 10, 2010

Page 15: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class implementation#import "BankAccount.h"

@implementation BankAccount

- (id)init { self = [super init]; return self;}

- (float)withdraw:(float)amount { // calculate valid withdrawal return amount;}

- (void)deposit:(float)amount { // record transaction}@end

Tuesday, August 10, 2010

Page 16: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class implementation#import "BankAccount.h"

@implementation BankAccount

- (id)init { self = [super init]; return self;}

- (float)withdraw:(float)amount { // calculate valid withdrawal return amount;}

- (void)deposit:(float)amount { // record transaction}@end

Tuesday, August 10, 2010

Page 17: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining the class implementation#import "BankAccount.h"

@implementation BankAccount

- (id)init { self = [super init]; return self;}

- (float)withdraw:(float)amount { // calculate valid withdrawal return amount;}

- (void)deposit:(float)amount { // record transaction}@end

Tuesday, August 10, 2010

Page 18: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Working with Objects

Tuesday, August 10, 2010

Page 19: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Creating Objects

• No concept of constructors in Objective-C

• Regular methods create new instances• Allocation and initialization are performed

separately• Provides flexibility in where an object is allocated

• Provides flexibility in how an object is initialized

BankAccount account = new BankAccount();

BankAccount *account = [[BankAccount alloc] init];

Java

Objective C

From blueprint to reality

Tuesday, August 10, 2010

Page 20: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Creating Objects• NSObject defines class method called alloc

• Dynamically allocates memory for object

• Returns new instance of receiving class

BankAccount *account = [BankAccount alloc];

• NSObject defines instance method init

• Implemented by subclasses to initialize instance after memory for it has been allocated

• Subclasses commonly define several initializers

• alloc and init calls are almost always nested into single line

account = [account init];

BankAccount *account = [[BankAccount alloc] init];

Tuesday, August 10, 2010

Page 21: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Methods• Classes can define both class and instance methods

• Methods are always public.

• “Private” methods defined in implementation

• Class methods (prefixed with +):• Define behaviors associated with class, not particular

instance

• No access to instance variables

• Instance methods (prefixed with -)• Define behaviors specific to particular instance

• Manage object state

• Methods invoked by passing messages...

Tuesday, August 10, 2010

Page 22: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Messages

• Methods are invoked by passing messages• Done indirectly. We never directly invoke

methods

• Messages dynamically bound to method implementations at runtime

• Dispatching handled by runtime environment

• Simple messages take the form:• [object message];

• Can pass one or more arguments:• [object messageWithArg1:arg1 arg2:arg2];

Speaking to objects

Tuesday, August 10, 2010

Page 23: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

self and super• Methods have implicit reference to owning

object called self (similar to Java’s this)

• Additionally have access to superclass methods using super

-(id)init { if (self = [super init]) { // do initialization } return self;}

Tuesday, August 10, 2010

Page 24: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Invoking methods in Java

Map person = new HashMap();

person.put("name", "Joe Smith");String address = "123 Street";String house = address.substring(0, 3);person.put("houseNumber", house);

List children = Arrays.asList("Sue", "Tom");person.put("children", children);

Tuesday, August 10, 2010

Page 25: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Invoking methods in Objective-CNSMutableDictionary *person =

[NSMutableDictionary dictionary];

[person setObject:children forKey:@"children"];

[person setObject:@"Joe Smith" forKey:@"name"];

NSString *address = @"123 Street";NSString *house = [address substringWithRange:NSMakeRange(0, 3)];[person setObject:house forKey:@"houseNumber"];

NSArray *children = [NSArray arrayWithObjects:@"Sue", @"Tom", nil];

Tuesday, August 10, 2010

Page 26: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Memory Management

Tuesday, August 10, 2010

Page 27: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Memory Management

• Garbage collection available for Mac apps (10.5+)

• Use it if targeting 10.5+!

• Use explicit memory management if targeting <= 10.4

• No garbage collection on iOS due to performance concerns.

• Memory managed using simple reference counting mechanism:

• Higher level abstraction than malloc / free

• Straightforward approach, but must adhere to conventions and rules

Dude, where’s my Garbage Collection?

Tuesday, August 10, 2010

Page 28: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Memory Management

• Objective-C objects are reference counted• Objects start with reference count of 1

• Increased with retain

• Decreased with release, autorelease

• When count equals 0, runtime invokes dealloc

alloc1

retain2

release1

release0

Reference Counting

Tuesday, August 10, 2010

Page 29: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

• Implicitly retain any object you create using:Methods starting with "alloc", "new", or contains "copy"

e.g alloc, allocWithZone, newObject, mutableCopy,etc.

• If you've retained it, you must release it

• Many objects provide convenience methods that return an autoreleased reference.

• Holding object reference from these methods does not make you the retainer• However, if you retain it you need an

offsetting release or autorelease to free it

Memory ManagementUnderstanding the rules

Tuesday, August 10, 2010

Page 30: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

retain / release / autorelease@implementation BankAccount

- (NSString *)accountNumber { return [[accountNumber retain] autorelease];}

- (void)setAccountNumber:(NSString *)newNumber { if (accountNumber != newNumber) { [accountNumber release]; accountNumber = [newNumber retain]; }}

- (void)dealloc { [self setAccountNumber:nil]; [super dealloc];}

@endTuesday, August 10, 2010

Page 31: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties

Tuesday, August 10, 2010

Page 32: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties

• Object-C 2.0 introduced new syntax for defining accessor code

• Much less verbose, less error prone

• Highly configurable

• Automatically generates accessor code

• Compliments existing conventions and technologies• Key-Value Coding (KVC)

• Key-Value Observing (KVO)

• Cocoa Bindings

• Core Data

Simplifying Accessors

Tuesday, August 10, 2010

Page 33: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Interface

Attribute Impacts

readonly/readwrite Mutability

assign/retain/copy Storage

nonatomic Concurrency

setter/getter API

@property(attributes) type variable;

Tuesday, August 10, 2010

Page 34: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Interface

Attribute Impacts

readonly/readwrite Mutability

assign/retain/copy Storage

nonatomic Concurrency

setter/getter API

@property(attributes) type variable;

@property(readonly) NSString *acctNumber;

Tuesday, August 10, 2010

Page 35: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Interface

Attribute Impacts

readonly/readwrite Mutability

assign/retain/copy Storage

nonatomic Concurrency

setter/getter API

@property(attributes) type variable;

@property(readwrite, copy) NSString *acctNumber;

Tuesday, August 10, 2010

Page 36: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Interface

Attribute Impacts

readonly/readwrite Mutability

assign/retain/copy Storage

nonatomic Concurrency

setter/getter API

@property(attributes) type variable;

@property(nonatomic, retain) NSDate *activeOn;

Tuesday, August 10, 2010

Page 37: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Interface

Attribute Impacts

readonly/readwrite Mutability

assign/retain/copy Storage

nonatomic Concurrency

setter/getter API

@property(attributes) type variable;

@property(readonly, getter=username) NSString *name;

Tuesday, August 10, 2010

Page 38: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Interface@interface BankAccount : NSObject { NSString *accountNumber; NSDecimalNumber *balance; NSDecimalNumber *fees; BOOL accountCurrent;}

@property(readwrite, copy) NSString *accountNumber;@property(readwrite, retain) NSDecimalNumber *balance;@property(readonly) NSDecimalNumber *fees;@property(getter=isCurrent) BOOL accountCurrent;

@end

Tuesday, August 10, 2010

Page 39: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Interface

@interface BankAccount : NSObject {// Look ma, no more ivars

}

@property(readwrite, copy) NSString *accountNumber;@property(readwrite, retain) NSDecimalNumber *balance;@property(readonly) NSDecimalNumber *fees;@property(getter=isCurrent) BOOL accountCurrent;

@end

New in 10.6 and iOS 4

Tuesday, August 10, 2010

Page 40: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Implementation@implementation BankAccount

@synthesize accountNumber;@synthesize balance;@synthesize fees;@synthesize accountCurrent;

...

@end

Tuesday, August 10, 2010

Page 41: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Properties: Implementation

@implementation BankAccount

// no more @synthesize statements

...

@end

New in 10.6 and iOS 4

Tuesday, August 10, 2010

Page 42: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Accessing Properties• Generated properties are standard methods

• Accessed through normal messaging syntax

[object property];[object setProperty:(id)newValue];

• Objective-C 2.0 property access via dot syntaxobject.property;object.property = newValue;

Dot notation is just syntactic sugar. Still uses accessor methods. Doesn't get/set values directly.

Tuesday, August 10, 2010

Page 43: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Protocols

Tuesday, August 10, 2010

Page 44: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Protocols

• List of method declarations• Not associated with a particular class

• Conformance, not class, is important

• Useful in defining:• Methods that others are expected to

implement

• Declaring an interface while hiding its particular class

• Capturing similarities among classes that aren't hierarchically related

Java’s Interface done Objective-C style

Tuesday, August 10, 2010

Page 45: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Protocols

NSCoding NSCodingNSObject

Person Account

CheckingAccount SavingAccount

Tuesday, August 10, 2010

Page 46: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Defining a Protocol

@protocol NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder;- (id)initWithCoder:(NSCoder *)aDecoder;

@end

NSCoding from Foundation FrameworkObjective-C equivalent to java.io.Externalizable

Tuesday, August 10, 2010

Page 47: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Adopting a Protocol

@interface Person : NSObject <NSCoding> {NSString *name;NSString *street;NSString *city, *state, *zip;

}

// method declarations

@end

Tuesday, August 10, 2010

Page 48: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Conforming to a Protocol

@implementation Person

-(id)initWithCoder:(NSCoder *)coder { if (self = [super init]) { name = [coder decodeObjectForKey:@"name"]; [name retain]; } return self;}

-(void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:name forKey:@"name"];}

@end

Partial implementation of conforming Person class

Tuesday, August 10, 2010

Page 49: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

@required and @optional• Protocols methods are required by default• Can be relaxed with @optional directive

@protocol SomeProtocol

- (void)requiredMethod;

@optional- (void)anOptionalMethod;- (void)anotherOptionalMethod;

@required- (void)anotherRequiredMethod;

@end

Tuesday, August 10, 2010

Page 50: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Categories & Extensions

Tuesday, August 10, 2010

Page 51: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Categories

• Add new methods to existing classes• Alternative to subclassing

• Defines new methods and can override existing

• Does not define new instance variables

• Becomes part of the class definition

• Inherited by subclasses

• Can be used as organizational tool

• Often used in defining "private" methods

Extending Object Features

Tuesday, August 10, 2010

Page 52: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Using Categories@interface NSString (Extensions)-(NSString *)trim;@end

@implementation NSString (Extensions)-(NSString *)trim { NSCharacterSet *charSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; return [self stringByTrimmingCharactersInSet:charSet];}@end

NSString *string = @" A string to be trimmed ";NSLog(@"Trimmed string: '%@'", [string trim]);

Interface

Implementation

Usage

Tuesday, August 10, 2010

Page 53: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Class Extensions

• Objective-C 2.0 adds ability to define "anonymous" categories• Category is unnamed

• Treated as class interface continuations

• Useful for implementing required "private" API

• Compiler enforces methods are implemented

Unnamed Categories

Tuesday, August 10, 2010

Page 54: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Class Extensions

@interface Person : NSObject {NSString *name;}-(NSString *)name;

@end

Example

Tuesday, August 10, 2010

Page 55: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Class Extensions

@interface Person ()-(void)setName:(NSString *)newName;@end

@implementation Person- (NSString *)name { return name;}

- (void)setName:(NSString *)newName { name = newName;}@end

Example

Tuesday, August 10, 2010

Page 56: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Summary

Tuesday, August 10, 2010

Page 57: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Objective-C

• Fully C, Fully Object-Oriented

• Powerful dynamic runtime

• Objective-C 2.0 added many useful new features• Garbage Collection for Mac OS X apps

• Properties, Improved Categories & Protocols

• Objective-C 2.1 continues its evolution:• Blocks (Closures)

• Synthesize by default for properties

Summary

Tuesday, August 10, 2010

Page 58: Objective-C for Java Developers - Intertech for Java Developers  Tuesday, August 10, 2010. Objective-C Overview Tuesday, August 10, 2010. Objective-C ... • Cocoa Bindings

Questions?

Tuesday, August 10, 2010