intro to obj-c design patterns or or how i learned to be less bad
TRANSCRIPT
![Page 1: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/1.jpg)
Or how I learned to be less bad
Intro to Obj-C Design Patterns
Haris Amin
![Page 2: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/2.jpg)
GLIMPSE
A fun way to meet new people through
![Page 3: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/3.jpg)
Design Patterns• Why do we care?
• Let’s explore two design patterns in Cocoa
1. Notification Center
2. Delegates
![Page 4: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/4.jpg)
Notification Center
• NSNotificationCenter class
• Easiest to learn (IMHO) but also easiest to abuse
• Very flexible
![Page 5: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/5.jpg)
Notification Center
• Let’s Grab a NotificationCenter ‘instance’
!
• Then PUBLISH a specific message (2 ways)
[NSNotificationCenter defaultCenter]
1. With content
2. Without Content
![Page 6: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/6.jpg)
Publish Message Without Content
• We want to notify that something specific ‘happened’
• Method Signature
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender
![Page 7: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/7.jpg)
Publish Message Without Content
• E.G. The Video’s current time just updated
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender
[[NSNotificationCenter defaultCenter] postNotificationName:@"TimeUpdated" object:self];
![Page 8: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/8.jpg)
Publish Message With Content
• E.G. The Video’s current time just updated WITH the actual current time
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo
[[NSNotificationCenter defaultCenter] postNotificationName:@"TimeUpdated" object:self userInfo:@{@"currentTime": 20}];
![Page 9: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/9.jpg)
Subscribing to a Message
• In our VideoPlayer class we will just subscribe to the NotificationCenter Message
- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTimeUpdate:) name:@"TimeUpdated" object:nil];
- (void)handleTimeUpdate:(NSNotification *)note{ // note.userInfo is the CONTENT we passed // ... }
![Page 10: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/10.jpg)
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
![Page 11: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/11.jpg)
• 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
Notification Center… THE BAD
![Page 12: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/12.jpg)
Delegates
• Very solid pattern
• Vigorously multitude of Cocoa classes (UITableView, UICollectionView, UIAlterView.. etc.)
• You have to define the protocol of the messages you send
![Page 13: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/13.jpg)
A Personal Note…
• I consumed delegates without really understanding how they work
• I used to be scared of trying to implement one myself
![Page 14: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/14.jpg)
TIME TO GAIN A SUPERPOWER!
![Page 15: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/15.jpg)
Delegates
• Very solid pattern
• Vigorously multitude of Cocoa classes (UITableView, UICollectionView, UIAlterView.. etc.)
• You have to define the protocol of the messages you send
![Page 16: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/16.jpg)
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
![Page 17: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/17.jpg)
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
![Page 18: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/18.jpg)
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
![Page 19: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/19.jpg)
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
![Page 20: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/20.jpg)
A parting note…
with great SUPER power comes great SUPER
responsibility!
![Page 21: Intro to Obj-C Design Patterns or Or how I learned to be less bad](https://reader033.vdocument.in/reader033/viewer/2022042818/55b53ce7bb61eb1c558b4571/html5/thumbnails/21.jpg)
Questions?
Cofounder/CO-CTO @itsglimpse
@harisamin
HARIS AMIN