When I first came into contact with Java, I thought the entire reflection package was very novel. It brought Java closer to the interpreted scripting language, at the same time, it also opened a distance from the 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. Programmers who switch from Java to objective-C should be happy to see that objective-C also supports reflection. In fact, objective-C has many such features as dynamically changing class definitions 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.
Obtain metadata of A Class You can obtain the metadata of an object's class by calling the following class methods:
- ClassC=[SelfClass];
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.
Dynamic Equation call I already haveThe method call section describes one aspect of reflection. 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
}
}
If you need an exact class match, instead of matching any inherited class, you can use ismemberofclass: method.
Check for agreement compliance Similar to 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:
- BoolConforms=[OBJConformstoprotocol: @ 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 ([OBJRespondstoselector: @ selector (amethod :)]){//It'sThere,SoWeCanCallIt[OBJAmethod: Yes];}
Of course, you can do more with objective-C reflection. Here I am just trying to talk about the most common application of reflection mechanisms. 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