iOS Development Note Series-base 3 (polymorphic, dynamic type, and dynamic binding)

Source: Internet
Author: User

Polymorphic: same name, different class

The ability to make different classes share the same method name becomes polymorphic. It allows you to develop a set of classes that each of these classes can respond to the same method name. The definition of each class encapsulates the code required to respond to a particular method, making it independent of other class definitions. This is because the OBJECTIVE-C runtime system knows which object the recipient of the message is when executing the method, and it always carries information about what class an object belongs to, which enables the system to make decisions at run time, rather than at compile time.

Dynamic binding and ID types

The ID data type is a common type of object that can be used to store objects belonging to any class. When using the ID type, some of the program's tests are deferred from compile time to run time because the system is not yet able to determine what type of data type it is at compile time.

If you use a dynamic type to invoke a method, you need to be aware of the rule: if you implement a method with the same name in more than one class, each method must conform to the type of each parameter and the return value type so that the compiler can generate the correct code for the message expression.

The classes in objective-c inherit from the NSObject class, and this class supports methods that can be used to avoid errors or to check the integrity of the program at run time, as follows:

Method Problem or behavior
-(BOOL) Iskindofclass:class-object object is not a member of Class-object or its subclasses
-(BOOL) Ismemberofclass:class-object object is not a member of Class-object
-(BOOL) Respondstoselector:selector Object is able to respond to the method specified by the selector
-(BOOL) Instancesrespondtoselector:selector Whether the specified class instance can respond to selector
-(BOOL) Issubclassofclass:class-object Whether the object is a subclass of the specified class
-(BOOL) Performselector:selector Apply the method specified by selector
-(BOOL) Performselector:selector Withobject:object Apply selector specified method, pass parameter object
-(BOOL) Performselector:selector Withobject:object1 withobject:object2 Apply selector to the specified method, passing parameters Object1 and Object2

  

This allows you to make some judgments before calling a method on an ID type object to block possible errors and crashes.

In addition, you can use other policies such as using the Forwardinvocation: method to forward messages to other object processing.

The ID type can also be constrained in real-world development by a protocol, so the compiler can know in advance if the method call is correct.

iOS Development Note Series-base 3 (polymorphic, dynamic type, and dynamic binding)

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.