SlideShare a Scribd company logo
Designs, patterns and practices
in Object Oriented Programming
for iOS Developers
Who the heck am I?
maciej.burda@me.com
Agenda
• obvious best practices in Obj-C (or at least what I do)
• software design patterns
• In mean time there are some questions waiting for you
The obvious is that which is
never seen until someone
expresses it simply.
Khalil Gibran
Best Practices
#pragma mark - group stuff
CHCamelCaseWordInEnglish
@properties vs iVars
NEON for the performance
Method naming
Examples
- (instancetype)initWithWidth:
(CGFloat)width andHeight:(CGFloat)height;
- (UIView*)taggedView:(NSInteger)tag;
- (void)setT:(NSString *)text
i:(UIImage *)image;
- (void)sendAction:(SEL)aSelector:
(id)anObject :(BOOL)flag;
.Notation or [Brackets]
if (!error)
return success;
Ternary operator
NSInteger value = 5;
result = (value != 0) ? x : y;
int a = 1, b = 2, c = 3, d = 4;
int x = 10, y = 5;
int result = a > b ? x = c > d ? c : d : y;
Design Patterns
- are reusable solutions to common problems
in software design.
!
- They’re templates designed to help you write
code that’s easy to understand and reuse.
!
- They also help you create loosely coupled
code so that you can change or replace
components in your code without too much
of a hassle.
Cocoa Heads Tricity - Design Patterns
MVC
KVO
Singleton
How to use
?
Singletons in Obj-C
• [NSUserDefaults standardUserDefaults]
• [UIScreen mainScreen]
• [NSFileManager defaultManager]
• [UIApplication sharedApplication]
Cocoa Heads Tricity - Design Patterns
Façade
• make a software library easier to
use, understand and test, since the
facade has convenient methods for
common tasks;
• make the library more readable, for
the same reason;
• reduce dependencies of outside
code on the inner workings of a
library, since most code uses the
facade, thus allowing more
flexibility in developing the system;
• wrap a poorly designed collection
of APIs with a single well-designed
API (as per task needs).
Cocoa Heads Tricity - Design Patterns
Strategy
Strategy
• defines a family of algorithms,
• encapsulates each algorithm
• makes the algorithms interchangeable within that
family.
@protocol Strategy <NSObject>
!
@optional
- (void) execute;
!
@end
@interface ConcreteStrategyA : NSObject <Strategy>
{
// ivars for A
}
@end
@implementation ConcreteStrategyA
!
- (void) execute
{
NSLog(@"Called ConcreteStrategyA execute method");
}
!
@end
@interface Context : NSObject
{
id<Strategy> strategy;
}
@property (assign) id<Strategy> strategy;
!
- (void) execute;
!
@end
@implementation Context
!
@synthesize strategy;
!
- (void) execute
{
if ([strategy respondsToSelector:@selector(execute)]) {
[strategy execute];
}
}
!
@end
Decorator (Wrapper, Adapter)
is a design pattern that allows behavior to be
added to an individual object, either statically
or dynamically, without affecting the behavior
of other objects from the same class.
Category
#import "UIImage+Retina4.h"
#import <objc/runtime.h>
!
static Method origImageNamedMethod = nil;
!
@implementation UIImage (Retina4)
!
+ (void)initialize {
origImageNamedMethod = class_getClassMethod(self, @selector(imageNamed:));
method_exchangeImplementations(origImageNamedMethod,
class_getClassMethod(self, @selector(retina4ImageNamed:)));
}
!
+ (UIImage *)retina4ImageNamed:(NSString *)imageName {
NSMutableString *imageNameMutable = [imageName mutableCopy];
NSRange retinaAtSymbol = [imageName rangeOfString:@"@"];
if (retinaAtSymbol.location != NSNotFound) {
[imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location];
} else {
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) {
NSRange dot = [imageName rangeOfString:@"."];
if (dot.location != NSNotFound) {
[imageNameMutable insertString:@"-568h@2x" atIndex:dot.location];
} else {
[imageNameMutable appendString:@"-568h@2x"];
}
}
}
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:@""];
if (imagePath) {
return [UIImage retina4ImageNamed:imageNameMutable];
} else {
return [UIImage retina4ImageNamed:imageName];
}
return nil;
}
!
@end
Delegate
This is an important pattern. Apple uses this approach in most of the UIKit
classes:
UITableView
UITextView
UITextField
UIWebView
UIAlert
UIActionSheet
UICollectionView
UIGestureRecognizer
UIScrollView
UIPickerView
Command
is a behavioral design pattern in which an object is used to represent
and encapsulate all the information needed to call a method at a
later time
NSInvocation
!
NSMethodSignature * mySignature = [NSMutableArray
instanceMethodSignatureForSelector:@selector(addObject:)];
NSInvocation * myInvocation = [NSInvocation
invocationWithMethodSignature:mySignature];
NSString * myString = @"String";
//Next, you would specify which object to send the message to:
!
[myInvocation setTarget:myArray];
//Specify the message you wish to send to that object:
!
[myInvocation setSelector:@selector(addObject:)];
//And fill in any arguments for that method:
!
[myInvocation setArgument:&myString atIndex:2];
//Note that object arguments must be passed by pointer.
!
//At this point, myInvocation is a complete object, describing a message that can be sent. To
actually send the message, you would call:
!
[myInvocation invoke];
An NSInvocation is an Objective-C message rendered
static, that is, it is an action turned into an object.
!
Where to go from that?
Thank you for your attention!
Questions?

More Related Content

PDF
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
PDF
Design patterns - Using Ruby
ODP
Aspect-Oriented Programming
PDF
Styled Components & React.js
PDF
Styled components presentation
PDF
iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
PDF
7 things one should learn from iOS
PDF
iOS Developer Overview - DevWeek 2014
elm-d3 @ NYC D3.js Meetup (30 June, 2014)
Design patterns - Using Ruby
Aspect-Oriented Programming
Styled Components & React.js
Styled components presentation
iOSNeXT.ro - 10 reasons you'll love Swift - Paul Ardeleanu
7 things one should learn from iOS
iOS Developer Overview - DevWeek 2014

Viewers also liked (8)

PPT
Mobile UI Design Patterns
PPT
Coding Standards & Best Practices for iOS/C#
PDF
Sensational iOS App Design: First Principles and New Trends for 2012
KEY
Cocoa Design Patterns
KEY
iOS Design Patterns
KEY
Mac/iOS Design Patterns
PDF
Design Patterns in iOS
PPTX
iOS Coding Best Practices
Mobile UI Design Patterns
Coding Standards & Best Practices for iOS/C#
Sensational iOS App Design: First Principles and New Trends for 2012
Cocoa Design Patterns
iOS Design Patterns
Mac/iOS Design Patterns
Design Patterns in iOS
iOS Coding Best Practices
Ad

Similar to Cocoa Heads Tricity - Design Patterns (20)

PDF
Hi performance table views with QuartzCore and CoreText
PPTX
Final ppt
PDF
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
PPTX
Javascript Design Patterns
PDF
WebNet Conference 2012 - Designing complex applications using html5 and knock...
PDF
Angular 2 for Java Developers
PPTX
Java - A broad introduction
PDF
Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)
PDF
Voorhoede - Front-end architecture
PDF
Porting legacy apps to Griffon
PPTX
Eclipse e4 Overview
PPTX
Bring your code to explore the Azure Data Lake: Execute your .NET/Python/R co...
PPTX
ASP.Net 5 and C# 6
PDF
Using Protocol to Refactor
PPTX
Mobile App Development: Primi passi con NativeScript e Angular 2
PDF
Awesome html with ujs, jQuery and coffeescript
PDF
Eclipse 40 - Eclipse Summit Europe 2010
PDF
iOS best practices
PDF
Heroku pop-behind-the-sense
Hi performance table views with QuartzCore and CoreText
Final ppt
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Javascript Design Patterns
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Angular 2 for Java Developers
Java - A broad introduction
Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)
Voorhoede - Front-end architecture
Porting legacy apps to Griffon
Eclipse e4 Overview
Bring your code to explore the Azure Data Lake: Execute your .NET/Python/R co...
ASP.Net 5 and C# 6
Using Protocol to Refactor
Mobile App Development: Primi passi con NativeScript e Angular 2
Awesome html with ujs, jQuery and coffeescript
Eclipse 40 - Eclipse Summit Europe 2010
iOS best practices
Heroku pop-behind-the-sense
Ad

Recently uploaded (20)

PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Understanding Forklifts - TECH EHS Solution
PDF
top salesforce developer skills in 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
history of c programming in notes for students .pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Essential Infomation Tech presentation.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
wealthsignaloriginal-com-DS-text-... (1).pdf
Digital Strategies for Manufacturing Companies
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Understanding Forklifts - TECH EHS Solution
top salesforce developer skills in 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Odoo Companies in India – Driving Business Transformation.pdf
history of c programming in notes for students .pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
How Creative Agencies Leverage Project Management Software.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Essential Infomation Tech presentation.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
PTS Company Brochure 2025 (1).pdf.......
How to Migrate SBCGlobal Email to Yahoo Easily
Upgrade and Innovation Strategies for SAP ERP Customers

Cocoa Heads Tricity - Design Patterns

  • 1. Designs, patterns and practices in Object Oriented Programming for iOS Developers
  • 2. Who the heck am I? maciej.burda@me.com
  • 3. Agenda • obvious best practices in Obj-C (or at least what I do) • software design patterns • In mean time there are some questions waiting for you
  • 4. The obvious is that which is never seen until someone expresses it simply. Khalil Gibran
  • 6. #pragma mark - group stuff
  • 9. NEON for the performance
  • 11. Examples - (instancetype)initWithWidth: (CGFloat)width andHeight:(CGFloat)height; - (UIView*)taggedView:(NSInteger)tag; - (void)setT:(NSString *)text i:(UIImage *)image; - (void)sendAction:(SEL)aSelector: (id)anObject :(BOOL)flag;
  • 14. Ternary operator NSInteger value = 5; result = (value != 0) ? x : y; int a = 1, b = 2, c = 3, d = 4; int x = 10, y = 5; int result = a > b ? x = c > d ? c : d : y;
  • 15. Design Patterns - are reusable solutions to common problems in software design. ! - They’re templates designed to help you write code that’s easy to understand and reuse. ! - They also help you create loosely coupled code so that you can change or replace components in your code without too much of a hassle.
  • 17. MVC
  • 18. KVO
  • 21. Singletons in Obj-C • [NSUserDefaults standardUserDefaults] • [UIScreen mainScreen] • [NSFileManager defaultManager] • [UIApplication sharedApplication]
  • 23. Façade • make a software library easier to use, understand and test, since the facade has convenient methods for common tasks; • make the library more readable, for the same reason; • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system; • wrap a poorly designed collection of APIs with a single well-designed API (as per task needs).
  • 26. Strategy • defines a family of algorithms, • encapsulates each algorithm • makes the algorithms interchangeable within that family.
  • 27. @protocol Strategy <NSObject> ! @optional - (void) execute; ! @end @interface ConcreteStrategyA : NSObject <Strategy> { // ivars for A } @end @implementation ConcreteStrategyA ! - (void) execute { NSLog(@"Called ConcreteStrategyA execute method"); } ! @end
  • 28. @interface Context : NSObject { id<Strategy> strategy; } @property (assign) id<Strategy> strategy; ! - (void) execute; ! @end @implementation Context ! @synthesize strategy; ! - (void) execute { if ([strategy respondsToSelector:@selector(execute)]) { [strategy execute]; } } ! @end
  • 29. Decorator (Wrapper, Adapter) is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.
  • 30. Category #import "UIImage+Retina4.h" #import <objc/runtime.h> ! static Method origImageNamedMethod = nil; ! @implementation UIImage (Retina4) ! + (void)initialize { origImageNamedMethod = class_getClassMethod(self, @selector(imageNamed:)); method_exchangeImplementations(origImageNamedMethod, class_getClassMethod(self, @selector(retina4ImageNamed:))); } ! + (UIImage *)retina4ImageNamed:(NSString *)imageName { NSMutableString *imageNameMutable = [imageName mutableCopy]; NSRange retinaAtSymbol = [imageName rangeOfString:@"@"]; if (retinaAtSymbol.location != NSNotFound) { [imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location]; } else { CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) { NSRange dot = [imageName rangeOfString:@"."]; if (dot.location != NSNotFound) { [imageNameMutable insertString:@"-568h@2x" atIndex:dot.location]; } else { [imageNameMutable appendString:@"-568h@2x"]; } } } NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:@""]; if (imagePath) { return [UIImage retina4ImageNamed:imageNameMutable]; } else { return [UIImage retina4ImageNamed:imageName]; } return nil; } ! @end
  • 31. Delegate This is an important pattern. Apple uses this approach in most of the UIKit classes: UITableView UITextView UITextField UIWebView UIAlert UIActionSheet UICollectionView UIGestureRecognizer UIScrollView UIPickerView
  • 32. Command is a behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time
  • 33. NSInvocation ! NSMethodSignature * mySignature = [NSMutableArray instanceMethodSignatureForSelector:@selector(addObject:)]; NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature:mySignature]; NSString * myString = @"String"; //Next, you would specify which object to send the message to: ! [myInvocation setTarget:myArray]; //Specify the message you wish to send to that object: ! [myInvocation setSelector:@selector(addObject:)]; //And fill in any arguments for that method: ! [myInvocation setArgument:&myString atIndex:2]; //Note that object arguments must be passed by pointer. ! //At this point, myInvocation is a complete object, describing a message that can be sent. To actually send the message, you would call: ! [myInvocation invoke]; An NSInvocation is an Objective-C message rendered static, that is, it is an action turned into an object. !
  • 34. Where to go from that?
  • 35. Thank you for your attention! Questions?