Accessors Vs Direct access to properties & Design Pattern

  • 3,500 views
Uploaded on

Accessors Vs Direct access to properties …

Accessors Vs Direct access to properties
Design Pattern : Categories
Design Pattern : Associative Storage
La chaine Météo!

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
3,500
On Slideshare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
5
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. CocoaHeads14 Mai 2013mardi 28 mai 13
  • 2. Backelite......recrute !mardi 28 mai 13
  • 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
  • 4. iVar : Accessors/Direct access• Performance ?• Risks / Limits ?mardi 28 mai 13
  • 5. property Using setter (ms)Using Direct Access(ms)strong + nonatomic 9 6weak + nonatomic 30 30unsafe_unretained +nonatomic0,8 0,2strong + atomic 9 6iPhone 3GS/iOS5 + 10,000 affectactions. (average basedon several tests.)iVar : Accessors/Direct accessPerformance?mardi 28 mai 13
  • 6. • Direct affectation don’t use the propertyattributes (atomic)• KVO/KVC needs accessors,• Don’t use accessor methods in init methodsand dealloc (You may have override youraccessors)iVar : Accessors/Direct accessRisk / 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;@endmardi 28 mai 13
  • 8. Design Pattern: Categories• Organize/split complexitymardi 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 *)controlleranimated:(BOOL)animated;@endmardi 28 mai 13
  • 10. Design Pattern: Categories• Categories can be used to declare either instancemethods or class methods but are not usuallysuitable 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;@endmardi 28 mai 13
  • 11. Design Pattern: Associative referenceshttp://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008048Objective-C programs interact with the runtime system at three distinctlevels: through Objective-C source code; through methods defined in theNSObject class of the Foundation framework; and through direct calls toruntime 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.hOBJC_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 anexisting class !Design Pattern: Associative referencesstatic 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];}}@endmardi 28 mai 13
  • 14. La chaine meteoIssue : How to add a new property on UINavigationBar• Subclass UINavigationBar, dumbass !• Use associated storage !mardi 28 mai 13
  • 15. La chaine meteomardi 28 mai 13
  • 16. Other examples• UIView + BkBadge• UIView + BkLoader....mardi 28 mai 13
  • 17. Thanksmardi 28 mai 13