Upload
konryd
View
2.647
Download
5
Tags:
Embed Size (px)
DESCRIPTION
Objective C
Citation preview
Objective-Cvs. C++ / Java
Studenckie Koło Naukowe “MacKN”Błażej Biesiada
Agenda
• Co to jest Obj-C?
• Historia
• Dlaczego Obj-c?
• Składnia
• Elementy charakterystyczne
• Co dalej?
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
Historia
StepStone
1986 1988
NeXT
1993
GNU Obj-C
1996
Apple
2001
Mac OS X
2007
Obj-C 2.0
Dlaczego Obj-C?
• Cocoa
• iPhone OS
• Objective-C 2.0
• A dlaczego C++? Think different!
[obj msg] vs. obj.met()
[obiekt wiadomosc];
[obiekt wiadomosc: arg1: arg2: arg3];
[obiekt ustawSzerokosc: arg1 Wysokosc: arg2 Glebokosc: arg3];
*.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
val / setVal vs. getVal / setVal
• sekcje @private, @protected, @public
• domyślnie @protected
• Key Value Coding, czyli nie tylko konwencja
id vs. void
• id - typ wskaźnikowy
• void - brak wyniku funkcji
• NIL - obiekt używany zamiast NULL
@try vs. tryCup *cup = [[Cup alloc] init];
@try {
[cup fill];
}
@catch (NSException *exception) {
NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
}
@finally {
[cup release];
}
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
@synchronized() vs. synchronized
- (void)criticalMethod
{
@synchronized(self) {
// Critical code.
...
}
}
Inne różnice
• #import vs. #include
• self vs. this
• [super msg] vs. super.met()
Properties, czyli coś nowego
@interface MyClass : NSObject
{
NSString *value;
}
@property(copy, readwrite) NSString *value;
@end
@implementation MyClass
@synthesize value;
@end
Kategorie klas#import "ClassName.h"
@interface ClassName ( CategoryName )
// deklaracje metod
@end
#import "ClassName+CategoryName.h"
@implementation ClassName ( CategoryName )
// definicje metod
@end
Coś więcej?
• Rozszerzenia klas
• Fast enumeration
• współpraca z C++
Co dalej?
“The Objective-C 2.0 Programming Language”
http://developer.apple.com/
www.mackn.agh.edu.pl
Dziękuję za uwagę!