CocoaHeads
14 Mai 2013
mardi 28 mai 13
Backelite...
...recrute !mardi 28 mai 13
Au programme
• Accessors Vs Direct access to properties
• Design Pattern : Categories
• Design Pattern : Associative Storage
• La chaine Météo!
mardi 28 mai 13
iVar : Accessors/Direct access
• Performance ?
• Risks / Limits ?
mardi 28 mai 13
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
• 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
Design Pattern: Categories
• Add new methods to existing classes
#import "IBSHelper.h"
@interface IBSHelper (FileName)
+ (NSString*)filenameForGeolocData;
+ (NSString*)filenameForBrandCodeData;
+ (NSString*)filenameForTradData;
+ (NSString *)currentLanguage;
+ (NSString *)currentLanguageForDownload;
+ (NSURL*)urlForGeolocData;
+ (NSURL*)urlForBrandData;
+ (NSURL*)urlForTradData;
@end
mardi 28 mai 13
Design Pattern: Categories
• Organize/split complexity
mardi 28 mai 13
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
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
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
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
• 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
La chaine meteo
Issue : How to add a new property on UINavigationBar
• Subclass UINavigationBar, dumbass !
• Use associated storage !
mardi 28 mai 13
La chaine meteo
mardi 28 mai 13
Other examples
• UIView + BkBadge
• UIView + BkLoader
....
mardi 28 mai 13
Thanks
mardi 28 mai 13

Accessors Vs Direct access to properties & Design Pattern

  • 1.
  • 2.
  • 3.
    Au programme • AccessorsVs Direct access to properties • Design Pattern : Categories • Design Pattern : Associative Storage • La chaine Météo! mardi 28 mai 13
  • 4.
    iVar : Accessors/Directaccess • Performance ? • Risks / Limits ? 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 affectationdon’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
  • 7.
    Design Pattern: Categories •Add new methods to existing classes #import "IBSHelper.h" @interface IBSHelper (FileName) + (NSString*)filenameForGeolocData; + (NSString*)filenameForBrandCodeData; + (NSString*)filenameForTradData; + (NSString *)currentLanguage; + (NSString *)currentLanguageForDownload; + (NSURL*)urlForGeolocData; + (NSURL*)urlForBrandData; + (NSURL*)urlForTradData; @end mardi 28 mai 13
  • 8.
    Design Pattern: Categories •Organize/split complexity 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: Associativereferences 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: Associativereferences //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 wayto 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
  • 15.
  • 16.
    Other examples • UIView+ BkBadge • UIView + BkLoader .... mardi 28 mai 13
  • 17.