Li Hongqiang Classic iOS face question 11

Source: Internet
Author: User
Tags shallow copy

 
    1. What is the difference between #import and # include, @class, #import<> and #import "" What difference?
    2. #import是Objective-C keyword to import the header file, #include是C/c++ import header file keyword, using #import header file will automatically import only once, will not repeat the import, equivalent to # include and #pragma once @class tell the compiler of a class declaration, when executed, to see the implementation of the class file, you can resolve the header file of each other, #import <> used to include the system header file, #import "" to include the user header file.

What is the nature of the object declared by 2.id?

An Id-declared object has a runtime attribute, which can point to an object of any type of objcetive-c;

3. What is the difference between an atom (atomic) and a non-atomic (non-atomic) attribute?

    • Atomic provides multithreading security. is to prevent it from being read by another thread when the write is incomplete, resulting in a data error

    • Nonatomic: In an environment where you manage memory, the accessor that is parsed retains and automatically frees the returned value, and if Nonatomic is specified, the accessor simply returns the value.

4. What are the several principles of memory management? Follow the default rules. Those keyword-generated objects

Need to be released manually? How to effectively avoid memory leaks when combined with property?

Who applies, who releases

Follow the cocoa touch usage principles;

Memory management mainly to avoid "premature release" and "Memory Leak", for "premature release" need to pay attention to the @property setting characteristics, must use the keyword to the attribute, for "memory leak", must apply for the release, be careful.

The object generated by the keyword Alloc or new needs to be released manually;

Set the correct property properties, for retain need to be released in the appropriate place,

5. What is the difference between a shallow copy and a deep copy?


deep copy: Copy the Reference object itself.  

There is only one copy, so if we do a modification to a_copy, we find that the object referenced by A is also modified. This is in fact a violation of our idea of copying copies. Deep copy is good to understand, there is   in memory;
Two copies of the independent object itself.

6. What is the role of the category? What is the difference between inheritance and category in implementation?
Answer: Category can be in the case of not knowing, without changing the original code to add a new method, can only add, can not delete the changes.
And if the category and methods in the original class produce name collisions, the category overrides the original method because the category has a higher priority.
There are 3 main functions of the category:
(1) The implementation of the class is dispersed across several different files or multiple frameworks.
(2) Create a forward reference to the private method.
(3) Add an informal agreement to the object.
Inheritance can add, modify, or Delete methods, and can add properties.

7. What are Kvo and KVC?
Answer: KVC: Key – Value encoding is a mechanism that indirectly accesses an object's properties by using a string to identify the property, rather than by invoking an access method, either directly or through an instance variable.
In many cases, the program code can be simplified. The Apple documentation actually gives a good example.

KVO: A key-value observation mechanism that provides a way to observe a change in a property, greatly simplifying the code.
In particular, to see that one of the places that I used to be is the monitoring of the button click Change state.

8. What is deferred loading?
Answer: Lazy mode, only in the use of the time to initialize.
Can also be understood as delayed loading.

9. How do I save data in the iphone app?

Answer: There are several preservation mechanisms:

1. Save on the server via the Web service

2. Save the object in a file by nscoder the curing mechanism

3. Save in the file database via SQLite or CoreData

10.objective-c How to manage the memory, say your opinion and solve the method?

A: Objective-c's memory management mainly has three ways arc (automatic memory count), manual memory count, memory pool.

1). (Garbage Collection) automatic memory count: This way is similar to Java in the course of your program's execution. Always have an expert in the back to help you clean up the rubbish, you do not have to consider when it began to work, how to work. You just need to understand, I applied for a memory space, when I no longer use so this memory becomes garbage, I completely forget it, anyway, the high man will help me to clean up the rubbish. Unfortunately, the man needs to consume a certain amount of resources, in the carrying device, the resources are tight goods so the iphone does not support this feature. Therefore, "garbage Collection" is not the scope of this introductory guide, the "garbage Collection" internal mechanism interested students can refer to some other information, but to tell the truth "garbage Collection" is not suitable for beginners to study.

Resolution: Created by Alloc–initial, after the reference count of +1, after each retain reference count +1, then in the program to do the corresponding number of release is good.

2). (Reference counted) manual memory count: That is, after a period of memory is applied, there is a variable to save the memory is used in the number of times, we temporarily call it a counter, when the counter becomes 0, then the time to release the memory. For example, when a piece of memory in program A is successfully applied, then the counter is changed from 0 to 1 (we call this process alloc), then program B also needs to use this memory, then the counter is changed from 1 to 2 (we call this process retain). Then program a no longer needs this memory, then program a will reduce this counter by 1 (we call this process release), program B also no longer need this memory, then also the counter minus 1 (this process or release). When the system (that is, the foundation) discovers that the counter is a member of 0, then the memory recycle program is called to recycle the memory (we call this process dealloc). By the way, if there is no foundation, then maintenance counters, free memory and so on, you need to do the work manually.

Workaround: Typically created by static methods of the class, the function name does not appear in the Alloc or Init typeface, such as [NSString string] and [Nsarray Arraywithobject:], after the creation of the reference count +0, after the function is released after the stack, is equivalent to a local variable on a stack. Of course, you can also extend the lifetime of an object by retain.

3). (Nsautorealeasepool) Memory pool: You can control the timing of memory requests and recoveries by creating and freeing memory pools.

Workaround: The autorelease is joined to the system memory pool, the memory pool is nested, and each memory pool needs to have a create release pair, just like the one written in the main function. The use is also very simple, for example [[[[NSString alloc]initialwithformat:@] Hey you! "] Autorelease], a NSString object is added to the most internal system memory pool, and when we release the memory pool, the objects are freed.

11. What is the difference between an atom (atomic) and a non-atomic (non-atomic) attribute?

For:

1). Atomic provides multithreading security. is to prevent it from being read by another thread when the write is incomplete, resulting in a data error

2). Non-atomic: In the environment in which it manages memory, the accessor that is parsed retains and automatically releases the returned value, and if Nonatomic is specified, the accessor simply returns the value.

12. What is the MVC design pattern? What design patterns are you familiar with?

For:

Design pattern: Not a new technology, but a coding experience, using such as Java interface, the iphone protocol, inheritance relations and other basic means, with more mature logic to deal with a certain type of things, summed up as the so-called design patterns. In object-oriented programming, Java has summed up 23 design patterns.

MVC design Pattern: Model, view, controller, can divide the whole application into three chunks, corresponding to the storage or processing of data, foreground display, business logic control. The idea of the iphone itself is to follow the MVC design pattern. It is not part of the 23 design pattern categories.

Proxy mode: Proxy mode provides a proxy object for an object, A reference to the source object is controlled by the proxy object. For example, a factory produces products, and do not want to sell directly to users, but to engage in a lot of agents, users can directly find agents to buy things, agents from the factory stock. Common such as QQ automatic reply belongs to agent interception, proxy mode is widely used in iphone.

Singleton mode: Plainly, a class does not create an object by Alloc, but instead returns the object of that class in a static way. The system only needs to have a global object, which helps us coordinate the behavior of the system as a whole, for example, to obtain [UIApplication sharedapplication]; any local call can get UIApplication object, this object is globally unique.

Observer pattern: When an object changes, all observers who observe the object are notified to react. All that is accomplished is to give the object of all the observer, and when that object changes, it invokes the observer's method of iterating over all the Observer's object to inform the observer.

13. What is the difference between inheritance and category in implementation?

A: Category can add new methods without knowing, without changing the original code, can only be added, cannot delete changes, and if the category and methods in the original class produce name conflicts, the class overrides the original method because the category has a higher priority.

14. There are 3 main functions of the category:

1). Spread the implementation of the class across multiple different files or multiple different frameworks.

2). Create a forward reference to the private method.

3). Add an informal agreement to the object.

Inheritance can add, modify, or Delete methods, and can add properties.

For the understanding of the singleton

A: To implement a singleton class in objective-c, you need to do at least the following four steps:

1). Implement a static instance for the Singleton object, initialize it, and set it to nil,

2). Implement an instance construction method to check if the static instance declared above is nil, and if it is new and returns an instance of this class,

3). Rewrite the Allocwithzone method to ensure that others directly use alloc and Init to try to gain a new strength without creating a new instance,

4). Allocwithezone,copywithzone,release and Autorelease are properly implemented.

   

     

Li Hongqiang Classic iOS face question 11

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.