Run Time runtime

Source: Internet
Author: User

1. Methods for replacing private classes at Object-c run time

Runtime full interpretation

2. Add a method to an object at run time

C method form definition of the added method

void Hellolog (id self, SEL _cmd)

{

NSLog (@ "Hellolog");

}

Oc method form definition is added method

-(void) Hellolog

{

NSLog (@ "Hellolog");

}

Classes that require new methods

Class Newclass = [NSObject class];

C Method Form

Class_addmethod (Newclass, @selector (Hellolog), (IMP) hellolog, "[email protected]:");

Oc Method Form

Class_addmethod (Newclass, @selector (Hellolog), (IMP) hellolog, "[email protected]:");

Call New method

id instance = [[NSObject alloc] init];

[Instance Performselector: @selector (Hellolog) Withobject:nil];

The OBJECTIVE-C runtime defines several important types.

    • Class: Defining the Objective-c Class
    • Ivar: Defines an instance variable of an object, including the type and name.
    • Protocol: Define formal agreements.
    • objc_property_t: Defines a property. This name may be called to prevent conflicts with user types in Objective-c 1.0, when there are no attributes.
    • Method: Defines an object method or class method. This type provides the name of the method (that is, the * * selector), the number and type of parameters, and the return value (which together is called the * * Signature of the method), and a function pointer to the code (that is, the method's implementation * *).
    • SEL: Defines the selector. The selector is a unique identifier for the method name.
    • IMP: Defines the method implementation. This is just a pointer to a function that takes an object, a selector, and a variable-length argument list (varargs), returning an object

Run Time runtime

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.