SlideShare una empresa de Scribd logo
1 de 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.

Más contenido relacionado

La actualidad más candente (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
 

Destacado

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

Destacado (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 a 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 a 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
 

Último

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 

Último (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

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.

Notas del editor

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