views:

114

answers:

3

I have seen examples (on here especially) of calling hideous C functions and getting structures back that have to be iterated, replete with reams of underbars.

Why can't I do this (pseudo to follow):

  Money *cost = [[Money alloc] init];

  for (Property *property in [[cost class] properties]){
    ..
  }

  for (Method *method in [[cost class] methods]){
    ..
  }

?

+7  A: 

Those functions are not hideous. They are all documented in the Objective-C Runtime Reference.

One reason I think there aren't the +properties and +methods methods are because it's rare you need to look for property/method that you don't know the name in compile time.

The most useful introspection functions in ObjC are -respondsToSelector:, -isKindOfClass:, -conformsToProtocol:, NSInvocation, KVC, KVO, NSXxxxFromString, and they are operating with objects.

KennyTM
Having checked out your link to the Objective-C runtime, it seems to me that it would be relatively trivial to add Objective-C methods to a category of NSObject to do exactly what the questioner asks for in his pseudocode.
JeremyP
@Jeremy, I just wanted to make sure I was not missing something before I went off and did my own wrapper.@Kenny, I know those methods are documented. As to what I would or would not need, I disagree with your characterization. After years of using Reflection in Java, I found myself many times getting properties from classes. That's frankly the whole idea. If you know the names of the properties at compile time, why not just make a statically-typed class?
Rob
@Rob: Because the implementations of those methods may not exist, like all those delegates used in Cocoa.
KennyTM
@Kenny: What?? I can reference a protocol statically even if there are no implementations, just as I can write code against interfaces then deliver that code to encounter implementations at runtime that were never seen at compile time. So draft a memo to the Java community that there really is no need for Reflection beyond just asking an object if it supports an interface or instanceof(). A huge amount of interesting code was written in the last decade to operate on classes whose properties and methods were unknown at compile time in Java.
Rob
@Rob: (1) Not a protocol, a method, like duck typing. (2) Don't equate Java with ObjC. Or maybe, what practical, interesting code in Java you think is applicable to ObjC?
KennyTM
+1  A: 

Here's why you can't do it: Because Cocoa operates on a level above the runtime. The Objective-C runtime functions can't reasonably depend on Cocoa, an optional library implemented on top of it. It would be possible to create an object-oriented layer on top of the runtime API, but this was not a design goal of Cocoa. It's just not necessary to do what Cocoa does.

Chuck
I understand that Cocoa does not need it. So it is just the case that, as was true in Windows programming forever, it's not object oriented: it's procedural with objects on top and if they didn't make objects for what you need, have fun either writing your own wrappers or going back in time to the Structured Programming Era.
Rob
@Rob: It's true that Objective-C is a hybrid of a pure OO language (Smalltalk) and a structured procedural language (C). The OO parts are closer to the orignal definition of "object-oriented" than Java is, but it does include C as well. More to the point, even if Objective-C did provide an OO runtime API, you wouldn't be able to use it easily with Cocoa, because Cocoa objects are not standard Objective-C objects. The standard library doesn't include things like retain/release, alloc/init, performSelector:, etc. Would you be happy with these alien objects?
Chuck
@Rob: And more to the point, I think you're trying to translate a Java idiom (equating "Java" with "OO") to a more strongly OO language where it is not appropriate or idiomatic.
Chuck
A: 

Check out NSClassDescription and the cover methods on NSObject. This may be close to what you are looking for.

David Koski