SlideShare ist ein Scribd-Unternehmen logo
1 von 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.

Weitere ähnliche Inhalte

Was ist angesagt? (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
 

Andere mochten auch

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

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

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
 
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 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
 
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
 
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
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 

Kürzlich hochgeladen (20)

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
 
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 Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
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?
 
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...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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...
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 

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.

Hinweis der Redaktion

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