21

I'm doing research about Objective-C, and I'd like to know if it has the concept of reflection. None of the documents I've found so far describes reflection.

Boann
  • 48,794
  • 16
  • 117
  • 146
son.vu
  • 213
  • 1
  • 2
  • 4
  • See http://en.wikipedia.org/wiki/Objective-C – ott-- Sep 26 '11 at 02:15
  • 1
    Objective-C have a much more powerful reflection ability than Java have. You can know more from [Apple's Objective-C Runtime Programming Guide.](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008048) – venj Sep 26 '11 at 02:21
  • You will get a better answer if you explain precisely what *you* mean by "reflection." Like many languages, ObjC has some kinds of reflection and not others. – Rob Napier Sep 26 '11 at 02:31
  • @son.vu note that you can accept an answer by clicking the big white checkmark shown to the left of the answer. – Yuji Sep 26 '11 at 02:38
  • @RobNapier . OK . i'm want research about all concept of Reflection in Objective-C . Besides, i also want research about Abstract Class in Obj-C . hik hik – son.vu Sep 26 '11 at 03:07

4 Answers4

27

There are runtime functions described in Runtime Reference which allows not only querying for features of a class or an instance, but also adding a method, or even creating a new class at runtime. I say this is a very dynamic kind of reflection, which was not usually available to C-based languages. Mike Ash's wrappers is an Objective-C wrapper around that. Again, it can even add methods! The base class of Cocoa, NSObject, also provide wrappers for many of the runtime functions, see NSObject protocol reference. For example

     [foo respondsToSelector:@selector(bar:)];

     if([foo isKindOfClass:[NSString class]]){ ... } 

does what the method names say. You can even add a method on the fly. For example,

   #import <Foundation/Foundation.h>
   #import <objc/runtime.h>

   @interface Foo:NSObject
   {
   }
   @end
   @implementation Foo
   -(void)sayHi
   {
        NSLog(@"Hi! from %@",NSStringFromSelector(_cmd));
   }
   +(BOOL)resolveInstanceMethod:(SEL)sel
   {
        Method method=class_getInstanceMethod(self,@selector(sayHi));
        class_addMethod(self,sel,method_getImplementation(method),method_getTypeEncoding(method));
        return YES;
   }
   @end

   int main(){
    NSAutoreleasePool*pool=[[NSAutoreleasePool alloc] init];
        Foo* foo=[[Foo alloc] init];
        [foo aeiou];
        [foo bark];
        [foo mew];
        [pool drain];
        return 0;
  }

This produces the output

  Hi! from aeiou
  Hi! from bark
  Hi! from mew

What it does is as follows:

  1. SEL is the variable which represents the sent message (or method call, in the other terminology.)
  2. Objective-C runtime calls resolveInstanceMethod: of a class if a message sent to an instance is not implemented in the class
  3. So, in this case, I just copy an implementation of a predefined method called sayHi to that method's implementation.
  4. From the method, you can use _cmd to see what was the selector used in calling the method. So, even from a single sayHi implementation, we can get different output.

Some of the standard Cocoa's implementation (Key-Value-Coding, Key-Value-Observing and Core Data in particular) uses the runtime to dynamically modify the class.

Yuji
  • 34,103
  • 3
  • 70
  • 88
15

Yes, Objective-C has reflection (and abstract classes). Whether that reflection fits your needs or not is a different question.

Reflection in Objective-C is surprisingly flexible for a C derived language. You can ask a class what methods it implements or you can modify and/or add methods to existing classes. You can even create new classes on the fly or change the class of any instance at any time.

A typical Objective-C program does none of theses things (at least not directly) save for occasionally calling respondsToSelector: or conformsToProtocol:. While Objective-C is a fully dynamic, reflective, object oriented language, that doesn't mean that such patterns are encouraged or typically used.

You might find my answers to these questions interesting:

In Objective C, how to find out the return type of a method via reflection?

Does Objective-C have a Standard Library?

Smalltalk runtime features absent on Objective-C?

And, of course, the runtime is very well documented. As is the language. Or you could read the source.

Community
  • 1
  • 1
bbum
  • 162,346
  • 23
  • 271
  • 359
  • Do you have any idea how to set the actual property once you have found it using Reflection? I have a question here: http://stackoverflow.com/q/25538890/1735836 – Patricia Aug 28 '14 at 15:47
1

I guess I don't know what aspect of reflection you're looking for. Are you looking for how to determine which class a given variable is? If so, check this out: Objective-C class -> string like: [NSArray className] -> @"NSArray"

// Get a object's class name
if ([NSStringFromClass([foo class]) compare:@"NSSomeObject"] == NSOrderedSame) {...}

// Does an object have a method
[foo respondsTo:@selector(method:)]

Are you looking for more than just this?

Community
  • 1
  • 1
manchicken
  • 156
  • 6
  • oh ! my manager require research about Abstract Class and Reflection in Objetive-C . I tried to find out but to no result , so I must ask everybody :( . P/S : thanks a lot . – son.vu Sep 26 '11 at 02:19
  • Huh? Is it related to your work? You need to pay us part of your salary :) – Yuji Sep 26 '11 at 02:36
1

There are a flock of methods that can access various properties of a class and, presumably, "peek" and "poke" at some object internals. I've never played with them, and a little reading that I've done suggests that they're far from perfect, but they do appear to offer some ability for "reflection", similar to the Java concept.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Do you have any idea how to set the actual property once you have found it using Reflection? I have a question here: http://stackoverflow.com/q/25538890/1735836 – Patricia Aug 28 '14 at 15:47