Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Intro to Obj-C 
Design Patterns 
Or how I learned to be less bad 
Haris Amin
GLIMPSE 
A fun way to meet 
new people through 
Instagram
Design Patterns 
• Why do we care? 
• Let’s explore two design patterns in 
Cocoa 
1. Notification Center 
2. Delegates
Notification Center 
• NSNotificationCenter class 
• Easiest to learn (IMHO) but also easiest to 
abuse 
• Very flexible
Notification Center 
• Let’s Grab a NotificationCenter ‘instance’ 
[NSNotificationCenter defaultCenter] 
! 
• Then PUBLISH...
Publish Message Without Content 
• We want to notify that something specific 
‘happened’ 
• Method Signature 
- (void)post...
Publish Message Without Content 
- (void)postNotificationName:(NSString 
*)notificationName object:(id)notificationSender ...
Publish Message With Content 
- (void)postNotificationName:(NSString 
*)notificationName object:(id)notificationSender 
us...
Subscribing to a Message 
- (void)addObserver:(id)notificationObserver 
selector:(SEL)notificationSelector name:(NSString ...
Notification Center… 
THE GOOD 
• Really flexible 
• Don’t have to define any sort of protocol for your 
messages 
• You c...
Notification Center… 
THE BAD 
• TOO flexible?! 
• Don’t have to define any sort of protocol for your 
messages 
• You can...
Delegates 
• Very solid pattern 
• Vigorously multitude of Cocoa classes 
(UITableView, UICollectionView, UIAlterView.. 
e...
A Personal Note… 
• I consumed delegates without really 
understanding how they work 
• I used to be scared of trying to i...
TIME TO GAIN A 
SUPERPOWER!
Delegates 
• Very solid pattern 
• Vigorously multitude of Cocoa classes 
(UITableView, UICollectionView, UIAlterView.. 
e...
Define a Delegate 
Protocol 
• Very solid pattern 
• Vigorously multitude of Cocoa classes 
(UITableView, UICollectionView...
Define Protocol 
@protocol VideoDelegate; 
! 
@interface Video: NSObject 
! 
@property (nonatomic, weak) id<VideoDelegate>...
Delegating Methods in 
the Protocol 
• In our Video class, where need be, we will 
delegate respective messages 
@implemen...
Implement Protocol 
#include "Video.h" // needed to include the @delegate protocol 
info 
! 
@interface VideoPlayer : UIVi...
A parting note… 
with great SUPER power 
comes great SUPER 
responsibility!
Questions? 
HARIS AMIN 
Cofounder/CO-CTO @itsglimpse 
@harisamin
Upcoming SlideShare
Loading in …5
×

Intro to Obj-C Design Patterns or Or how I learned to be less bad

479 views

Published on

Presented at A Swift Start on 08/29/2014:
A one day iOS community conference discussing
the ups and downs of learning iOS. Beginners and pros invited

An intro to two Objc-C/Cocoa design patterns: Notification Center and Delegates. The talk focuses on learning multiple ways to solve the problem while avoiding the 'chainsaw massacre' of one powerful approach over another.

Published in: Engineering
  • Be the first to comment

Intro to Obj-C Design Patterns or Or how I learned to be less bad

  1. 1. Intro to Obj-C Design Patterns Or how I learned to be less bad Haris Amin
  2. 2. GLIMPSE A fun way to meet new people through Instagram
  3. 3. Design Patterns • Why do we care? • Let’s explore two design patterns in Cocoa 1. Notification Center 2. Delegates
  4. 4. Notification Center • NSNotificationCenter class • Easiest to learn (IMHO) but also easiest to abuse • Very flexible
  5. 5. Notification Center • Let’s Grab a NotificationCenter ‘instance’ [NSNotificationCenter defaultCenter] ! • Then PUBLISH a specific message (2 ways) 1. With content 2. Without Content
  6. 6. Publish Message Without Content • We want to notify that something specific ‘happened’ • Method Signature - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender
  7. 7. Publish Message Without Content - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender • E.G. The Video’s current time just updated [[NSNotificationCenter defaultCenter] postNotificationName:@"TimeUpdated" object:self];
  8. 8. Publish Message With Content - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo • E.G. The Video’s current time just updated WITH the actual current time [[NSNotificationCenter defaultCenter] postNotificationName:@"TimeUpdated" object:self userInfo:@{@"currentTime": 20}];
  9. 9. Subscribing to a Message - (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender • In our VideoPlayer class we will just subscribe to the NotificationCenter Message [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTimeUpdate:) name:@"TimeUpdated" object:nil]; - (void)handleTimeUpdate:(NSNotification *)note{ // note.userInfo is the CONTENT we passed // ... }
  10. 10. Notification Center… THE GOOD • Really flexible • Don’t have to define any sort of protocol for your messages • You can subscribe and publish messages in any number of places
  11. 11. Notification Center… THE BAD • TOO flexible?! • Don’t have to define any sort of protocol for your messages • You can subscribe and publish messages in any number of of places
  12. 12. Delegates • Very solid pattern • Vigorously multitude of Cocoa classes (UITableView, UICollectionView, UIAlterView.. etc.) • You have to define the protocol of the messages you send
  13. 13. A Personal Note… • I consumed delegates without really understanding how they work • I used to be scared of trying to implement one myself
  14. 14. TIME TO GAIN A SUPERPOWER!
  15. 15. Delegates • Very solid pattern • Vigorously multitude of Cocoa classes (UITableView, UICollectionView, UIAlterView.. etc.) • You have to define the protocol of the messages you send
  16. 16. Define a Delegate Protocol • Very solid pattern • Vigorously multitude of Cocoa classes (UITableView, UICollectionView, UIAlterView.. etc.) • You have to define the protocol of the messages you send
  17. 17. Define Protocol @protocol VideoDelegate; ! @interface Video: NSObject ! @property (nonatomic, weak) id<VideoDelegate> delegate; ! @end ! @protocol VideoDelegate <NSObject> @required //... ! @optional -(void)video:(Video *)video updatedTime:(NSUInteger)currentTime; ! @end // end of delegate protocol
  18. 18. Delegating Methods in the Protocol • In our Video class, where need be, we will delegate respective messages @implementation Video //.. ! if (self.delegate && [self.delegate respondsToSelector:@selector(video:updatedTime)]) { [self.delegate video:updatedTime]; } //.. @end
  19. 19. Implement Protocol #include "Video.h" // needed to include the @delegate protocol info ! @interface VideoPlayer : UIViewController <VideoDelegate> @end ! @implementation VideoPlayer //.. ! -(void)video:(Video *)video updatedTime:(NSUInteger)currentTime{ // HURRAY! We know what the currentTime is :) } ! //.. @end
  20. 20. A parting note… with great SUPER power comes great SUPER responsibility!
  21. 21. Questions? HARIS AMIN Cofounder/CO-CTO @itsglimpse @harisamin

×