3. Au programme
• Accessors Vs Direct access to properties
• Design Pattern : Categories
• Design Pattern : Associative Storage
• La chaine Météo!
mardi 28 mai 13
5. property Using setter (ms)
Using Direct Access
(ms)
strong + nonatomic 9 6
weak + nonatomic 30 30
unsafe_unretained +
nonatomic
0,8 0,2
strong + atomic 9 6
iPhone 3GS/iOS5 + 10,000 affectactions. (average based
on several tests.)
iVar : Accessors/Direct access
Performance?
mardi 28 mai 13
6. • Direct affectation don’t use the property
attributes (atomic)
• KVO/KVC needs accessors,
• Don’t use accessor methods in init methods
and dealloc (You may have override your
accessors)
iVar : Accessors/Direct access
Risk / Limits?
mardi 28 mai 13
9. Design Pattern: Categories
• Makes easy to support multiple OS versions
@interface UITableView (BkUICore)
- (void) bkRegisterNibName:(NSString *)nibName forCellReuseIdentifier:
(NSString *)identifier;
- (id) bkDequeueReusableCellWithIdentifier:(NSString *)identifier;
@end
@interface UIViewController (BkUICore)
- (void) bkDismissModalViewControllerAnimated:(BOOL)animated;
- (void) bkPresentViewController:(UIViewController *)controller
animated:(BOOL)animated;
@end
mardi 28 mai 13
10. Design Pattern: Categories
• Categories can be used to declare either instance
methods or class methods but are not usually
suitable for declaring additional properties.
One other design pattern can help us !
Associated storage
@interface UITableView (BkUICore)
- (void) bkRegisterNibName:(NSString *)nibName forCellReuseIdentifier:
(NSString *)identifier;
- (id) bkDequeueReusableCellWithIdentifier:(NSString *)identifier;
@end
mardi 28 mai 13
11. Design Pattern: Associative references
http://developer.apple.com/library/mac/#documentation/
Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/
Introduction.html#//apple_ref/doc/uid/TP40008048
Objective-C programs interact with the runtime system at three distinct
levels: through Objective-C source code; through methods defined in the
NSObject class of the Foundation framework; and through direct calls to
runtime functions.
mardi 28 mai 13
12. Design Pattern: Associative references
//nsobjet.h
@interface NSObject
+ (Class)superclass;
+ (Class)class;
- (BOOL)respondsToSelector:(SEL)aSelector;
+ (BOOL)conformsToProtocol:(Protocol *)protocol;
//runtime.h
OBJC_EXPORT Class class_getSuperclass(Class cls)
OBJC_EXPORT Class object_getClass(id obj)
OBJC_EXPORT BOOL class_respondsToSelector(Class cls, SEL sel)
OBJC_EXPORT BOOL class_conformsToProtocol(Class cls, Protocol *protocol)
Some of the NSObject methods simply query the runtime system for information.
These methods allow objects to perform introspection.
mardi 28 mai 13
13. • A way to add simulate new properties to an
existing class !
Design Pattern: Associative references
static const char nibsAssocKey;
@implementation UITableView (BkUICore)
- (void) bkRegisterNibName:(NSString *)nibName forCellReuseIdentifier:(NSString
*)identifier
{
if ([self respondsToSelector:@selector(registerNib:forCellReuseIdentifier:)]) {
[self registerNib:[UINib nibWithNibName:nibName bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:identifier]; //NS_AVAILABLE_IOS(5_0)
} else {
NSMutableDictionary *nibs = objc_getAssociatedObject(self, &nibsAssocKey);
if (nil == nibs) {
nibs = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &nibsAssocKey,
nibs, OBJC_ASSOCIATION_RETAIN);
}
[nibs setValue:nibName forKey:identifier];
}
}
@end
mardi 28 mai 13
14. La chaine meteo
Issue : How to add a new property on UINavigationBar
• Subclass UINavigationBar, dumbass !
• Use associated storage !
mardi 28 mai 13