Más contenido relacionado Similar a CocoaHeads Rennes #3 : Bien débuter sur iOS (20) Más de CocoaHeadsRNS (16) CocoaHeads Rennes #3 : Bien débuter sur iOS5. Objective-C : protocole
@protocol SubscriptionListener <Listener>
@required
- (void)didSubscribe;
@optional
- (void)didUnsubscribe;
@end
6. Objective-C : protocole
@interface Employee : Person <SubscriptionListener> {
}
@end
@implementation Employee
- (void)didSubscribe {
...
}
@end
7. rq1-> comparaison Java
Objective-C : message
(importance des
étiquettes)
+ (Color*)redColor;
Color* myColor = [Color redColor];
- (Color*)colorByAddingBlue:(float)blueValue;
myColor = [myColor colorByAddingBlue:0.2];
Color* color = Color.createColor(0.2, 0.4, 0.7, 0.6);
+ (Color*)createColorRed:(float)red green:(float)green blue:(float)blue alpha:(float)alpha;
Color* color = [Color createColorRed:0.2 green:0.4 blue:0.7 alpha:0.6];
[[Color redColor] colorByAddingBlue:0.2];
9. Objective-C : création
d’objet
• Allocation mémoire : alloc
• Initialisation : init
Person *john = [[Person alloc] init];
10. Objective-C :
initialiseur
- (id)initWithIndex:(int)index {
self = [super init];
if (self) {
index_ = index;
}
return self;
}
11. Objective-C :
gestion mémoire
• Compteur de références : retainCount
• alloc, copy, retain : +1
• release, autorelease : -1
employee
2
• 0 : dealloc
boss1 boss2
12. Objective-C :
gestion mémoire
“ You are responsible for the objects you own ”
alloc/copy/retain release/autorelease
13. Gestion mémoire :
variable locale
- (void)doSomeWork {
Job *boringJob = [[Job alloc] init];
...
[boringJob release];
}
- (void)drinkHotBeverage {
Cofee *cofee = [[[Cofee alloc] init] autorelease];
...
}
14. Gestion mémoire :
variable d’instance
- (id)initWithBoss:(Boss*)boss {
self = [super init];
if (self) {
boss_ = [boss retain];
}
return self;
}
- (void)dealloc {
[boss_ release];
[super dealloc];
}
15. Objective-C : getter &
setter
@interface Employee : Person {
float salary_;
Boss *boss_;
}
@property (assign) float salary;
@property (retain) Boss *boss;
@end
16. Objective-C : getter &
setter
@implementation Employee
@synthesize salary = salary_;
@synthesize boss = boss_;
@end
17. Développer sur iOS :
API
• CocoaTouch
• Descendant de Cocoa
• Design MVC
• Foundation Kit
• UIKit
• CoreData
18. Développer sur iOS :
outils
• XCode
• InterfaceBuilder
• GCC/GCD, LLVM/LLDB
• Instruments
19. CocoaTouch par
l’exemple (1) : vue
• UIView
• Dessin
• Hiérarchie
• Interaction
• Customisation
• Sous-classes : UILabel, UIButton, etc.
21. CocoaTouch par
l’exemple (2) : contrôleur
• UIViewController
• Gestion d’une hiérarchie de vues
• Médiation entre données et vues
• Logique de navigation
• Sous-classes : UINavigationController,
UITableViewController, etc.
23. Must know-Must read
• Documentation & Forum Apple
• Forum PommeDev
• StackOverflow
• CocoaHeads
• “ Beginning iPhone Development :
exploring the iPhone SDK ”, APress
jeremy.brault@niji.fr