It’s recruitment season again. Here are some tips for iOS interviews. I hope you all get your expected salary!

More interview information

1. What is the difference between iOS classes and structs?

Class Temperature {var value: Float = 37.0} class Person {var temp: Temperature? func sick() { temp? .value = 41.0}} let A = Person() let B = Person() let temp = Temperature() a.temp b.temp = tempCopy the code

Temp (temp) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp (Temperature) temp The temp of A is changed, and the temp of B is also changed. The value of temp for both A and B is changed to 41.0. If Temperature is changed to struct, temp of A does not affect temp of B.

In memory, reference types such as classes are stored on the heap, and value types such as structures are stored and manipulated on the stack. Compared with operations on the stack, operations on the heap are more complex and time-consuming, so Apple officially recommends using a structure to improve the efficiency of App running.

Class has several functions that structs do not:

Classes can be inherited so that subclasses can use the features and methods of the parent class. Type conversions can be checked and interpreted at Runtime to free up resources.

The structure is small, suitable for copying operations, and more secure than multiple references to an instance of a class. Don’t worry about memory leaks or multithreading conflicts.

2. What is the COMMUNIST Party CD? What is your understanding of communist Party of China?

GCD stands for Grand Central Dispatch.

Grand Central Dispatch (GCD) is a relatively new solution to multi-core programming developed by Apple. It was first released in Mac OS X 10.6 snow leopard and was recently introduced to iOS4.0.

GCD is a very efficient and powerful alternative to technologies such as NSThread. GCD is perfectly capable of handling complex asynchronous programming problems such as data locking and resource leakage.

GCD can do a lot of things, but I’ll just focus on the basics of implementing multithreading in iOS applications.

Before we begin, it is important to understand that the GCD queues are provided with blocks of code that are scheduled to run on system or user-created queues. Declare a queue

A queue created by the user is returned:

dispatch_queue_t myQueue =  dispatch_queue_create("com.iphonedevblog.post",  NULL);
Copy the code

Where the first parameter identifies the queue and the second parameter defines the queue (currently not supported, so NULL is passed).

Executing a queue will execute the incoming code asynchronously:

dispatch_async(myQueue, ^{ [self doSomething]; }); Here, the queue created previously is passed in first, and the code block run by the queue is provided.

Declare and execute a queue. If you don’t need to keep a reference to the queue to run, you can do this with the following code: Dispatch_async (dispatch_queue_create (“com.iphonedevblog.post”, NULL), ^{[self doSomething]; }); If you need to pause a queue, you can call the following code. Pausing a queue blocks all code associated with that queue. dispatch_suspend(myQueue);

Pause a queue Don’t forget to recover if you pause a queue. The operations for pause and restore are similar to retain and release in memory management. Calling dispatch_suspend increases the pause count and dispatch_resume decreases. Queues only start running if the pause count becomes zero. dispatch_resume(myQueue);

Some operations cannot be run on asynchronous queues, so they must be run on the main thread (one for every application). UI drawing and any calls to NSNotificationCenter must be made at the main thread length. An example of accessing the main thread in another queue and running code is as follows: dispatch_sync(dispatch_get_main_queue(), ^{[self dismissLoginWindow]; }); Note that dispatch_suspend (and dispatch_resume) does not work on the main thread.

With GCD, you can keep your program from becoming unresponsive. Multithreading is not easy to use, using GCD makes it easy. You don’t have to do thread management, great!

  dispatch_queue_t   t1=dispatch_queue_create("1"  ,NULL); 
  dispatch_queue_t   t2=dispatch_queue_create("2",NULL); 
  dispatch_async(t1, ^{ 
      [self print1]; 
    }); 
  dispatch_async(t2, ^{ 
      [self print2]; 
    }); 
Copy the code

3. What is iOS auto release pool and how does it work?

When you send an AutoRelease message to an object, Cocoa puts a reference to that object into the latest automatic release pool. It is still a valid object, so other objects in the scope defined by the autorelease pool can send messages to it. When the program executes at the end of the scope, the auto-release pool is released, and all objects in the pool are released.

1. Object-c contains its own memory and its own “reference counting”, referring to its own memory allocation. The object has a single reference count when it allocates its own memory, and a single reference count when it contains a copy of its own memory. The reference count is reduced by one whenever release and autoRelease are encountered, and if the object’s count becomes zero, it is destroyed by the system.

NSAutoreleasePool is used to manage reference counts, which is usually not your job.

3. There is no difference between an autoRelease and a release except when the reference count is decrement by one, and an Autorelease does this only when the use of the object is actually over.

4. IOS can a tableView be associated with two different data sources? How would you handle it?

Answer: First we look at the code, how the data source association, actually is implemented in the data source association method. So we don’t care how we associate it, how it associates, the method is just let me go back and set up the relevant data source as I want. So, I think you can set up multiple data sources, but the question is, what are you trying to do? How do you want the list to be displayed, in different data source partition blocks?

5. What does the iOS category do? What is the difference between inheritance and categories in implementation?

A: You can add new methods to a category without knowing or changing the original code. You can only add, not delete. And if there is a name conflict between a category and a method in the original class, the category overrides the original method because the category has higher precedence. Categories have three main functions:

(1) Split the implementation of the class into multiple different files or multiple different frameworks.

(2) Create forward references to private methods.

(3) Add informal protocols to objects. Inheritance can add, modify, or delete methods, and can add attributes.

6. What happens from entering the URL to presenting the page

The browser sends an HTTP request to the Web server. The server responds with a permanent redirect. The browser traces the redirect address HTML 9. Browsers send requests for resources embedded in HTML (such as images, audio, video, CSS, JS, etc.)

7. What are the persistence methods in iOS?

Property list file — The store of NSUserDefaults actually generates a plist file locally and stores the desired properties in the plist file

Object archiving – Files are created locally and data is written to any file type

SQLite Database – Create database files locally for data processing

CoreData – Same idea as database processing, but implemented differently

8. Which timers do you often use in the development process? Is there any error in the timer time?

In iOS, NSTimer, CADisplayLink and GCD timer are common. NSTimer and CADisplayLink are implemented based on NSRunLoop, so there are errors. GCD timer only depends on the system kernel, which is more punctual than the first two.

The error is caused by: It is related to the NSRunLoop mechanism, because RunLoop checks whether the current accumulated time has reached the interval set by the timer after each lap. If not, RunLoop will enter the next task and check the current accumulated time after the task is finished. In this case, the total time may exceed the interval of the timer, so there is error.

9. How to prevent the use of Pointers out of bounds?

You must have a pointer to a valid memory address,

1, prevent arrays from out of bounds 2, prevent from copying too many contents into a block of memory 3, prevent from null pointer 4, prevent from changing const pointer 5, prevent from changing contents pointing to static storage 6, prevent from releasing a pointer twice 7, prevent from using wild pointer.

Have you ever used Runtime on a project? For example

Objective-c is a dynamic language. The compiler does not care about the type of object receiving the message, and the object receiving the message is handled at run time.

Pragramming level runtime is mainly embodied in the following aspects:

Associated Objects Associated Objects forward messages sent Messaging Message Forwarding Method mixing Method Swizzling “object” NSProxy Foundation | Apple Developer Documentation KVC, KVO About key-value Coding # Concludes this article. The next article will continue to bring you interview materials for other knowledge points. If you have any comments and suggestions, please leave a message to me. See you in the next article!

More interview information

— Original address