Share
Embed
From Java to Objective-C
Simple talk about differences between Java and Objective-C syntax.
kenji
April 08, 2014
Transcript
19 slides
-
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 -
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 -
Fichiers • .java -- .class • .h / .m --
.o • .jar • .a / .dylib 5 mardi 8 avril 2014 -
Point d’entrée 7 int main(int argc, char **argv) { return
0; } public static void main(String[] args) { } mardi 8 avril 2014 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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