Foundation = Objective-c _ Part3

Source: Internet
Author: User
Tags access properties


1. Property
    1. Basic use----the compiler will automatically generate a declaration of a property's Getter/setter method whenever it sees @property
2. @synthesize
    1. @synthesize is a compiler directive that simplifies the implementation of our Getter/setter method
    2. What is an implementation: writing curly braces after a declaration represents the implementation
    3. Tell the compiler after @synthesize which @property generated declaration needs to be implemented
    4. Tell @synthesize that you need to assign the value passed to WHO and return the value to the caller
    5. If you do not tell the system after the @synthesize to assign the value to whom, the system defaults to the same member variable that was written after the @synthesize with the same name.
3. Property Enhancements:
  1. The default @property assigns the passed-in property to the member variable that begins with _
  2. @property has one drawback: it generates only the simplest declarations and implementations of the Getter/setter method, and does not filter the incoming data
    • If you want to filter incoming data, then we have to rewrite the Getter/setter method
      • If the setter method is overridden, the property will only generate getter methods
      • If the getter method is overridden, the property will only generate setter methods
      • If the Getter/setter method is overridden at the same time, the property does not automatically generate a private member variable for us
    • If you do not want to filter the incoming data, simply provide a way for the outside to manipulate member variables, then you can use the @property
  3. If we use @property to generate the Getter/setter method, then we can not write the member variable, the system will automatically give us a _ beginning of the member variable
  4. Note: @property the member variable that is automatically generated for us is a private member variable, that is, generated in the. m file, not in the. h file.
    • Modifier of property
    • If a property is provided with a Getter/setter method, then we call this property a readable writable property (readwrite-is this by default)
    • If only the getter method is provided, we call this property a read-only property (ReadOnly)
    • If only the setter method is provided, then we call this property a write-only property (ReadOnly)
    • If neither getter nor setter method is provided, then we call this property a private property
    • There is a convention between programmers, generally get the value of the attribute of type bool, we will change the name of the obtained method to Isxxx
      • @property (getter=ismarried) BOOL married; is married
      • In this case, married's Getter method name is ismarried
      • Similarly, the method name of the setter can also be rewritten with the Setter= method name.
5. ID (Dynamic Data type)
  1. ID is a data type and is a Dynamic data type
    • Since it is a data type, it can be used to
      • Defining variables
      • As arguments to functions
      • As the return value of a function
  2. By default, all data types are static data types
    • Characteristics of static data types:
      • The type of the variable is known at compile time,
      • Know what properties and methods are in the variable
      • These properties and methods can be accessed at compile time,
      • And if you define a variable by a static data type, if you access properties and methods that are not part of the static data type, the compiler will error
  3. Features of Dynamic Data types:
    • At compile time, the compiler does not know the true type of the variable, but only when it is run to know its true type.
    • And if you define a variable through a dynamic data type, if you access properties and methods that are not part of the Dynamic data type, the compiler will not error
  4. ID = = NSObject * Universal pointer
    • The difference between ID and NSObject *:
    • NSObject * is a static data type
    • ID is a Dynamic data type
  5. You cannot call a subclass-specific method by defining a variable with a static data type
    • You can call a subclass-specific method by defining a variable with a dynamic data type
    • A variable defined by a Dynamic data type can call a private method
    • Disadvantage: Because the Dynamic Data type can call any method, it is possible to call the method that does not belong to itself, and compile without error, so it may cause runtime errors
    • Scenario: polymorphic, can reduce the amount of code, avoid calling subclass-specific methods require coercion type conversion
    • To avoid runtime errors raised by Dynamic data types, in general, if a variable is defined with a dynamic data type, a decision is made to determine whether the current object can invoke the method before invoking the object's method
id obj = [Student new];
             / *
                 if ([obj isKindOfClass: [Student class]]) {
             // isKindOfClass, to determine whether the specified object is a certain class, or a subclass of a certain class
              [obj eat];
              }
              * /

            if ([obj isMemberOfClass: [Student class]]) {
             // isMemberOfClass: determine whether the specified object is an instance of the currently specified class
             [obj eat];
             } 
6. New Method Implementation principle
    1. New has done three things.
      • Open storage space + Alloc method
      • Initialize all attributes (member variables)-init method
      • Returns the address of an object
    2. Example
// what alloc does: 1. open up storage space 2. set all properties to 0 3. return the address of the current instance object
     Person * p1 = [Person alloc];
     // 1. Initialize the member variables, but by default, the implementation of init does nothing 2. Return the initialized instance object address
     Person * p2 = [p1 init];
     // [[Person alloc] init];

     // Note: The address returned by alloc is the same address as the address returned by init
     NSLog (@ "p1 =% p, p2 =% p", p1, p2); 
7. Basic concepts of construction methods
    1. The method that starts with Init in OC, which we call the construction method
      • The purpose of a construction method: to initialize an object so that an object is created with some properties and values
    2. Overriding the Init method, initializing member variables in the Init method
      • Note: Overriding the Init method must be rewritten in the format specified by Apple, and will cause some unknown errors if not followed
      • The parent class must be initialized before the child class is initialized
      • The parent class must be judged to be successful, only the parent class initialization succeeds to continue initializing the subclass
      • Returns the address of the current object
      • Be sure to assign the return value of [Super init] to self
8. The difference between Instancetype and ID
    1. Instancetype = = ID = = Universal pointer = = Point to an object

      • ID cannot judge the true type of an object at compile time
      • Instancetype can determine the true type of an object at compile time.
    2. ID and Instancetype In addition to a compile-time do not know the real type, one at compile-time know the true type, there is a difference

      • The ID can be used to define a variable, which can be a return value, as a formal parameter
      • Instancetype can only be used as a return value
    3. Note: After any custom construction method, the return value uses Instancetype as far as possible, do not use the ID

9. Custom Construction Methods
    • Custom Construction methods:
    • is actually customizing an init method
      • Must be an object method
      • Must return to Id/instancetype
      • Method name must begin with Init
- (instancetype)initWithAge:(int)age;
    • A class can have 0 or more custom construction methods
    • A custom construction method can have 1 or more parameters
- (instancetype)initWithAge:(int)ageandName:(NSString *)name;
    • The representation of custom construction methods in inheritance
      • Note: Do your own thing (attribute is defined in which class, it should always be assigned by this class)
      • The order of the custom construction method init: Subclass init = parent class init = nsobject init = nsobject return = Return of parent class return = = Subclass
      • Note: The property name, do not start with new, it is possible to throw an unknown error; The method name does not start with new
11. Basic concepts of class factory methods
    1. What is a class factory method:
      • Class methods for quickly creating objects, which we call class factory methods
      • The class factory method is primarily used to allocate storage space to objects and initialize this storage space
    2. Specification:
      • Must be class method +
      • The method name begins with the name of the class, with the first letter lowercase
      • There must be a return value, the return value is Id/instancetype
    3. The custom class factory method is a specification for Apple, and in general we give a class A custom constructor method and a custom class factory method for creating an object
    4. The attention point of class factory method in Inheritance
      • Note: Any custom class factory method that creates an object in the class factory method must be created with self
      • Self in the class method represents the class object, exactly what class object is represented?? Who calls the current method, self represents who
13. Nature of the class
    1. The inheritance of "class object" of all classes is the inheritance relationship of "meta-class object"
    2. Each object has an Isa,
      • Instance Object ISA = Class object
      • Class object ISA = Meta class object
      • Meta-Class object ISA = Root meta-class object
      • The root meta-class object ISA = Root meta-class object (pointing to itself) the root meta-class object, which is NSObject
14. Acquisition and usage scenarios for class objects
    1. How to get Class objects
      • Syntax: Class test = [class name classes];
      • Note: A class has only one category object in memory
    2. Application Scenarios for Class objects
      • Used to create an instance object
      • Used to invoke class methods
15. Class Start-up process
    • As soon as the program starts, the code for the class is loaded into memory. Put it in the code area
    • The Load method is called when the current class is loaded into memory, and is called only once
      • If there is an inheritance relationship, the Load method of the parent class is called before the load method of the child class is called
+ (void) load {}
    • The Initialize method is called when the current class is first used (when the class object is created)
      • The Initialize method is called only once during the entire program's run, no matter how many times you use the class to call only once
      • Initialize used for one-time initialization of a class
+ (void) initialize { }
. SEL type
    • The first function of the SEL type, mate object/class to check if there is a method implemented in the object/class
      • Respondtoselector determine whether an object has implemented a specified method (object methods and class methods are automatically recognized based on the calling object)
SEL sel = @selector (setAge :);
     Person * p = [Person new];
     // Determine if the setAge: method at the beginning of the p object is implemented
     // If the P object implements the setAge: method, it will return YES
     // If the P object does not implement the setAge: method, then it will return NO
     BOOL flag = [p respondsToSelector: sell];
     NSLog (@ "flag =% i", flag);

     // respondsToSelector Note: If the method is called through an object, it will be determined whether the object implements the method starting with-
     // If the method is called through a class, it will be determined whether the class implements a method starting with +
     SEL sel1 = @selector (test);
     flag = [p respondsToSelector: sel1];
     NSLog (@ "flag =% i", flag);

     flag = [Person respondsToSelector: sel1];
     NSLog (@ "flag =% i", flag);
    • The second function of the SEL type, mated to an object/class to invoke an Sel method
      • Performselector can only be used to pass objects, 0 ~ 2 parameters
SEL sel = @selector (demo);
     Person * p = [Person new];
     // Call the method corresponding to the sel type in the p object
     [p performSelector: sel1];

     SEL sel1 = @selector (signalWithNumber :);
     // withObject: parameters to be passed
     // Note: If a method with a parameter is called via performSelector, the parameter must be an object type
     // That is, the formal parameter of the method must accept an object, because withObject can only pass one object
     [p performSelector: sel1 withObject: @ "13900000001"];

     SEL sel2 = @selector (setAge :);
     [p performSelector: sel2 withObject: @ (5)];
     NSLog (@ "age =% i", p.age);

     // Note: performSelector can only pass up to 2 parameters
     SEL sel3 = @selector (sendMessageWithNumber: andContent :);
     [p performSelector: sel3 withObject: @ "13900000001" withObject: @ "abcdef"];
    • Mate objects use the SEL type as the parameter of the method
 
Car *c = [Car new];
    SEL sel = @selector(run);

    Person *p = [Person new];
    [p makeObject:c andSel:sel];


Foundation = Objective-c _ Part3


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.