Li Hongqiang iOS classic face question 12

Source: Internet
Author: User
Tags gcd

1. Talk about the response chain

Answer: The incident response chain. Includes click events, screen refresh events, and more. Propagates from top to bottom or from bottom to top of the view stack.

It can be said that the distribution, delivery and processing of the incident. Specifically, you can look at the touch event this piece. Because the question is too abstract.

Serious doubts about the problem, the more the later the more general.

Can be from the chain of responsibility model, in terms of the incident response chain processing, its own extensibility

2. When to use Nsmutablearray, when to use Nsarray?

A: When the array is running in the program, it needs to be constantly changing, using the Nsmutablearray, when the array is initialized, it will no longer change, use Nsarray. It should be noted that the use of Nsarray only indicates that the array does not change at run time, i.e. it cannot add and remove elements to the Nsaarry array, but does not indicate that the contents of elements within its array cannot be changed. Nsarray is thread-safe, Nsmutablearray is not thread-safe, and multithreading is used to nsmutablearray needs attention.
3. What is a simple construction method?

A: The simple construction method is generally provided by the Cocoatouch framework, such as NSNumber + Numberwithbool: + Numberwithchar: + numberwithdouble: + numberwithfloat: + Numberwithint:

Most of the classes in the foundation have a simple construction method, we can get the system to create good objects by the simple construction method, and do not need to release manually.

4. When does the project choose to use GCD and when to choose Nsoperation?

A: The advantage of using nsoperation in a project is that Nsoperation is a highly abstract thread, and using it in a project will make the program structure of the project better, the sub-class nsoperation design idea, is has the object-oriented advantage (multiplexing, encapsulation), The implementation is multithreaded support, and the interface is simple, it is recommended to use in complex projects.

The advantage of using GCD in a project is that the GCD itself is very simple and easy to use, saving code for uncomplicated multithreaded operations, and the use of block parameters is more readable and recommended for simple projects.

5. What is block

A: There are many definitions for closures (blocks), where closures are functions that can read other functions ' internal variables, which are close to nature and better understood. For the first contact with the block of the classmate, will feel a bit around, because we are accustomed to write such a program main () {Funa ();} Funa () {Funb ();} Funb () {...}; Is the function main called function A, function A called function B ... Functions are executed sequentially, but not in reality, such as project manager M, who has 3 programmers a, B, C, when he gives programmer a to implement functional F1, he does not wait for a to complete, then to arrange B to implement F2, but to arrange for a function F1,b function F2,c function F3, Then maybe write a technical document, and when a has a problem, he will come to the project manager m, when B is done, will notify M, this is an example of asynchronous execution. In this case, block will be able to work, because in the project manager m, to a job, and will also tell a if encounter difficulties, how can find him to report problems (such as hit his mobile phone number), this is the project manager m to a callback interface, to return the operation, such as receiving telephone, Baidu query, Return Web page content to a, this is a block, in M confessed to work, has been defined, and obtained the F1 task number (local variable), but when a encountered a problem, only call execution, cross-function in the project manager m query Baidu, get results back to the block.

6.block Implementation principle

A: objective-c is an extension of the C language, and the implementation of block is based on pointers and function pointers.

From the development of computational language, the earliest goto, the pointer to the high-level language, to the object-oriented language block, from the machine's thinking, a step closer to human thinking, in order to facilitate developers more efficient, direct description of the logic of Reality (demand).

Working with instances

Block invocation of animation effect under Cocoatouch frame

Use typed to declare block

Typedef?void (^didfinishblock)? (NSOBJECT?*OB);
This declares a block of type Didfinishblock,

Then it will be available

@property? (nonatomic,copy)? Didfinishblock?finishblock;

Declare a BLOKC object, note that the object property is set to copy, and a copy is automatically copied when the block parameter is received.

__block is a special type,

A local variable declared with this keyword can be changed by block, and its value in the original function will be changed.

7. About Block

A: Interview, the interviewer will first ask, whether to understand the block, whether the use of blocks, these questions equal to the opening, often is the beginning of a series of questions, so be sure to truthfully according to their own situation to answer.

1). What are the advantages of using block and using delegate to complete the delegation mode?

The first thing to understand is the delegate mode, which is used extensively in iOS, which is an object adapter in the adapter pattern in design mode, and OBJECTIVE-C uses the ID type to point to everything, making the delegate mode more concise. Learn the details of the delegate model:

iOS design mode-delegate mode

The advantage of using block to implement the delegate mode is that the block code of the callback is defined inside the delegate object function, which makes the code more compact;

An adaptation object no longer needs to implement a specific protocol, and the Code is more concise.

2). Multi-Threading and block

GCD and Block

Using the Dispatch_async series method, the block can be executed in the specified manner

GCD Programming Example

Full definition of Dispatch_async

Void?dispatch_async (
Dispatch_queue_t?queue,
Dispatch_block_t?block);

Function: Commits an asynchronously executed block in the specified queue, without blocking the current thread

The thread that the block executes is controlled through the queue. The main thread executes the Finishblock object defined in the previous article

Dispatch_async (Dispatch_get_main_queue (), ^ (void) {Finishblock ();});

8. Talk about the Object-c memory management method and process?

A: 1). When you create an object using the New,alloc and copy methods, the object's retention counter value is 1. When you no longer use the object, you are responsible for sending a release or Autorelease message to the object. This will then be destroyed at the end of the service life.

2). When you get an object in any other way, assume that the object has a retention counter value of 1, and that it has been set to auto-release, you do not need to do anything to ensure that the object is cleaned up. If you intend to have the object for a period of time, you need to keep it and make sure that it is released when the operation

3). If you keep an object, you need to (eventually) release or release the object automatically. You must keep the Retain method and the release method equal in number of uses.

  

Li Hongqiang iOS classic face question 12

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.