Objective C

19
Objective-C vs. C++ / Java Studenckie Koło Naukowe “MacKN” Błażej Biesiada

description

Objective C

Transcript of Objective C

Page 1: Objective C

Objective-Cvs. C++ / Java

Studenckie Koło Naukowe “MacKN”Błażej Biesiada

Page 2: Objective C

Agenda

• Co to jest Obj-C?

• Historia

• Dlaczego Obj-c?

• Składnia

• Elementy charakterystyczne

• Co dalej?

Page 3: Objective C

Co to jest Obj-C?

• skrót od Objective-C ;-)

• rozszerzenie języka C

• obiektowość oparta na Smalltalku-u

• wykorzystywany głównie w Mac OS X

Page 4: Objective C

Historia

StepStone

1986 1988

NeXT

1993

GNU Obj-C

1996

Apple

2001

Mac OS X

2007

Obj-C 2.0

Page 5: Objective C

Dlaczego Obj-C?

• Cocoa

• iPhone OS

• Objective-C 2.0

• A dlaczego C++? Think different!

Page 6: Objective C

[obj msg] vs. obj.met()

[obiekt wiadomosc];

[obiekt wiadomosc: arg1: arg2: arg3];

[obiekt ustawSzerokosc: arg1 Wysokosc: arg2 Glebokosc: arg3];

Page 7: Objective C

*.m vs. *.cpp//ClassName.h

@interface ClassName : ItsSuperclass

{

float height;

BOOL filled;

NSColor *fillColor;

}

+ (id)alloc;

- (void)display;

@end

//ClassName.m

@implementation ClassName

+ (id)alloc {

//...

}

- (void)display {

//...

}

@end

Page 8: Objective C

val / setVal vs. getVal / setVal

• sekcje @private, @protected, @public

• domyślnie @protected

• Key Value Coding, czyli nie tylko konwencja

Page 9: Objective C

id vs. void

• id - typ wskaźnikowy

• void - brak wyniku funkcji

• NIL - obiekt używany zamiast NULL

Page 10: Objective C

@try vs. tryCup *cup = [[Cup alloc] init];

@try {

[cup fill];

}

@catch (NSException *exception) {

NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);

}

@finally {

[cup release];

}

Page 11: Objective C

protokoły vs. interfejsy

@protocol MyProtocol

- (void)requiredMethod;

@optional

- (void)anOptionalMethod;

- (void)anotherOptionalMethod;

@required

- (void)anotherRequiredMethod;

@end

@interface MyClass ( MyProtocol )

{

int myVal;

}

- (void)myOwnClassMethod;

@end

Page 12: Objective C

@synchronized() vs. synchronized

- (void)criticalMethod

{

@synchronized(self) {

// Critical code.

...

}

}

Page 13: Objective C

Inne różnice

• #import vs. #include

• self vs. this

• [super msg] vs. super.met()

Page 14: Objective C

Properties, czyli coś nowego

@interface MyClass : NSObject

{

NSString *value;

}

@property(copy, readwrite) NSString *value;

@end

@implementation MyClass

@synthesize value;

@end

Page 15: Objective C

Kategorie klas#import "ClassName.h"

@interface ClassName ( CategoryName )

// deklaracje metod

@end

#import "ClassName+CategoryName.h"

@implementation ClassName ( CategoryName )

// definicje metod

@end

Page 16: Objective C

Coś więcej?

• Rozszerzenia klas

• Fast enumeration

• współpraca z C++

Page 17: Objective C

Co dalej?

“The Objective-C 2.0 Programming Language”

http://developer.apple.com/

Page 18: Objective C

www.mackn.agh.edu.pl

Page 19: Objective C

Dziękuję za uwagę!