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

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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
 
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
 
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
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Dernier (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 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
 
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...
 
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
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
+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...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

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