Slide 1

Slide 1 text

From Java to Objective-C R. Bergoin mardi 8 avril 2014

Slide 2

Slide 2 text

Histoire • Java 1.0 : 1996 • Sun Microsystems • Oracle Corp (2010) 2 • ObjC 1.0 : 1983 • Productivity Products International • NeXT (1988) • Apple (1996) mardi 8 avril 2014

Slide 3

Slide 3 text

Popularité 3 mardi 8 avril 2014

Slide 4

Slide 4 text

Comparaison rapide • Java • compilé : javac • interprété : jvm • portable • Vim / Eclipse / Netbeans 4 • ObjC • compilé : clang / gcc • runtime (fonctions C) • OS X / iOS • Vim / Xcode / AppCode mardi 8 avril 2014

Slide 5

Slide 5 text

Fichiers • .java -- .class • .h / .m -- .o • .jar • .a / .dylib 5 mardi 8 avril 2014

Slide 6

Slide 6 text

Logo 6 mardi 8 avril 2014

Slide 7

Slide 7 text

Logo 6 mardi 8 avril 2014

Slide 8

Slide 8 text

Point d’entrée 7 int main(int argc, char **argv) { return 0; } public static void main(String[] args) { } mardi 8 avril 2014

Slide 9

Slide 9 text

Point d’entrée 7 public static void main(String[] args) { // Ne plante pas => renvoie void } int main(int argc, char **argv) { return 0; } public static void main(String[] args) { } mardi 8 avril 2014

Slide 10

Slide 10 text

Ville.java 8 public class Ville { String nom; String codeInsee; String codePostal; public Ville(String nom, String codeInsee, String codePostal) { this.nom = nom; this.codeInsee = codeInsee; this.codePostal = codePostal; } public String getNom() {return this.nom;} private void setNom(String aNom) {this.nom = aNom;} // idem codeInsee/codePostal } mardi 8 avril 2014

Slide 11

Slide 11 text

Ville.h 9 @interface Ville : NSObject @property (nonatomic, readonly) NSString *nom; @property (nonatomic, readonly) NSString *codeInsee; @property (nonatomic, readonly) NSString *codePostal; - (instancetype)initWithNom:(NSString *)nom codeInsee:(int)codeInsee andCodePostal:(int)codePostal; @end mardi 8 avril 2014

Slide 12

Slide 12 text

Ville.m 10 @interface Ville () @property (nonatomic, strong) NSString *nom; @property (nonatomic, strong) NSString *codeInsee; @property (nonatomic, strong) NSString *codePostal; @end @implementation Ville - (instancetype)initWithNom:(NSString *)nom codeInsee:(int)codeInsee andCodePostal:(int)codePostal { self.nom = nom; self.codeInsee = codeInsee; self.codePostal = codePostal; } @end mardi 8 avril 2014

Slide 13

Slide 13 text

Créer une ville 11 Ville v = new Ville("Clermont-ferrand", "63000", "63113"); Ville *v = [[Ville alloc] initWithNom:@"Clermont-ferrand" codeInsee:@"63113" andCodePostal:@"63000"]; mardi 8 avril 2014

Slide 14

Slide 14 text

Le code Java est buggué ! 12 Ville v = new Ville("Clermont-ferrand", "63000", "63113"); Ville *v = [[Ville alloc] initWithNom:@"Clermont-ferrand" codeInsee:@"63113" andCodePostal:@"63000"]; mardi 8 avril 2014

Slide 15

Slide 15 text

Le code Java est buggué ! 12 Ville *v = [[Ville alloc] initWithNom:@"Clermont-ferrand" codeInsee:@"63113" andCodePostal:@"63000"]; Ville v = new Ville("Clermont-ferrand", "63113", "63000"); mardi 8 avril 2014

Slide 16

Slide 16 text

Appels de méthodes (envoi de messages) 13 Ville *v = [[Ville alloc] initWithNom:@"Clermont-ferrand" codeInsee:@"63113" andCodePostal:@"63000"]; Ville v = new Ville("Clermont-ferrand", "63113", "63000"); String nom = v.getNom().toUpperCase(); NSString *upNom = [[v nom] uppercaseString]; mardi 8 avril 2014

Slide 17

Slide 17 text

Libérer la mémoire 14 Ville v = new Ville("Clermont-ferrand", "63000", "63113"); Ville *v = [[Ville alloc] initWithNom:@"Clermont-ferrand" codeInsee:@"63113" andCodePostal:@"63000"]; [v release]; // Avant ARC (rien après, merci clang :-) v = null; Runtime.getRuntime().gc(); // mais quand... String nom = v.getNom().toUpperCase(); NSString *upNom = [[v nom] uppercaseString]; mardi 8 avril 2014

Slide 18

Slide 18 text

Plus... • @autoreleasepool (gestion mémoire) • nil (réponds à tous messages) • strong / weak • @protocol (interface au sens “objet” en fr.) • named class extensions : @interface Ville (Additions) ... @end • Blocks (lambda) : • @selector(setNom:) (ptr de méthode : SEL) 15 int (^mul)(int, int) = ^(int a, int b) { return a*b }; mardi 8 avril 2014

Slide 19

Slide 19 text

Plus... • https://developer.apple.com/ • ProgrammingWithObjectiveC.pdf • http://nshipster.com/ • http://www.objc.io/ 16 mardi 8 avril 2014