Basic oc-property and point syntax

Source: Internet
Author: User
  1. Before learning attributes, review the previously learned setter and getter methods:

    (1) The role of the setter method: used to assign values to a single instance variable (only one instance variable can be used, but the get method is also used)

    (2) standard writing of the setter method:-method, no return value, name starting with set + name of the instance variable to be set (uppercase is required for the first letter) (The setter method has only one parameter) + parameter type (the same as the instance variable type) + parameter name (the same as the instance variable name)

    (3) The getter method is used to obtain the value of a single instance variable.

    (4) standard writing of the getter method:-number method. The method name has the same return value type as the instance variable type.

  2. When there are many instance variables, it is inconvenient to use the setter method and getter method, and the amount of code is also increased, so the attribute appears. the attribute is a new syntax after oc2.0, used to replace the setter and getter methods. You can use the attribute to quickly create the declaration of the setter and getter methods, as well as the implementation of the setter and getter methods. in addition, the attribute also adds security processing for instance variable operations.

    (1) attribute writing standard: @ property (used to define attributes) attribute type (the same as the instance variable type) attribute name (the same as the instance variable name)

    (2) Note: @ property automatically declares the setter and getter methods.

  3. Three attributes:

    (1) primary feature: read/write

    Readonly tells the compiler that only the getter method is generated when the property is automatically generated. The setter method is not generated.

    Readwrite tells the compiler that when attributes are automatically generated, both the setter method and the getter method must be generated, which is the default read/write feature of the system.

    Setter = AA; tells the compiler that when the setter method is automatically generated, the method name of the setter method is named AA, instead of the default

    Getter = BB; tells the compiler that when the getter method is automatically generated, the getter method name is the specified name BB, instead of the default

    (2) second-largest feature: atomicity

    Atomic: Atomic feature to ensure thread security. It implements Internal Security Processing (shackles and unlocking) and is the default atomic feature of the system.

    Nonatomic: Non-atomicity. thread security is not guaranteed because the setter and getter methods are frequently used and may require multiple accesses within a period of time, therefore, the use of atomic consumes a lot of system resources and reduces program execution efficiency. Although nonatomic does not guarantee thread security, it is usually safe to use. Therefore, for atomic features, use nonatomic

    (3) third feature: Semantic Features

    Assign: direct value assignment, applicable to basic data types, or for object types. It is the default semantic feature of the system.

    Copy: used for object types. Only objects that comply with the nscopying protocol can copy a new object and have new ownership of the object (reference count + 1) (I understand it for the time being)

    Retain: for object types, the reference count of the object is + 1.

  4. Attribute implementation:

    (1) writing standard: @ synthesize attribute name = instance variable (the instance variable to be operated inside the specified setter and getter methods)

    (2) If the instance variables specified in the standard writing method are not defined, the system will automatically generate the specified instance variables, but the generated instance variables are private and the subclass cannot be accessed directly, if the instance variable wants to allow subclass access. instance variables must be defined in the H file.

    (3) If the instance variables to be accessed by the setter and getter are not specified, the system automatically generates an instance variable with the same name as the property name.

    (4) For the setter and getter methods, if we implement them, the system will not automatically generate

    (5) If. in the M file, attributes are not merged using @ synthesize. The system automatically merges the attributes. However, the instance variables operated by the system's default setter and getter methods are _ attribute names.

    (6) If @ synthesize is omitted, the system will not generate the corresponding setter and getter methods and instance variables when we implement the setter and getter methods by ourselves.

    (7) It is often used to declare attributes only in. H files, and other systems automatically generate.

  5. Point syntax is a way to quickly access the setter and getter methods (similar to the previous structure variable to access the struct Member)

    Standard writing: Object Name. attribute name ----- if it is placed on the left of the equal sign, the setter method is called. In other cases, the getter method is called.

  6. The corresponding setter and getter methods are generated by default when attributes are declared. Therefore, attributes are associated with setter and getter methods.

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.