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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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, seeNSObjectprotocol reference. For exampledoes what the method names say. You can even add a method on the fly. For example,
This produces the output
What it does is as follows:
SELis the variable which represents the sent message (or method call, in the other terminology.)resolveInstanceMethod:of a class if a message sent to an instance is not implemented in the classsayHito that method’s implementation._cmdto see what was the selector used in calling the method. So, even from a singlesayHiimplementation, 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.