SlideShare une entreprise Scribd logo
1  sur  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.

Contenu connexe

Tendances (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
 

En vedette

โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูก
โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูกโรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูก
โรงแรม ที่พัก ทริปท่องเที่ยวต่างประเทศราคาถูก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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Dernier (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

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.

Notes de l'éditeur

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