OC dynamic type, dynamic binding, dynamic loading

Source: Internet
Author: User

Objective-c has quite a lot of dynamic characteristics, basic, is also often mentioned and used to have

    • Dynamic type (typing)
    • Dynamic binding
    • Dynamically loaded (dynamic loading)
Dynamic type: The program cannot determine which class it belongs to until it executes.

ID data type
An ID-generic object type that can store an object of any type, with no number behind it, which is itself a pointer
Similar to void
, but can only point to object types

static types and dynamic types

    1. Compile time check and run-time check
    2. Static types can check for errors at compile time
    3. Static type declaration code good readability
    4. Dynamic types can only find errors at run time
Dynamic binding: The program will not determine the actual method to invoke until it executes.

Dynamic binding is done by binding certain properties and corresponding methods to the instance after the class of the instance is determined.

BOOL Type

BOOL is the C-language Boolean type, with True and False,bool is the Boolean type of Objective C, with Yes and no, because OC can be mixed with C, so bool and bool can appear in the code at the same time

BOOL in-depth parsing:
typedef signed Char BOOL;
The bool type has two values of yes,no. Yes=1,no=0.

Description: BOOL in objective-c is actually a type definition (typedef) of a signed character type (signed char) that uses 8 bits of storage space. Define yes with the # define directive as 1,no defined as 0.

  class class: Represents a class name, class is created,     We can take class as the class of the object. class cla1 = [class name class] Class CLA2 = [Object class] class cla3 = nsclassfromstring (@" class name ")           
    SEL 类成员方法的指针    可以理解 @selector()就是取类方法的编号,他的行为基本可以等同C语言的中函数指针,只不过C语言中,可以把函数名直接赋给一个函数指针,而Object-C的类不能直接应用函数指针,这样只能做一个@selector语法来取.    它的结果是一个SEL类型。这个类型本质是类方法的编号(函数地址)      1>类里面的方法都是被转换成SEL变量进行存储的。     2>放类声明一个对象,对象调用方法的时候,系统会被这个方法转换成SEL,然后拿这个SEL到类方法中去匹配。 3>我们可以自己手动把方法转换成SEL,然后用这个SEL去查找方法(performSelector)。
    -isMemberOfClass:    判断是否是这个类的实例    -isKindOfClass:    判断是否是这个类或者这个类的子类的实例    -respondsToSelector:    判读实例是否有这样方法    +instancesRespondToSelector:    判断类是否有这个方法。此方法是类方法。
Dynamic loading: Load the required resources on demand

This is easy to understand, for iOS development, the basic is to adapt to different models. The most classic example is the loading of @2x images on the retina device, and the loading of the original image on an older normal screen device. With the launch of the retina ipad and the advent of possible Retina Macs, this feature is believed to be increasingly being used.

Gifts

Polymorphism occurs in order for different classes to be able to use the same name method. This makes the program more readable, and it also reduces the difficulty of programming.

Dynamic type and dynamic binding is to solve the drawbacks caused by the convenience of multiple states, with dynamic type and dynamic binding, not to consider the output of the method is which type of method, will be automatically determined.

The appearance of ID type is to better take the return value of dynamic type and dynamic method.

OC dynamic type, dynamic binding, dynamic loading

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.