iOS face question 01

Source: Internet
Author: User

1. What is the difference between #import和 # include, @class? What is the difference between #import <> and #import ""?

A: 1. #import和 # include can contain the contents of a file completely, #import能防止同一个文件被包含多次;

[email protected] simply declares a class name and does not contain a complete declaration of the class; @class can also solve the problem of the loop;

3. #import <> is used to include the file that comes with the system, #import "" to include the custom file.

2. What are the functions of ReadWrite, ReadOnly, assign, retain, copy, Nonatomic, Atomic, strong, weak? In which case is it used?

1.readwrite: Both the Declaration and implementation of the Get method and the set method are generated; This property is read-write and is the default property;

2.readonly: Only the Declaration and implementation of the Get method is generated; it is read-only, and in @implementation, only one reader is required. If you attempt to assign a value to a property using the dot operator, you will get a compilation error.

The implementation of the 3.assign:set method is a direct assignment for the base data type and does not change the index count. In an application that uses garbage collection, if a property uses assign and the class conforms to the nscopying protocol, it is necessary to explicitly indicate this tag instead of simply using the default value, otherwise a compilation warning will be given. (This again explains to the compiler that you really need to assign a value, even if it is a copy.) Not understand)

The implementation of the 4.retain:set method is the release old value, retain new value, for the OC object. Not for Core Foundation objects (because the core Foundation object does not have a reference count)

eg

-(void) SetName: (NSString *) name{    if (_name! = name) {        [_name release]        ; = [name retain];    }}

The implementation of the 5.copy:set method is the release old value, copy new value, for NSString, block and other types.

-(void) SetName: (NSString *) name{    if (_name! = name) {        [_name release];         = [name copy];    }}

The difference between copy and retain *******

Copy is the same object that was created, and retain is not.

such as a NSString object, the address is Ox000111, the content is "name"

Copy to another nsstring, the address is 0x000222, the content is the same, the new object retain is 1, the old object has not changed;

After retain to another nsstring, the address is the same, 0x000111, the content is the same, the old object's retain value +1;

end**************

6.nonatomic: Non-atomic, the implementation of the set method is not locked, high performance than atomic, multi-threaded concurrent access can improve performance. If this attribute is not added, the default is two access methods are atomic transaction access.

7.atomic: atomicity, when setting the @property property of a member variable, the default is atomic, which provides multithreading security. The setter function will change to the following:

-(void) SetName: (NSString *) name{    {lock}    if (_name! = Name ) {        [_name release];         = [name retain];    }    

Nonatomic prohibit multi-threading, variable protection, improve performance.

Atomic is a thread-protection technique used by OBJC, which is a resource-intensive way to prevent the data from being read by another thread when the write is not completed. If you are not programming with multithreaded communication, nonatomic is a good choice. If you do not specify nonatomic, the parsed accessor retains and automatically releases the returned value in the environment in which it manages the memory, and if nonatomic is specified, the accessor simply returns the value.

8.strong: As with retain, when used, the reference count is also added. When all the strong types that point to it are released, it is freed, so the weak type that points to it is emptied.

9.weak, weak reference, when circular reference, use weak.

Not to be continued--

iOS face question 01

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.