Objective-C reflectionThis is what I will introduce in this article. When I first came into contact with Java, I thought the wholeReflectionThe packages are very novel, which makes Java and interpreted scripting languages closer, while also opening the distance from mainstream C and C ++.
It is incredible to see the class metadata of an object at runtime, although these may not be frequently used in daily application programming. From JavaObjective-CProgrammers should like itObjective-CAlso supportedReflection. Actually,Objective-CThere are many dynamic features such as dynamically changing the class definition and creating a new class.
However, it is hard to say how powerful these functions are. This also makes me feel that objective-C is a somewhat bloated language. In my opinion, objective-C has a positioning Crisis: Is it an interpreted language or a compilation language? The runtime is dynamic to a large extent. Unlike C ++, objective-C is bound during runtime. This is also why we can define a method that has never been declared in the header file during implementation, or extend the class through category. Unfortunately, this bloated model makes it difficult to find something useful in daily programming. This article is about to discover some of the "Treasures ".
Root nsobject
Most (if not all) dynamic reflection supports nsobject class. Similar to object objects in Java, nsobject is the root class of all classes (except for some rare exceptions. Therefore, all classes you write can support reflection. All of these reflection support is not part of objective-C, but is derived from the runtime environment of NS. This is also the reason why these things feel added to some additional things. Because it is added with additional stuff.
You can obtain the metadata of an object's class by calling the following class method:
- Class C = [self class];
This method is both an instance method and a class method. It returns a C constructor with a lot of magic information, such as instance variables and methods. All of these are outdated compared to the java. Lang. Reflect package, and the interfaces that use objective-C to access this information seem complicated. This may be intentionally designed to "filter" unqualified programmers. So far, the only place I use these is to provide parameters for the iskindofclass: method to be introduced below. For a long time, I don't need to look into the contents of the class structure.
I have introduced one aspect of reflection in the article "call a dynamic equation. This allows you to create a method call at runtime and input parameters. This is similar to the java. Lang. Reflect. method class in Java.
Check the inheritance relationship
Java has an operator named instanceof that can be used to check whether an object is an instance of a specific class or interface. Objective-C also has a similar function, that is, using iskindofclass: method. Iskindofclass: Yes is returned when the message receiver is an instance of the specified class and its subclass. For example, an associated pointer array can be used to perform different operations based on its type:
- For (baseclass * base in myarray ){
- If ([base iskindofclass: [classone class]) {
- // Do Stuff specific to classone
- } Else if ([base iskindofclass: [classtwo class]) {
- // Do Stuff specific to classtwo
- } Else if ([base iskindofclass: [classthree class]) {
- // Do Stuff specific to classthree}
- // Etc}
If you need an exact class match, instead of matching any inherited class, you can use ismemberofclass: method.
The check is similar to the instance check. You can test whether an object complies with a specific protocol. Java uses the instanceof method to handle classes and interfaces, but objective-C uses a more cumbersome method. When testing compliance, you should use conformstoprotocol: method:
- Bool conforms = [OBJ conformstoprotocol: @ protocol (myinterface)];
Check whether the method exists. For Java and C ++ veterans like me, it is strange to know whether an object implements a method. However, the objective-C class is dynamic to a large extent, and you need to check whether the method you need exists. This requires the respondstoselector: method. The following code checks whether the receiver implements (or inherits) the specified method:
- If ([OBJ respondstoselector: @ selector (amethod :)])
- {// It's there, so we can call it [OBJ amethod: Yes];
- }
Of course, useObjective-COfReflectionYou can do more things. Here I am just trying to talk about it.ReflectionThe most common application of the mechanism. If you need to add core dynamic features to your software, you need to familiarize yourself with these documents:
- Runtime programming guide: Introduction runtime reference
Summary: AnalysisObjective-C reflectionI hope this article will help you!
From: http://mobile.51cto.com/iphone-282485.htm
By sschu