SlideShare a Scribd company logo
1 of 12
Categories
Categories
@interface NSString (MyExtension)

-(NSString *)randomizeCharacters;

@end




//in code

NSString *stringToScramble = @”Tom Marvolo Riddle”

NSString *scrambledString = [stringToScramble randomizeCharacters];
When should you use a
     category?
Categories
• Extend classes without subclassing.
• Create informal protocols.
• Spread implementation over multiple files.
• Simply development when multiple
  programmers are working on the same file.
• Fix bugs in classes when you don’t have
  access to the source (in a last resort).
Some Cocoa classes
are “class clusters” so
  subclassing is very
  difficult (NSString,
    NSArray, etc).
Sometimes, Apple uses
   “informal protocols.”
    (categories with no
      implementation)

-(void)awakeFromNib;
(Maybe this is
          obsolete.)
@protocol MyProtocol

@optional:
-(void)thisMethod;
-(void)thatMethod;
@end
Anonymous Categories
   are like private
      methods.
@implementation MyClass()
-(void)methodThatIWantToBePrivate;
@end
But not really.
      (The Objective-C runtime isn’t that secure.)


@implementation MyClass()
-(void)methodThatIWantToBePrivate;
@end

// in another class
MyClass *myClass = [[MyClass alloc] init];
[myClass methodThatIWantToBePrivate]; // calls it
Be Careful About

• Overriding existing methods
• Causing conflicts with other categories (if
  two categories implement the method with
  the same name, the result is on predictable)
Subclassing vs Categories


• Many times, the difference is not clear-cut
• Subclass when you need to add a property
  (but in iOS 5 categories can add
  properties)
• Don’t subclass Class Clusters
One example when I use categories is
 using Core Data. Core Data will auto
          generate class files.

     If I want to add methods to those
classes, I add them in a category so that, if
   I need to regenerate the classes, they
         won’t overwrite my changes.

More Related Content

What's hot (10)

Java01
Java01Java01
Java01
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
Singleton Design Pattern - Creation Pattern
Singleton Design Pattern - Creation PatternSingleton Design Pattern - Creation Pattern
Singleton Design Pattern - Creation Pattern
 
Glenn Vanderburg — Learning to love JavaScript
Glenn Vanderburg — Learning to love JavaScriptGlenn Vanderburg — Learning to love JavaScript
Glenn Vanderburg — Learning to love JavaScript
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)
 
Java
JavaJava
Java
 
JavaScript Beyond jQuery
JavaScript Beyond jQueryJavaScript Beyond jQuery
JavaScript Beyond jQuery
 
Ruby's metaclass
Ruby's metaclassRuby's metaclass
Ruby's metaclass
 
Test cho pptx
Test cho pptxTest cho pptx
Test cho pptx
 

Viewers also liked

โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูก
โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูกโรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูก
โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูกKittithat Uthansang
 
Reactive cocoa
Reactive cocoaReactive cocoa
Reactive cocoagillygize
 
BCOP Be A Hero Program
BCOP Be A Hero ProgramBCOP Be A Hero Program
BCOP Be A Hero ProgramAaron Estacio
 
State ofappdevelopment
State ofappdevelopmentState ofappdevelopment
State ofappdevelopmentgillygize
 
Aguinaldo Boquimpani - Marketers - Interactive TV Advertising
Aguinaldo Boquimpani - Marketers - Interactive TV AdvertisingAguinaldo Boquimpani - Marketers - Interactive TV Advertising
Aguinaldo Boquimpani - Marketers - Interactive TV AdvertisingAguinaldo Boquimpani
 
Optimize llvm
Optimize llvmOptimize llvm
Optimize llvmgillygize
 
ViewController/State
ViewController/StateViewController/State
ViewController/Stategillygize
 
ความรู้เบื้องต้นเกี่ยวกับการแนะแนว
ความรู้เบื้องต้นเกี่ยวกับการแนะแนวความรู้เบื้องต้นเกี่ยวกับการแนะแนว
ความรู้เบื้องต้นเกี่ยวกับการแนะแนวCalan Smith
 
Two-StageCreation
Two-StageCreationTwo-StageCreation
Two-StageCreationgillygize
 
App Idea - Meet your team
App Idea - Meet your teamApp Idea - Meet your team
App Idea - Meet your teamCheah Eng Soon
 
Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOSgillygize
 
Communication & public speaking
Communication & public speakingCommunication & public speaking
Communication & public speakingAaron Estacio
 
Azure With Windows Store App Development
Azure With Windows Store App DevelopmentAzure With Windows Store App Development
Azure With Windows Store App DevelopmentCheah Eng Soon
 

Viewers also liked (14)

โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูก
โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูกโรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูก
โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูก
 
Reactive cocoa
Reactive cocoaReactive cocoa
Reactive cocoa
 
BCOP Be A Hero Program
BCOP Be A Hero ProgramBCOP Be A Hero Program
BCOP Be A Hero Program
 
State ofappdevelopment
State ofappdevelopmentState ofappdevelopment
State ofappdevelopment
 
Aguinaldo Boquimpani - Marketers - Interactive TV Advertising
Aguinaldo Boquimpani - Marketers - Interactive TV AdvertisingAguinaldo Boquimpani - Marketers - Interactive TV Advertising
Aguinaldo Boquimpani - Marketers - Interactive TV Advertising
 
Optimize llvm
Optimize llvmOptimize llvm
Optimize llvm
 
ViewController/State
ViewController/StateViewController/State
ViewController/State
 
ความรู้เบื้องต้นเกี่ยวกับการแนะแนว
ความรู้เบื้องต้นเกี่ยวกับการแนะแนวความรู้เบื้องต้นเกี่ยวกับการแนะแนว
ความรู้เบื้องต้นเกี่ยวกับการแนะแนว
 
Two-StageCreation
Two-StageCreationTwo-StageCreation
Two-StageCreation
 
App Idea - Meet your team
App Idea - Meet your teamApp Idea - Meet your team
App Idea - Meet your team
 
SQLite with UWP
SQLite with UWPSQLite with UWP
SQLite with UWP
 
Connecting to a REST API in iOS
Connecting to a REST API in iOSConnecting to a REST API in iOS
Connecting to a REST API in iOS
 
Communication & public speaking
Communication & public speakingCommunication & public speaking
Communication & public speaking
 
Azure With Windows Store App Development
Azure With Windows Store App DevelopmentAzure With Windows Store App Development
Azure With Windows Store App Development
 

Similar to Categories

JAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESJAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESNikunj Parekh
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questionsMehtaacademy
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptxSAICHARANREDDYN
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritancemcollison
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013 The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013 GR8Conf
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not JavaChris Adamson
 

Similar to Categories (20)

Java basics
Java basicsJava basics
Java basics
 
JAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESJAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICES
 
Jist of Java
Jist of JavaJist of Java
Jist of Java
 
25 java interview questions
25 java interview questions25 java interview questions
25 java interview questions
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
 
Javasession6
Javasession6Javasession6
Javasession6
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
Inheritance Mixins & Traits
Inheritance Mixins & TraitsInheritance Mixins & Traits
Inheritance Mixins & Traits
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
C#
C#C#
C#
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013 The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not Java
 
Parte II Objective C
Parte II   Objective CParte II   Objective C
Parte II Objective C
 

Recently uploaded

Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...CzechDreamin
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101vincent683379
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераMark Opanasiuk
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024Stephen Perrenod
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPTiSEO AI
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandIES VE
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Hiroshi SHIBATA
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIES VE
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe中 央社
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty SecureFemke de Vroome
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGDSC PJATK
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctBrainSell Technologies
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaCzechDreamin
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...FIDO Alliance
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftshyamraj55
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...FIDO Alliance
 
Your enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4jYour enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4jNeo4j
 

Recently uploaded (20)

Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Your enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4jYour enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4j
 

Categories

  • 2. Categories @interface NSString (MyExtension) -(NSString *)randomizeCharacters; @end //in code NSString *stringToScramble = @”Tom Marvolo Riddle” NSString *scrambledString = [stringToScramble randomizeCharacters];
  • 3. When should you use a category?
  • 4. Categories • Extend classes without subclassing. • Create informal protocols. • Spread implementation over multiple files. • Simply development when multiple programmers are working on the same file. • Fix bugs in classes when you don’t have access to the source (in a last resort).
  • 5. Some Cocoa classes are “class clusters” so subclassing is very difficult (NSString, NSArray, etc).
  • 6. Sometimes, Apple uses “informal protocols.” (categories with no implementation) -(void)awakeFromNib;
  • 7. (Maybe this is obsolete.) @protocol MyProtocol @optional: -(void)thisMethod; -(void)thatMethod; @end
  • 8. Anonymous Categories are like private methods. @implementation MyClass() -(void)methodThatIWantToBePrivate; @end
  • 9. But not really. (The Objective-C runtime isn’t that secure.) @implementation MyClass() -(void)methodThatIWantToBePrivate; @end // in another class MyClass *myClass = [[MyClass alloc] init]; [myClass methodThatIWantToBePrivate]; // calls it
  • 10. Be Careful About • Overriding existing methods • Causing conflicts with other categories (if two categories implement the method with the same name, the result is on predictable)
  • 11. Subclassing vs Categories • Many times, the difference is not clear-cut • Subclass when you need to add a property (but in iOS 5 categories can add properties) • Don’t subclass Class Clusters
  • 12. One example when I use categories is using Core Data. Core Data will auto generate class files. If I want to add methods to those classes, I add them in a category so that, if I need to regenerate the classes, they won’t overwrite my changes.

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n