Objective-c introspection (Introspection) Usage Summary _c language

Source: Internet
Author: User

Introspection (introspection) is a powerful feature of object-oriented languages and environments, and objective-c and cocoa are particularly good at this aspect. Introspection is the ability of an object to reveal its detailed information as a Run-time object. These details include the location of the object on the inheritance tree, whether the object follows a specific protocol, and whether it can respond to specific messages, and so on. The NSObject protocol and class define a number of introspection methods that are used to query Run-time information in order to identify the object's characteristics.

Proper use of introspection can make object-oriented programs run more efficient and robust. It also helps to avoid incorrectly distributing messages and incorrectly assuming that objects are equal.

The following section provides an example of how to effectively use the NSObject introspection method in your code.

1, Iskindofclass:class

Check whether the object is that class or an object that its inheriting class instantiates

2, Ismemberofclass:class

Object that examines whether the object is that class but does not include an inherited class

Example:

The OBJECTIVE-C code is as follows:

if ([Item Iskindofclass:[nsdata class]]) { 
  const unsigned char *bytes = [item bytes]; 
  unsigned int length = [item length]; 
  // ... 
} 

If item is an object that is instantiated by the Nsmutabledata class, and the class is a subclass of the NSData class, then the value of [item Iskindofclass:[nsdata class] is true, and [item ismemberofclass:[ The value of NSData class] is false.
If item is an object that is instantiated by the NSData class, then the value of [item Ismemberofclass:[nsdata class] is true.

3, Respondtoselector:selector

Check if the object contains this method

The OBJECTIVE-C code is as follows:

-(void) Docommandbyselector: (SEL) Aselector { 
  if ([self respondstoselector:aselector]) { 
    [self Performselector:aselector Withobject:nil]; 
  } else { 
    [_client docommandbyselector:aselector]; 
  } 
} 

4, Conformstoprotocol:protocol

Checks whether the object conforms to the protocol and implements all the required methods in the protocol.

The OBJECTIVE-C code is as follows:

//... if (! [((ID) testobject) conformstoprotocol: @protocol (Nsmenuitem)])) {NSLog (@ "Custom MenuItem, '%@ ', not loaded; it must conform to the ' Nsmenuitem ' protocol.\n", [Testobject class]) 
  ; 
  [Testobject release]; 
Testobject = nil; }
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.