Fundamentals of IOS interview questions

Time:2021-10-22

Classification, extension, agent, notification, KVC, KVO, attribute keyword

1、 Category

*1. The role of classification?

Declare private methods and decompose large class files

*2. Characteristics of classification?

You can add classifications to system classes. During runtime, after the instance method list, protocol list and attribute list in category are added to the main class (the position of all methods in category in the method list is before the method with the same name of the main class), then the load method of all classes will be called recursively, all of which are executed before the main function.

*3. What can be added to the classification?

Instance method, class method, property (add getter and setter methods, no instance variable, add instance variable with associated object)

*4. If there are two categories A and B in the project, and there is a method with the same name in the two categories, which method will finally take effect?

Depending on the compilation order of the classification, the method with the same name of the last compiled classification will finally take effect, and the previous ones will be overwritten (this is not true coverage, because other methods still exist, but can not be accessed, because when dynamically adding class methods, the method list is traversed in reverse order, and the last compiled classified methods will be placed in front of the method list, and will be accessed first when accessing. Similarly, if a method with the same name as the method of the original class is declared, the method of the original class will also be overwritten Method).

5. What happens if two categories with the same name are declared?

An error will be reported, so the third-party classification generally has a naming prefix

6. Can a category add member variables?

No. Member variables can only be simulated and implemented through the association object (objc_setassociatedobject), but its essence is the association content. The association content of all objects is placed in the same global container hash table: associationshashmap, which is uniformly managed by the associationsmanager.

2、 Extension

1. The role of expansion?

Declare private attribute, declare private member variable

2. Extended features?

Compile time resolutions can only exist in the form of declarations. In most cases, they are placed in the. M of the host class, and extensions cannot be added to the system class

3、 Delegate

Agent is a design pattern. The principal declares the protocol, defines the interface to be implemented, and the agent implements the method according to the protocol

Break is generally used to avoid circular references
Fundamentals of IOS interview questions

4、 Notification (nsnotification)

The observer pattern is used to implement a mechanism for transmitting information across layers. The delivery method is one to many.
Fundamentals of IOS interview questions

5、 KVO (key value observing)

KVO is another implementation of the observer

Isa swizzling is used to implement KVO

Fundamentals of IOS interview questions

Changing the value KVO using the setter method will take effect, and changing the value KVO using KVC will also take effect, because KVC will call the setter method

- (void)setValue:(id)value {
    [self willChangeValueForKey:@"key"];
    [super setValue:value];
    [self didChangeValueForKey:@"key"];
}

Directly assigning a member variable will not trigger KVO because the setter method will not be called. Willchangevalueforkey and didchangevalueforkey need to be added

6、 KVC (key value coding)

KVC can directly access the properties of the object through the key or assign values to the properties of the object, so that the properties of the object can be accessed or modified dynamically at runtime

When calling the code of setValue: attribute value forkey: @ “name”, the underlying execution mechanism is as follows:

1. The program preferentially calls set < key >: attribute value method, and the code completes the setting through setter method. Note that < key > here refers to the member variable name, and the case of the initial letter shall comply with the naming rules of KVC, the same below

2. If the setname: method is not found, the KVC mechanism will check whether the + (bool) accessinstancevariablesdirectly method returns yes. By default, the method will return yes. If you rewrite the method to return no, KVC will execute the setValue: forundefinedkey: Method in this step, but ordinary developers will not do so. Therefore, KVC mechanism will search whether there is a member variable named < key > in the class. No matter whether the variable is defined at the class interface or the class implementation, and no matter what access modifier is used, KVC can assign a value to the member variable only when there is a variable named < key >.

3. If this class does not have a set < key >: method, neither does it_< Key > member variable, KVC mechanism will search_ Is < key >.

4. As above, if this class does not have a set < key >: method, neither does it_< Key > and_ Is < key > member variables. KVC mechanism will continue to search for member variables of < key > and is < key >. And assign values to them.

5. If none of the methods or member variables listed above exist, the system will execute the setValue: forundefinedkey: method of the object. The default is to throw an exception.

If you want to disable KVC, override the + (bool) accessinstancevariablesdirectly method to return No. in this way, if KVC does not find the set < key >: property name, it will directly use the setValue: forundefinedkey: method.

When calling the code of valueforkey: @ “name”, KVC’s search method for key is different from setValue: attribute value forkey: @ “name”, and its search method is as follows:

1. First, find the getter method in the order of get < key >, < key >, is < key >, and call it directly if found. If it is a bool or int equivalent type, it will be wrapped into an nsnumber object

2. If the above getter is not found, KVC will find methods in the format of countof < key >, objectin < key > atindex or < key > atindes. If the countof < key > method and one of the other two methods are found, a proxy collection that can respond to all nsarray methods (it is nskeyvaluearray and a subclass of nsarray) will be returned. If you call the methods of this proxy collection, or send the methods belonging to nsarray to this proxy collection, you will use countof < key >, Objectin < key > atindex or < key > atindexes are called in the form of a combination of these methods. There is also an optional get < key >: range: method. So you want to redefine some functions of KVC. You can add these methods. It should be noted that your method name should comply with KVC’s standard naming method, including method signature.

3. If the above method is not found, the methods in the format of countof < key >, enumerator of < key >, memberof < key > will be found at the same time. If these three methods are found, a proxy set that can respond to the method of nsset will be returned. As above, sending nsset messages to this proxy set will be called in the form of a combination of countof < key >, enumerator of < key >, memberof < key >.

4. If it is not found yet, check the class method + (bool) accessinstancevariablesdirectly. If yes (default behavior) is returned, it will press the same as the previous setting_< key>,_ Search member variable names in the order of is < key >, < key >, is < key >, which is not recommended here, because direct access to instance variables destroys encapsulation and makes the code more fragile. If the class method + (bool) accessinstancevariablesdirectly returns no, the valueforundefinedkey: method will be called directly, and an exception will be thrown by default

7、 Attribute keyword

1. Read / write permission: readonly, readwrite (default)

2. Atomicity: (atomic), nonatomic. Atomic is safe to read and write, but inefficient. It is not absolutely safe, such as operating arrays, adding or removing. In this case, mutex locks can be used to ensure thread safety

3. Reference count

retain/strong

Assign modifies the basic data type

Break does not change the reference count of the decorated object. After the object is released, the break pointer is automatically set to null

Copy is divided into deep copy and shallow copy

Shallow copy, copy of object pointer, target object pointer and source object pointer point to the same memory space, and the reference count increases

Deep copy, the copy of object content, opens up a new memory space

Fundamentals of IOS interview questions

Both copy and mutablecopy of mutable objects are deep copies

The copy of an immutable object is a shallow copy and mutable is a deep copy

The copy method returns immutable objects

@property (nonatomic, copy) NSMutableArray * array; This will crash because the copy object is immutable

Nsstring uses copy instead of strong to modify a name attribute. If a variable object is assigned, when the value of the variable object changes, the value of name will also change. This is not what we expect, because after using strong to modify name, it refers to the same memory address as the variable object. If copy is used, it is a deep copy, It will open up a new memory space, so when the variable object value changes, it will not affect the value of name.

————————————————

The article will continue to be updated next. You can also send me a private letter to get the latest information and interview related information in time. If you have any comments and suggestions, please leave me a message.

Please pay attention to the little partners who like IOS! Give me a compliment if you like! thank you! thank you! thank you!

Original link: IOS interview question foundation_ Never stop blog – CSDN blog_ IOS interview questions