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!

 

From: http://mobile.51cto.com/iphone-282485.htm

By sschu

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.