The static type

  • When a pointer variable is defined as a specific object, it is statically typed. The class to which the pointer variable belongs is known at compile time. This variable always stores objects of that particular class

    @interface Person : NSObject property (nonatomic,strong) NSString *name; - (void)run;
    @end
    @implementation Person
    -(void)run{
        NSLog(@"run");
    }
    @end
    int main(int argc, const char * argv[]) {
        Person *p = [[Person alloc]init];
        p.name = @"ABC";
        [p run];
        return 0;
    }
    Copy the code
  • Characteristics of static data types:

    1. The type of the variable is known at compile time
    We know at compile time that P is of type PersonCopy the code
    1. Know what properties and methods are in a variable
    @property (nonatomic,strong) NSString *name; - (void)run;
    Copy the code
    1. These properties and methods are accessible at compile time
     p.name = @"ABC";
      [p run];
    Copy the code
    1. And if you define variables with static data types, the compiler will report an error if you access properties and methods that are not static data types

Dynamic type

  • Refers to a program that does not determine which class an object belongs to until execution
    @interface Person : NSObject @property (nonatomic,strong) NSString *name; - (void)run;
    @end
    @implementation Person
    -(void)run{
        NSLog(@"run"); } - (void)test{
        NSLog(@"test"); } @end @interface Student : Person @property (nonatomic,assign) int age; - (void)eat;
    @end
    @implementation Student
    -(void)eat{
        NSLog(@"eat");
    }
    @end
    
    int main(int argc, const char * argv[]) {
        id obj1 = [[Person alloc]init];
        [obj1 run];
        id obj2 = [[Student alloc]init];
        [obj2 eat];
        return 0;
    }
    Copy the code
  • Characteristics of dynamic data types:
  1. The compiler does not know the true type of a variable at compile time, but only at run time
    id obj1 = [[Person alloc]init];
    id obj2 = [[Student alloc]init];
    Copy the code

idThe type andinstancetype

  • idtype
    • idIs a generic object type that can point to objects belonging to any class. It can be understood as a universal pointervoid*Data type,idIt is also a dynamic data type that can be used to define variables, as function arguments, as function return values, and so on
      • id == NSObject*(Universal pointer)
      • NSObject*Is a static type
    • Because dynamic data types can call any method, it is possible to call a method that is not their own without reporting an error at compile time, resulting in a runtime error
    • Usage scenario: Polymorphic, which reduces the amount of code required to call subclass-specific methods and avoids mandatory type conversions
    • The ID type cannot use dot syntax because dot syntax is a compiler property, while the ID type is a runtime property
    • Use static typing as much as possible. Static typing can catch errors earlier and improve readability
  • instancetype
    • instancetypeThe true type of an object can be determined at compile time
    • instancetypeCan only be used for return values
    • Custom constructor, return value as much as possibleinstancetype, do not useid