iOS Interview Summary (2022)

Self introduction

Hello interviewer, my name is Qiangqiang Tianxia. I graduated from Jiangsu Institute of Technology in 2014. I have about 8 years of experience in iOS mobile development. I worked for an e-commerce company Liancheng Data in 2013-15. I worked as an Android engineer and participated in the development of City and City APP. Development (a social e-commerce project) worked for Fangcun Technology in 15-16 years, is a software development company, worked in iOS development, and started to be responsible for Apple-side projects from 0-1, mainly responsible for an e-commerce project, 16 -So far, he has worked in a financial software company (completed the research and development of 2 standard products in the industry) and is mainly engaged in financial software development. Currently, he is the leader of the iOS development team of the company's domestic business mobile product line. He is mainly responsible for product demand docking, project structure construction, and core Functional development, including hybrid development technology implementation, H5, applet and other development work.

Memory Management - Attribute Modifiers

Read and write (readonly, readwrite)

Atomicity (atomic, noatomic) only guarantees that acquisition and assignment are quite thread-safe (set, get methods), but does not guarantee other operations of attributes, such as adding or deleting operations on arrays.

assign is used to assign values ​​to basic data types without changing the reference count. If it is used to modify the object (when the object is released, the pointer still points to the original memory address, and accessing it again will cause a wild pointer)

Weak modifies the Object type object. After the modified object is released, the pointer address will be set to nil, which is a weak reference.

strong is a strong reference type, and the reference counter is released when it is 0 with the number of references + 1.

weak When there are no more pointers of strong reference types pointing to it, it will be released, and all weak reference pointers will be cleared.

W e ak realization principle

Inside the dealloc method, the internal specific implementation,

Based on the runtime, the system maintains a weak reference table for reference objects. Through the hash algorithm, find the position of the abandoned object in the weak reference table, find the list array of weak reference pointers, and traverse them to change all weak reference pointers to nil

Block

Three block types, mainly to judge whether external variables are referenced and whether there is a copy operation, data storage location

Do not use external variables called global block

Blocks that use external variables and are not copied are stack blocks

The copy operation of the stack block is the heap block, and the copy operation of the global block is still the global block

Memory leak (circular reference) NSTimer, block

The circular reference can be broken through __weak in the block, and NSTimer avoids circular reference by encapsulating a management class

Block is modified with copy - in order to refer to external variables inside the block, the block is copied from the stack area to the heap area by default

Proxies are decorated with weak - to avoid circular references

Singleton, Proxy, Notification , KVC , KVO , B lock

Singleton: In the overall process, there is only one instance object, which shares a share of object resources, and the memory space is only allocated once.

Agent: Agents are generally used for one-to-one message delivery, which is different from the one-to-many delivery mode of notifications. The agent is the method of the entrusting party to let the entrusting party complete the specific implementation through the agreement, and the agent can return the return value to the entrusting party after completing the specific implementation. Declare the proxy through weak on the client side, mainly to avoid circular references.

Notification: Based on the observer mode, it is a mechanism for cross-layer message delivery, and the delivery method can be in the form of one-to-many.

KVO: It is another implementation based on the Observation Mode. The system is implemented through isa mixing technology. While addingObserver, the system will dynamically create a subclass of kvo_obj based on the original object as the parent class based on the runtime, and the subclass is rewritten. The setValue method of the parent class, and the isa pointer of the original class points to the new subclass, willChangeValueForKey, didChangeValueForKey will be called internally, and the callback method will be called to complete the observation process.

KVC: A method provided by the system to obtain or set the value of a variable through the valueForKey and SetValueForKey methods through the name of the value of the variable.

First, whether the instance variable accessed by key has a get method with the same name. If it exists, complete the call. If it does not exist, continue to query the member variable with the same name. If it does not exist, continue to query the parent class. .

Classification ( C at egory )

The implementation of classification is determined by the system at runtime. Classification can add classification to system classes, add instance methods, add attributes, etc. (you can’t add member variables when the classification is declared or implemented, you can use the method of associated objects at runtime add member variable)

If the method added by classification has the same name as the original class, it will overwrite the original class method in execution, and the effective order of multiple classification methods with the same name depends on the compilation order.

Extension ( Ex tension )

The implementation of the extension is determined by the system at the compilation stage. The extension can be used to declare private properties and private methods. The extension only exists in the form of declaration, and the specific implementation still needs to be in the original class.

Multithreading

Thread needs to manually manage the thread life cycle, it is not used much, and it is mostly used in permanent threads

First, the bottom layer of GCD is the interface of C, and Operation is a layer of encapsulation based on GCD.

GCD is simple and efficient to use. If it is only to speed up the operation of existing methods, it is more convenient to use GCD for lightweight operations, and it is used the most.

The Operation queue supports KVO, which can detect whether the thread is executing, whether it is finished (isFinisn), whether it is canceled (isCancel), and can control the amount of queue concurrency. One is to restart the start method, and you need to control all states by yourself.

The execution speed of GCD is a little faster than Operation

GCD ( ABC executes and executes D )

Dispatch_queue_t, concurrent concurrent queue, serial serial queue

Execute the previous request through dispatch_group, and finally call the dispatch_group_notify function to return to the main queue or other queues

GCD (multiple read single write)

Mainly write, read through dispatch_async, and write through dispatch_barrier

thread lock

NSLock

Recursive lock ( NSRecursiveLock ) can solve the deadlock of multiple locks in the same queue

The synchronized keyword is generally used when creating a single column

"Semaphore" in GCD

Deadlock:

Deadlock is that the queue causes circular waiting and cannot continue to execute

Simple example: use the main queue synchronization in the main thread

The synchronization task must wait for the current queue to be executed, and the current task and the current method are both on the main thread. The principle of first-in-first-out in the queue must wait for the current method to be executed, which in turn forms a mutual circular wait, resulting in a deadlock

Runloop

The thread resident mechanism is an infinite loop inside. It is true that the thread task can receive task messages, and can handle the dormant state when there is no task.

Realize thread resident

Create a runloop for the thread, add a source source to the runloop, and execute the runloop

R u nloop common mode has a default

(Default) mode, the default mode

(tracking) mode, interface tracking mode

(common) mode, a pseudo-mode, a technique for synchronizing source to multiple models

runlopp can only run in one mode. To switch between multiple modes, you must suspend the current mode and run another mode

Runtime

The method call-message mechanism of the system: the object searches the corresponding method implementation in the mapping table according to the method number SEL

1. Realization of dynamic exchange of two methods

2. Dynamically add attributes

3. Dynamic adding method

4. Intercept and replace method

5. Realize automatic conversion between dictionary and model

performance optimization

From the perspective of CPU and GPU

Avoid the calculation and drawing of the main thread process, complex multi-purpose multi-threading

Reduce transparent views and try to avoid off-screen rendering

UI level

Image processing: asynchronous request, local cache, on-demand request

List processing: reuse of cells, asynchronous processing of data, on-demand loading of images (off-screen rendering), avoid loading during sliding, asynchronous drawing of complex images, and avoid refreshing the entire list

Detect through the Instruments tool provided by Xcode:

1. Time Profiler: The CPU analysis tool analyzes the execution time of the code.

2.Leaks: memory detection, memory leak detection tool.

3. Core Animation: time-consuming detection of graphics dynamics.

off-screen rendering

Off-screen rendering refers to opening another buffer area in addition to the buffer area other than the current screen for off-screen rendering, so that the GPU can extract data from the buffer area and display it on the screen at a specified time. Usually, pay attention Avoid off-screen rendering, because it will consume CPU performance, but if you need CPU and GPU to do some rendering cooperation, you can also manually enable off-screen rendering to achieve a smooth interface

third party library

OC:Sdwebimage,AFNetworking,YYKit。。。

Swift:Kingfisher,Alamofir,SnapKit。。。

other

svn, git, Cocoapods, componentization, middleware, etc.

bonus:

Android, Vue, uni-app, React, Taro, applet, Flutter, RN, Java

Guess you like

Origin blog.csdn.net/u012378566/article/details/122497330