Parsing Objective-C reflection

Source: Internet
Author: User
Tags c constructor

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:

 
 
  1. 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:

 
 
  1. for(BaseClass* base in myArray) {  
  2. if([base isKindOfClass:[ClassOne class]]) {  
  3. // do stuff specific to ClassOne  
  4. } else if([base isKindOfClass:[ClassTwo class]]) {  
  5. // do stuff specific to ClassTwo  
  6. } else if([base isKindOfClass:[ClassThree class]]) {  
  7. // do stuff specific to ClassThree }  
  8. // 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:

 
 
  1. 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:

 
 
  1. if([obj respondsToSelector:@selector(aMethod:)])   
  2. { // it's there, so we can call it [obj aMethod:YES];  
  3.  } 

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:

 
 
  1. Runtime Programming Guide: Introduction Runtime Reference 

Summary: AnalysisObjective-C reflectionI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.