2

I am trying to draw parallels between Objective-C Category and features in Java and Ruby. Please see if these conclusions are accurate -

Objective-C Category adds new methods to an existing class even when you do not have access to source code of the class. Java does not have anything similar but in Ruby you can "open" any class, including core language class like String.

class String 
  def my_new_method
  "new test method"
 end
end

"some string".my_new_method
=> "new test method"

Similarly in Objective-C - you can "re-open" the NSString class using category.

#import "NSString+ForTest.h"

@implementation NSString (ForTest)

-(NSString*) myNewMethod {
  return @"new test method";
}

@end

However, one difference I see is that, in Ruby, once a class is opened and modified within a runtime, ALL subsequent uses of that class are affected, in other words it is a system wide change, while in Objective-C only code that imports this header file (and any subclasses) are affected. Are there any other differences?

2 Answers 2

3

However, one difference I see is that, in Ruby, once a class is opened and modified within a runtime, ALL subsequent uses of that class are affected, in other words it is a system wide change, while in Objective-C only code that imports this header file (and any subclasses) are affected

Nope—in Objective-C, all classes in a program (or library) compiled with the class extension are affected, even if your code never imports the relevant header file, so the feature is very similar to that of Ruby.

3
  • 2
    This is true, if you were to do [instanceOfClassThatWasExtendedButHeaderNeverImported performSelector:@selector(methodThatWasAddedInCategory:)]; It would work without crashing. The compiler needs the header only to verify at compile time that a method you are calling directly (rather than sending a selector literal message) is actually there. The runtime will find it whether the compiler knew about it or not when compiling this call to performSelector.
    – atomkirk
    Sep 7, 2013 at 17:39
  • @mipadi - thanks. However, I am seeing an ARC semantic issue error, when I am trying to use the new category method in another class, in the same project. I am using XCode 4.6. Also if I change to id like - -(void) categoryTest { id hello; hello = @"hello"; if([hello respondsToSelector: @selector(myNewMethod)]) { [hello myNewMethod]; } } I still see "No known instance method for selector myNewMethod" error.
    – rnk
    Sep 7, 2013 at 17:56
  • 1
    @rnk That has nothing to do with whether the category influences the class, that is simply ARC not allowing you to call a method whose declaration hasn't been seen because it can't know the memory management semantics therein. You could declare the method as a category on NSObject and the code would compile, for example.
    – bbum
    Sep 7, 2013 at 18:39
2

Another difference is that in ruby, you can open a class and add instance variables to the class:

irb(main):002:0> class String
irb(main):003:1> def a=(v)
irb(main):004:2> @v = v
irb(main):005:2> end
irb(main):006:1> def a
irb(main):007:2> @v
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> s = "string"
=> "string"
irb(main):011:0> s.a = "blah"
=> "blah"
irb(main):012:0> s.a
=> "blah"

In Objective-C categories, you cannot add instance variables to an existing class.

You can however use the runtime to associate an object with another object, and people often use this to add, what looks like from the interface, properties/ivars.

How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.