class: center, middle # Objective-C #### Un aperçu historique .footnote[[github.com/n-b/objc-history](http://github.com/n-b/objc-history)] -- *(Partiel et subjectif)* --- class: center ## C & SmallTalk -- ## Brad Cox & Tom Love -- ## Stepstone -- ## gcc -- ## NeXT -- ##### NeXTSTEP, OPENSTEP, GnuStep -- ## Apple -- ## clang --- class: center, middle # NeXT *“How much does Stepstone charge for the PC version of Obj-C ?
And, does it run on a 286 ?”* *comp.sys.next.programmer, 1992* --- layout: true ## NeXT --- #### Style La plupart des méthodes retournent `self`. @interface SomeClass - (id) doThis; - (id) doThat; @end ... [[someObject doThis] doThat]; --- #### Style id est le type par défaut. @interface SomeClass - method: arg; @end @implementation SomeClass - method: arg { // return someObject; } @end --- #### “Cocoa” * Interface Builder et AppKit * (Xcode s’appelle Project Builder) * IBOutlets and IBActions * Target-Action * Responder Chain --- #### NeXTSTEP 4.0, 1994 * Préfixe `NS` * Gestion “Semi-Automatique” de la mémoire - (id) retain; - (void) release; - (id) autorelease; -- * Sur GnuStep, les `Object`s ont une méthode `free`, comme en c++. --- #### Foundation * NSSelectorFromString, NSClassFromString, etc. * NSInvocation * NSProxy --- #### Enterprise Objects Framework * Key-Value Coding via le protocole `EOFKeyValueCoding` - (void) valueForKey:(NSString*)key; - (void) setValue:(id)value forKey:(NSString*)key; * WebObjects * Renaîtra sur Mac en tant que **CoreData**. --- layout:false class: center, middle # Apple --- layout: true ## Apple --- Rachat de NeXT en 1997. -- *“Cocoa is [now] owned by Apple, and the indications are that Apple is going to either completely drop its support for ObjC in favor of Java, or at least de-emphasize its use to the point where nobody would want to even look at it (in fact, this "death by default" has already happened).”* comp.sys.next.programmer, Avril 2000. -- Apple a plusieurs plans : * Carbon et CoreFoundation -- * JavaCocoa --- #### Toll-Free Bridging (Mac OS X 10.0) NSString * <=> CFStringRef -- `objc_getfutureclass` : Used by CoreFoundation's toll-free bridging. Do not call this function yourself. --- #### Exceptions Modernes (Mac OS X 10.3) NS_DURING [[NSException exception] raise]; NS_HANDLER // handle it NS_ENDHANDLER -- @try @throw exception @catch // handle it @finally --- #### Key-Value Observing et Bindings (Mac OS X 10.3) [object addObserver:self forKeyPath:@"someKey" options:0 context:NULL]; -- `objc_duplicateclass` : Used by Foundation's Key-Value Observing. Do not call this function yourself. --- ## CoreData (Mac OS X 10.4) #### Key-Value Coding sur les Collections [array valueForKey:@"someValue"] -- #### Opérateurs KVC sur les Collections [persons valueForKeyPath:@"@avg.age"]; [array valueForKey:@"someValue"] --- layout: false class: center, middle # Mac OS X 10.5 # Objective-C 2.0 --- layout:true ## Objective-C 2.0 --- #### Garbage Collector D’après Apple, “State of the art”. --- #### Notation pointée [obj setFoo:value] devient obj.foo = value; -- #### Properties - (Foo*) foo; - (void) setFoo:(Foo*)foo; devient @property Foo* foo; --- #### Fast Enumeration -- NSEnumerator * e NSEnumerator * enumerator = [array objectEnumerator]; NSObject * object = nil; while ( (object = [enumerator nextObject]) != nil ) { ... } -- for( NSObject* object in array) { ... } --- #### “Modern runtime” Uniquement sur Mac 64-bit et sur iOS. **Non-fragile ABI** @defs et sizeof(NSArray) n’ont plus de sens. --- #### Bonus * JavaCocoa deprecated. -- * Bridges du runtime avec d’autres languages : rubyCocoa, python (pyObjC), AppleScript… -- * `@optional` methods in Protocols. -- * Class Extentions (“catégorie anonyme”) // SomeClass.m @interface SomeClass(PrivateMethods) ... @end // devient // SomeClass.m @interface Class() ... @end --- layout:false class: center,middle # Clang --- layout:true ## Clang --- #### Clang Static Analyzer (Xcode 3) Détection automatique de leaks et de bugs, basée sur les conventions du language. --- #### Blocks (“Objective-C 2.1”) Ajout à `C`, mais les blocks sont utilisables comme des objets Objective-C. -- __block BOOL found = NO; NSSet *aSet = [NSSet setWithObjects: @"Alpha", @"Beta", @"Gamma", @"X", nil]; NSString *string = @"gamma"; [aSet enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { if ([obj localizedCaseInsensitiveCompare:string] == NSOrderedSame) { *stop = YES; found = YES; } }]; // At this point, found == YES --- layout:true ## LLVM Compiler 3.0 --- #### Private Instance Variables // SomeClass.h @interface SomeClass : NSObject { int foo; id bar; } ... @end devient // SomeClass.m @interface SomeClass () { int foo; id bar; } ... @end --- #### Private Instance Variables // SomeClass.h @interface SomeClass : NSObject { int foo; id bar; } ... @end ou même @implementation SomeClass { int foo; id bar; } ... @end --- #### ARC -- * Garbage Collector deprecated -- * `retain` et `release` sont intégrés au language. -- * `objc_method_family` -- * `weak` pointers mis à nil automatiquement --- layout:true ## LLVM Compiler 4.0 --- #### `@synthesis` automatique des ivars des properties --- #### Boxing [NSNumber numberWithDouble:M_PI/2.0] devient @(M_PI/2.0) --- #### Container literals pour `NSArray` et `NSDictionary [NSArray arrayWithObject:obj1, obj2, obj3, nil]; devient @[obj1, obj2, obj3] --- #### Subscripting [dictionary objectForKey:@"key"]; devient dictionary[@"key"] --- #### Bonus * Mot clé `instancetype` @interface A + (instancetype)constructAnA; @end -- [[NSMutableArray new] addObject:someObject]; -- * Function Overloading in C float __attribute__((overloadable)) tgsin(float x) { return sinf(x); } double __attribute__((overloadable)) tgsin(double x) { return sin(x); } -- * CoreFoundation “compatible” avec ARC : `arc_cf_code_audited`. --- layout: true # Futur ? --- * Appel à "`super`" automatique, sans respécifier les arguments. (par exemple pour viewDidAppear:) -- * `-[NSString stringWithFormat:]` plus court -- * Boxing pour NSValue
`[NSValue valueWithCGPoint:CGPointMake(10,20)];`
`@( CGPointMake(10,20) )` -- * Namespaces -- * Attribut de méthode pour le *designated initializer* -- * Méthodes protected et marquées pour réimplémentation -- * Valeurs de paramètres par défaut - (void) doSomething; - (void) doSomethingWithStuff:(Stuff*)s; --- layout:false # Links * [comp.sys.next.programmer](https://groups.google.com/forum/?fromgroups=#!topic/comp.sys.next.programmer/NUXThBOc360) * [Objective-C Feature Availability Index](http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/ObjCAvailabilityIndex/_index.html)
* [The OPENSTEP Development Environment](http://objc.toodarkpark.net/introobj.html#712) * [MacTECH : C++ Versus Objective-C](http://www.mactech.com/articles/mactech/Vol.13/13.03/CandObjectiveCCompared/index.html) * [GnuStep ObjectiveC Book](http://www.gnustep.org/resources/documentation/ObjectivCBook.pdf) * [History of Cocoa and Objective-C](http://www.cocoacast.com/cocoacast/?q=node/14) * [Objective-C Runtime Reference](http://developer.apple.com/library/ios/ipad/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html) * [Clang Language Extensions](http://clang.llvm.org/docs/LanguageExtensions.html#objc_features)