Questions for iOS programmers
1. What is the automatic release pool and how does it work?
When you send an autorelease message to an object, Cocoa puts a reference of this object into the latest Auto Release pool. It is still a legitimate object, so other objects in the scope defined by the automatic release pool can send messages to it. When the program is executed at the end of the scope, the automatically released pool will be released, and all objects in the pool will be released.
2. Let's talk about the static keyword.
1) The static variable in the function body applies to this function body. Unlike the auto variable, the memory of this variable is allocated only once,
Therefore, the value remains the value of the previous time during the next call;
2) The static global variables in the module can be accessed by the functions used in the module, but cannot be accessed by other functions outside the module;
3) The static function in the module can only be called by other functions in the module. The scope of use of this function is limited in the Declaration.
In its module;
4) static member variables in the class belong to the entire class and only one copy of all objects of the class;
5) The static member function in the class belongs to the whole class. this function does not receive this pointer, so it can only be a static member variable of the category.
3. What is a single-piece instance?
Some classes in the Foundation and Application Kit frameworks only allow the creation of single-piece objects, that is, the unique instances of these classes in the current process. For example, the NSFileManager and NSWorkspace classes are used to instantiate single-piece Objects Based on processes. When you request instances of these classes, they will pass you a single instance reference. If the instance does not exist, the instance is allocated and initialized first. A single-piece object acts as a control center and is responsible for guiding or coordinating various services. If the class has only one instance, for example
NSWorkspace), you should generate a single-piece instance, instead of multiple instances. If you may have multiple instances one day in the future, you can
To use a single-piece instance mechanism, rather than a factory method or function.
4. What are the advantages and disadvantages of Objective-C?
Advantages of objc:
1) Cateogies
2) Posing
3) Dynamic Identification
4) indicator calculation
5) elastic message transmission
6) Not an overly complex C-derivative Language
7) Objective-C and C ++ Mixed Programming
Disadvantages:
1) namespaces are not supported.
2) Operator Overloading is not supported.
3) Multi-inheritance is not supported.
4) use the dynamic runtime type. All methods are called by functions, so many compilation optimization methods are not used. Such as inline functions), poor performance.
5. What is the difference between # import and # include? What does @ class represent?
@ Class is generally used when the header file needs to declare an instance variable of this class. In the m file, you still need to use # import
The advantage of # import over # include is that it does not cause Repeated inclusion.
6. What is the difference between a thread and a process?
Processes and threads are the basic units for running programs that the operating system understands. The system uses this basic unit to realize the system's concurrency for applications.
The main difference between processes and threads is that they are different operating system resource management methods. A process has an independent address space. After a process crashes, it will not affect other processes in the protection mode, but the thread is only a different execution path in the process. A thread has its own stack and local variables, but there is no separate address space between threads. If a thread dies, the whole process dies. Therefore, multi-process programs are more robust than multi-threaded programs, however, during process switching, resources are consumed and the efficiency is lower. But for some concurrent operations that require simultaneous and shared variables, you can only use threads, not processes.
7. Memory Management of Object-C?
1. when you create an object using the new, alloc, and copy methods, the reserved counter value of this object is 1. when you no longer use this object, you are responsible for sending a release or autorelease message to this object. in this way, the object will be destroyed at the end of its service life.
2. when you obtain an object using any other method, assume that the reserved counter value of this object is 1 and has been set to Auto Release, you do not need to perform any operations to ensure that the object is cleared. if you want to own this object for a period of time, you need to keep it and ensure that it is released when the operation is complete.
3. If you retain an object, You Need To (eventually) release it or automatically release it. You must ensure that the retain method and the release method are used equally.
8. What are the functions of attributes readwrite, readonly, assign, retain, copy, and nonatomic?
Readwrite is a readable and writable feature. When you need to generate the getter method and setter method
Readonly is a read-only feature that only generates the getter method and does not generate the setter method. You do not want the attribute to change outside the class.
Assign is a value assignment feature. The setter method assigns input parameters to instance variables. Only variables are set;
Retain indicates that the property is held. The setter method retains the input parameter first and then assigns a value. The retaincount of the input parameter will be + 1;
Copy indicates the value assignment feature. The setter method copies the input object. When a new variable is required.
Nonatomic non-atomic operations determine whether the setter getter generated by the compiler is an atomic operation. atomic indicates multi-thread security. nonatomic