(iOS Interview Guide)
# iOS classic Interview Questions 2021 (part 1)
36. Describe the realization principle of ARC. When does it insert retain/release?
ARC: Automatic reference count. It will automatically insert retain/release when the object is created or dies. To achieve the purpose of automatic memory management.
What are the differences between Framework and Library? What’s the difference between a dynamic library and a static library?
Differences between Library and Framework:
In iOS, the Library can only contain compiled code, i.e..a files. But in general, a complete module may contain not only code, but also.h headers,.nib view files, image resource files, and documentation. Libraries like those provided by UMeng are not easy to configure by dragging a bunch of files into Xcode. The Framework is used in Cocoa/Cocoa Touch as a way to package resources such as the above files in a cluster for easy use by developers (like bundles).
The difference between static and dynamic libraries:
In simple terms, a statically linked library is when modules are compiled and incorporated into an application that is larger in itself but no longer needs to rely on third-party libraries. When you run multiple applications that contain the library, there will be multiple copies of the library in memory, redundancy. Dynamic libraries can be published separately, found and loaded into memory at run time, and if there are common libraries, they can be shared, saving space and memory. Libraries can also be upgraded individually or released as plug-ins.
38. What is runloop?
In general, a thread can only execute one task at a time, and then it exits. If we need a mechanism that allows a thread to handle events at any time but not exit. The code logic is as follows:
function loop() { initialize(); do { var message = get_next_message(); process_message(message); } while(message ! = quit) }Copy the code
This model is often referred to as an EventLoop. A RunLoop is actually an object that manages the events and messages it needs to process. An entry function is provided to perform the above EventLoop logic. In OSX/iOS, two such objects are provided: NSRunLoop and CFRunLoopRef.
CFRunLoopRef is within the CoreFoundation framework and provides apis for pure C functions, all of which are thread safe.
NSRunLoop is a wrapper based on CFRunLoopRef and provides object-oriented apis that are not thread-safe.
Apple doesn’t allow you to create a RunLoop directly, but only provides two automatically fetched functions: CFRunLoopGetMain() and CFRunLoopGetCurrent().
There is a one-to-one correspondence between threads and runloops.
What is the difference between #include and #import? #import @class
“#include”, like” #import”, exports headers. However, #import does not cause cross-compilation and ensures that header files are imported only once. #import will contain all the information about this class. It includes instance variables and methods, and @class just tells the compiler that the name it declares after it is the name of the class, and the definition of the class, I’ll tell you later. Using #import is efficient and prevents compilation errors that include each other.
What is the difference between Static and const?
Const indicates read-only and is used only in declarations.
Static generally has two functions, specifying the scope and the storage method. For local variables, static specifies that they are static storage, that each call starts with the value of the last call, and that the storage space is not freed when the call ends.
For global variables, if scoped by a file, the variable is visible only in the current file. For static functions, it is also visible in the current module function.
Describe the difference between GET and POST requests.
(1)POST needs to specify that GET is not required, and the default is GET, and GET has cache, and POST does not have cache
(2) The GET parameter is placed after the URL, and the first parameter is? Concatenation, starting with the second argument until the last one, if there are more than one, split by &; The POST argument is put in the body of the request, and the first argument, no, and then the second argument, until the end, if there are more than one, split by &;
(3)GET is generally used to obtain data, and POST is used to submit data to the server
(4) The parameter of GET is exposed in the address bar, which is not safe; The POST parameter is hidden in the request body, which is relatively safe.
(5)GET request has no request body, while POST request has a request body.
(6) The data submitted by GET request is limited by browser, 1K, and POST request is theoretically unlimited.
42. What happens in memory when an attribute is __block?
What __block does is that the compiler creates a function for you that you can use to change the value of the variable that the block captures.
Talk about the difference between blocks and functions.
Blocks can be written inside methods, functions need to be written outside methods. Blocks can access local variables in methods.
44. Do you know runloop? How many modes does it have? Let me just briefly describe the application scenarios.
Runloop mode:
Default:
NSDefaultRunLoopMode (Cocoa), kCFRunLoopDefaultMode (CoreFoundation)
Connection:
NSConnectionReplyMode
Modal:
NSModalPanelRunLoopMode
Event tracking:
NSEventTrackingRunLoopMode
Common modes:
NSRunLoopCommonModes (Cocoa), kCFRunLoopCommonModes (CoreFoundation)
Application scenario:
(1) Communicate with other threads using a port or custom input source.
(2) Use timers in threads
(3) use any performSelector method
(4) Keep the thread to execute a periodic task.
45, What versioning tools do you use in your work?
Use git tools for version management.
46. Have you ever used git? What common commands have you used?
Git init, git add, Git Commit, Git merge, Git branch, Git checkout, Git pull, Git push etc
What types of animations are commonly used by CoreAnimation?
All core animation animation classes are inherited from the CAAnimation class. CAAnimationGroup group animation, CATransition transition animation, CABasicAnimation: Basic animation, CAKeyframeAnimation.
48. How many queues does the GCD system provide?
Two types are DispatchSerialQueue and DispatchConcurrentQueue.
What is the concept and time complexity of binary search tree?
O(n)
“Weak self” in a block
I don’t know. It can be added or not. Setting blocks to nil can also break a circular reference.
51, GCD queue, main queue, must be executed on main thread?
Yes. Must be in the main thread.
52. Have you ever encountered any problems with the database? How to solve it?
Encountered multithreaded operation database read and write deadlock problem, using the FMDB provided in the way of rollback to solve
A brief introduction to sandbox in iOS.
The sandbox mechanism in iOS is a security system that states that an app can only read files in the folder created for that app and cannot access content elsewhere. All non-code files are kept in this place. Such as pictures, sounds, property lists and text files.
(1) Each application is in its own sandbox
(2) You cannot cross your own sandbox to access the content of another application’s sandbox
(3) Permission authentication is required for the application to request or receive data.
You can only read and write files in a few directories because your application is in a sandbox:
Documents: User data in the application can be stored here, and iTunes backup and recovery will include this directory
TMP: Stores temporary files. ITunes will not back up and restore this directory. Files in this directory may be deleted after the application exits
Library: Stores default Settings or other state information for an application.
Library/Caches: Stores cached files. ITunes does not back up files in this directory, and files in this directory will not be deleted when the app exits
54, why do strings need to use copy?
To prevent the mutableString from being inadvertently modified, NSMutableString is a subclass of NSString. So an NSString pointer can hold an NSMutableString.
What is the difference between nonatomic and atomic?
Atomic is a thread-protection technique used by Objective-C to prevent an unfinished write from being read by another thread. Thus causing data errors. This mechanism is very resource-intensive, so on a small mobile device like the iPhone, without using multithreaded communication programming. Nonatomic is recommended.
Default accessor is atomic operation, that is, in a multithreaded environment, parsing the visitor provides an access to property security, the return value from getter or by setting the setting values can be completed at a time, even a multithreaded in access to, if you do not specify nonatomic, in their own memory management environment, The parsed accessor retains and automatically releases the reserved value. If nonatomic is specified, the accessor simply returns a value.
What’s the difference between at sign synthesize and at sign dynamic?
@synthesize: If you don’t implement the setter and the getter manually, the compiler will automatically generate the setter and the getter for you.
@dynamic: Tells the compiler that setters and getters for properties are implemented by the user and are not generated automatically. If a property is declared @dynamic var, and there is no setter and getter, it will compile fine. If the program runs to person. Name = newName; Or newName=person.name, causing the program to crash. Unrecognized Selector sent to instance… This is called dynamic binding.
57, @ property (copy) NSMutableArray * array; What’s wrong with this code? If yes, please describe the reason;
(1) When adding, deleting, or modifying elements in an array, the program will crash because it cannot find the corresponding method, because copy is to copy an immutable NSArray object.
(2) No nonatomic modifiers are used. The default is atomic, which can seriously affect performance.
58, NSString * STR = @hello world! With NSString * STR = [[NSString alloc] initWithString:@” Hello world! “] ; What are the differences in terms of memory management?
In MRC, the former means that you do not hold the object, so you do not need to manually manage its memory. The latter means that you hold the string and need to manually manage the memory yourself to free it.
NSString*obj = [[NSData alloc] init]; What type of object is obj at compile time and run time?
It’s an NSString object at compile time, an NSData object at run time and first of all, declaring NSString star testObject tells the compiler that obj is a pointer to some Objective-C object, because no matter what type of object it’s pointing to, The amount of memory a pointer occupies is fixed, so the resulting executable code is the same for any type of object declared here. So the NSString limitation here just tells the compiler, please check obj as if it were an NSString, and if you call a method that’s not an NSString, you get a warning, and then you create an NSData object, and you store the memory address of that object in obj. So at run time, the memory space that OBj points to is an NSData object. You can use obj as an NSData object.
60, self.name=object and name=object
The former sets the value by calling the setter method, while the latter is a normal assignment operation.
Why is copy not mutableCopy in property declaration?
This is because when a property is declared as a “copy” feature, the system automatically determines whether to use copy (including retain) or mutableCopy depending on the receiver’s characteristics.
What is the difference between id and void* in Objective-C? What is the difference between ID and InstanceType? What’s the difference between nil, null, and null?
In Objective-C, id and void* are different:
Id is a pointer to an object of class OC, and it can be declared as a pointer to any object of class, and when you use id in OC, the compiler assumes that you know which object of class ID is pointing to. Unlike void, the compiler does not know or assume a pointer to any type.
Id = instanceType
The id returns the ID type, and the instanceType returns the type of the class.
The similarity is that they are both return types of methods.
The difference between:
(1) InstanceType can return an object of the same type as the method. Id can only return an object of an unknown type.
(2) InstanceType can only be used as a return value. Id can be used as an argument.
Nil, nil, NULL:
Nil is a pointer to an object that does not exist (an object null pointer). Nil is a pointer to a class (nulllike pointer) that does not exist. NULL points to a NULL pointer of another type. NSNull in collection objects, an object that represents a null value.
63. How many ways are there to parse data? What’s the difference between them? What kind of data parsing are you using in your project?
JSON and XML.
64, use the preprocessor instruction #define to declare a constant indicating how many seconds there are in a year (ignore the leap year problem)
“# define SECONDS_PER_YEAR (606024 * 365) UL”
65, write a “standard” macro MIN that takes two arguments and returns the smaller one.
“# define MIN (X, Y) (X) > (Y)? (X) : (Y))”
What is the difference between +load and +initialize?
+initialize: This class is called before it is first initialized. We use it to initialize static variables.
The +load method will be called when the class is loaded, so when the iOS app starts, all the classes will be loaded.
The +initialize method is similar to a lazy load. If the class is not used, it is not called by default and is only loaded once.
If you are in a class, the +load method will execute all of them, but the load method in the class will override the method in the class, and the +initialize method will override the method in the class, executing only one.
The call to +initialize occurs before the +init method. The subclass calls the +initialize method of the superclass.
The difference between new and alloc/init
In summary, new and alloc/init are almost identical in functionality, allocating memory and doing initialization. The difference is that the new approach can only do the initialization using the default init method. Alloc allows you to use other custom initialization methods.
If you design interfaces and apis, what should you pay attention to?
(1) Use prefixes to avoid namespace conflicts
(2) Provide “all-powerful initialization method”
(3) Implement the description method
(4) Try to use immutable objects
(5) Use a clear and coordinated naming method
(6) Prefix the private method name
(7) Error handling
(8) Implement the NSCopying protocol.
Have you ever used lazy loading in a project? Can you briefly talk about lazy loading?
Lazy loading is also called lazy loading. I’m writing its get method. If it is a lazy load, be careful to determine if it already exists, and if it does not, then instantiate it.
The advantage is that you don’t have to write all the code to create the object in the viewDidLoad method, so the code is more readable. Each control is responsible for its own instantiation in the getter method, and the code is strongly independent and loosely coupled to each other.
The difference and connection between process and thread.
A process is a static container that contains many threads, which are linear execution paths of a series of methods.
71, describe the memory partition.
Stack: store function parameter values, local variable values, etc. The release is automatically allocated by the compiler
Heap area: freed by programmer allocation. If the programmer does not release, the program may be reclaimed by the system at the end of the program
Global area: Global variables and static variables are stored together. Initialized globals and static variables are in one area and uninitialized globals and uninitialized static variables are in another area. The program is released by the system. The global area can be divided into uninitialized global area:. BBS segment and initialized global area: data segment.
Constant area: store constant string, after the end of the program by the system to release
Code area: Stores the binary code of the function body.
What’s the difference between a queue and a stack?
Stack: a linear table that is restricted to inserting or deleting only at the end of the table. The tail is the top of the stack, and the head is the bottom of the stack. It’s also called a last in first out linear table.
Queue: is a first in, first out linear table. It only allows inserts at one end of the table and deletes at the other.
73. There are several ways to program multiple threads in Objective-C
Pthread, NSThread, NSOperation, GCD.
74. What about the Objective-C Runtime mechanism? Let’s talk briefly about how an object calls a method.
How an object calls a method (Objective-C Runtime)
(1) Try to find the message in the Dispatch table of the object class. If found, skip to the corresponding function IMP to execute the implementation code
(2) If it is not found, Runtime will send +resolveInstanceMethod: or +resolveClassMethod: to try to resolve the message
(3) if the resolve method to return NO, Runtime is sent – forwardingTargetForSelector: allows you to forward this message to another object.
(4) if there is no new target object returns, the Runtime will send – methodSignatureForSelector: and – forwardInvocation: message. You can send – invokeWithTarget: messages to manually forward or send – doesNotRecognizeSelector: throw an exception.