The dynamics of Swift?

Source: Internet
Author: User

The important thing about dynamic is the ability to get all the methods and properties of a class, and we use the following code to print a list of methods and properties.

    • A function call of the pure Swift class is no longer a OBJECTIVE-C runtime message, but a C + + vtable that determines which function is called at compile time, so it is impossible to get the method or property through runtime.
    • TESTSWIFTVC inherits from UIViewController , the base class is NSObject , and swift in order to be compatible with the OBJECTIVE-C, the class that inherits from NSObject retains its dynamic nature, so we can get his method through runtime.
Method swizzling

The most common use of dynamics is method swizzling, which replaces a method of a class with a custom method to achieve hook action.

    • For pure Swift classes (such as Testaswiftclass), it is not possible to replace the method with the OBJC runtime because the above test shows that these methods, properties
    • For inheriting from the NSObject class (such as TESTSWIFTVC), the method that cannot be obtained by the runtime must not be replaced. Can the method that can be obtained by the runtime be replaced?

@objc

Used to export Swift's API to Objective-c and OBJECTIVE-C runtime, if your class inherits from a Objective-c class (such as NSObject), it will automatically be inserted into the @objc identity by the compiler.

Dynamic

加了@objc标识的方法、属性无法保证都会被运行时调用,

因为Swift会做静态优化。要想完全被动态调用,必须使用dynamic修饰。

使用dynamic修饰将会隐式的加上@objc标识

Summarize
    • The pure Swift class has no dynamics, but adding a dynamic adornment before the method or property can get the dynamics.
    • The Swift class that inherits from NSObject, which inherits from the parent class, is dynamic, and other custom methods and properties need to be modified to be dynamic.
    • If the parameters of the method, the property type is swift-specific, cannot be mapped to a type of objective-c (such as character, Tuple), then this method, property cannot add a dynamic adornment (compile error)
    • The Swift class will have a module prefix in the OBJECTIVE-C

The dynamics of Swift?

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.