Objective-c Quick Start-Basics (Fri)

Source: Internet
Author: User

1. What is a property? What do attributes do for us? Please explain it in detail.

(1) The ① property is the syntax defined by OBJECTIVE-C 2.0, which provides the default implementation of setter and Getter methods for instance variables, ② to some extent simplifies the program code, and enhances the access security of instance variables.

(2) The ① property will default to the instance variable you declare, which will help you to get the Getter,setter method you need (which you do not declare, or that you did not implement); ② if you are not satisfied with the Getter,setter method generated by the default system The Getter,setter method that needs to rewrite an instance variable at the same time needs to be annotated @synthesize then use the instance variable to assign the Value property name//name = _name do not write the inverse.

2. What is attribute? How many kinds of attribute are there in the property?

(1) Objective-c provides properties to simplify programmer coding, and provides some keywords for attributes to control setter, getter Implementation details, these keywords we call attributes of properties (Attribute);

(2) Altogether 3 large categories of attribute;

(3) first Category: Literacy control (ReadOnly, ReadWrite, setter, getter)

①readonly, tells the compiler to declare only the Getter method (no setter method).

For example: @property (readonly) NSString *name; Equivalent to:-(NSString *) name;

②readwrite, tells the compiler to declare both the setter and the getter.

For example: @property (ReadWrite) NSString *name;  Equivalent to:-(void) SetName: (NSString *) name; -(NSString *) name;

③readwrite is the default setting for read-write control.

    Type II: Atomicity control (nonatomic, Atomic)

①atomic:setter, getter method in multi-threaded access is absolutely safe, that is, setter, getter inside do multi-threaded access processing (the default setting of atomic control is atomic);

②nonatomic:setter, Getter methods do not do multi-threaded access processing, just ordinary setter, getter method;

    Note:① program development process, setter, getter everywhere in use, if use atomic, need to constantly to setter, getter locking unlock to ensure thread access security, will occupy system resources, reduce system performance;

② is typically set to Nonatomic, and some properties are defined as atomic when they require thread safety.

For example: for example: @property (readwrite,nonatomic) NSString *name;  Equivalent to:-(void) SetName: (NSString *) name; -(NSString *) name;

    Class III: Semantic settings (assign, retain, copy)

①assign. The internal implementation of setter and getter is directly assigned value.

Example: for example: @property (nonatomic,assign) int age;

      

②retain. The internal implementations of setter and getter do memory optimization.

Example: for example: @property (nonatomic,retain) NSString *name;

      

③copy. The internal implementation of setter and getter will also do memory optimization.

Example: for example: @property (nonatomic,copy) NSString *name;

      

    Tip : ① If the semantic settings of properties are non-object types (such as int,float, etc.) use assign;

② If the attribute is a semantic setting of an object type (such as NSSTRNG, Nsarray, etc.) using retain;

③ If the property is an object type and you want to copy the parameter, use the Copy keyword.

3. What conditions must be satisfied to use dot grammar?

Point syntax can be used for all methods that conform to the system default setter and getter writing format.

For example: [Person setname:@ "Zhangsan"]; can be equivalent to write person.name = @ "Zhangsan";

NSString *name = [person1 name]; can be equivalent to write nsstring *name = person1.name;

4, the essence of using point syntax is to call the Setter,getter method. When is the setter method called and when is the getter method called?

The assignment operation is to invoke the Getter method when the setter method is called, and no assignment is just used as a value.

5. What is the full name of KVC?

KVC Full Name: (key-value-coding), key-value encoding, is an indirect way to access instance variables. Key: Keys for identifying instance variables; Vlaue: The value corresponding to the instance variable.

6, in KVC, give an instance variable of an object to assign a value, what method is used?

① Common key value: Setvalue:forkey:

② has the assignment of a hierarchical relationship: for example: The student's teacher's girlfriend [Stu TEA.GF] is used: Setvalue:forkeypath:

7. In KVC, if the instance variable corresponding to key does not exist in the process of assignment or value, what is the result of execution? How to avoid?

When key does not exist to prevent crashes, rewrite the setter method in the corresponding class. m file with Setvalue:forundefinedkey:

Example:

  

8. What is the method for batch assignment of an instance variable of an object in KVC?

Bulk assignments Place key-value pairs in a dictionary and then directly assign a value to the dictionary using setvaluesforkeyswithdictionary:

Email:[email protected]

Objective-c Quick Start-Basics (Fri)

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.