SlideShare a Scribd company logo
1 of 21
Download to read offline
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 a specific message (2 ways) 
1. With content 
2. Without Content
Publish Message Without Content 
• We want to notify that something specific 
‘happened’ 
• Method Signature 
- (void)postNotificationName:(NSString 
*)notificationName object:(id)notificationSender
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];
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}];
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 
// ... 
}
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
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
Delegates 
• Very solid pattern 
• Vigorously multitude of Cocoa classes 
(UITableView, UICollectionView, UIAlterView.. 
etc.) 
• You have to define the protocol of the 
messages you send
A Personal Note… 
• I consumed delegates without really 
understanding how they work 
• I used to be scared of trying to implement 
one myself
TIME TO GAIN A 
SUPERPOWER!
Delegates 
• Very solid pattern 
• Vigorously multitude of Cocoa classes 
(UITableView, UICollectionView, UIAlterView.. 
etc.) 
• You have to define the protocol of the 
messages you send
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
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
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
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
A parting note… 
with great SUPER power 
comes great SUPER 
responsibility!
Questions? 
HARIS AMIN 
Cofounder/CO-CTO @itsglimpse 
@harisamin

More Related Content

Viewers also liked (9)

Dynamic Port Scanning
Dynamic Port ScanningDynamic Port Scanning
Dynamic Port Scanning
 
Packet sniffing in switched LANs
Packet sniffing in switched LANsPacket sniffing in switched LANs
Packet sniffing in switched LANs
 
Arp (address resolution protocol)
Arp (address resolution protocol)Arp (address resolution protocol)
Arp (address resolution protocol)
 
Address resolution protocol
Address resolution protocolAddress resolution protocol
Address resolution protocol
 
Packet sniffing & ARP Poisoning
 Packet sniffing & ARP Poisoning  Packet sniffing & ARP Poisoning
Packet sniffing & ARP Poisoning
 
Writing A Health Research Proposal
Writing A Health Research ProposalWriting A Health Research Proposal
Writing A Health Research Proposal
 
Address Resolution Protocol
Address Resolution ProtocolAddress Resolution Protocol
Address Resolution Protocol
 
My research proposal.ppt
My research proposal.pptMy research proposal.ppt
My research proposal.ppt
 
The Research Proposal
The Research ProposalThe Research Proposal
The Research Proposal
 

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

iOS Development: What's New
iOS Development: What's NewiOS Development: What's New
iOS Development: What's New
NascentDigital
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
Petr Dvorak
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
DataArt
 

Similar to Intro to Obj-C Design Patterns or Or how I learned to be less bad (20)

iOS Development: What's New
iOS Development: What's NewiOS Development: What's New
iOS Development: What's New
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
 
Getting started with titanium
Getting started with titaniumGetting started with titanium
Getting started with titanium
 
Ios development
Ios developmentIos development
Ios development
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
 
Wordpress Shortcode
Wordpress ShortcodeWordpress Shortcode
Wordpress Shortcode
 
Getting started with Appcelerator Titanium
Getting started with Appcelerator TitaniumGetting started with Appcelerator Titanium
Getting started with Appcelerator Titanium
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOS
 
iPhone dev intro
iPhone dev introiPhone dev intro
iPhone dev intro
 
Beginning to iPhone development
Beginning to iPhone developmentBeginning to iPhone development
Beginning to iPhone development
 
Bootstrapping iPhone Development
Bootstrapping iPhone DevelopmentBootstrapping iPhone Development
Bootstrapping iPhone Development
 
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
 
CoconutKit
CoconutKitCoconutKit
CoconutKit
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not Java
 
Reactive summit 2020 microsoft orleans the easy way
Reactive summit 2020   microsoft orleans the easy wayReactive summit 2020   microsoft orleans the easy way
Reactive summit 2020 microsoft orleans the easy way
 
Wahckon[2] - iOS Runtime Hacking Crash Course
Wahckon[2] - iOS Runtime Hacking Crash CourseWahckon[2] - iOS Runtime Hacking Crash Course
Wahckon[2] - iOS Runtime Hacking Crash Course
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talk
 
Cappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application FrameworkCappuccino - A Javascript Application Framework
Cappuccino - A Javascript Application Framework
 
React nativebeginner1
React nativebeginner1React nativebeginner1
React nativebeginner1
 
Design Patterns in iOS
Design Patterns in iOSDesign Patterns in iOS
Design Patterns in iOS
 

Recently uploaded

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 

Recently uploaded (20)

VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 

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

  • 1. Intro to Obj-C Design Patterns Or how I learned to be less bad Haris Amin
  • 2. GLIMPSE A fun way to meet new people through Instagram
  • 3. Design Patterns • Why do we care? • Let’s explore two design patterns in Cocoa 1. Notification Center 2. Delegates
  • 4. Notification Center • NSNotificationCenter class • Easiest to learn (IMHO) but also easiest to abuse • Very flexible
  • 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. Publish Message Without Content • We want to notify that something specific ‘happened’ • Method Signature - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender
  • 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. 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. 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. 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. 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. 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. A Personal Note… • I consumed delegates without really understanding how they work • I used to be scared of trying to implement one myself
  • 14. TIME TO GAIN A SUPERPOWER!
  • 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. 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. 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. 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. 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. A parting note… with great SUPER power comes great SUPER responsibility!
  • 21. Questions? HARIS AMIN Cofounder/CO-CTO @itsglimpse @harisamin