Categories, extensions, proxies, notifications, KVC, KVO, property keywords

1. Categories

*1. What is the function of classification?

Declare private methods to decompose large class files

*2. What are the characteristics of classification?

You can add taxonomies for system classes. At runtime, after the instance method list, protocol list, and property list in the Category are added to the main class (all methods in the Category are placed in the method list before the method of the same name in the main class), the load methods of all classes are recursively called, all before the main function.

*3. What can be added to the category?

Instance methods, class methods, properties (add getter and setter methods, no instance variables, add instance variables with associated objects)

*4. If there are two categories A and B in the project, and one of the two categories has the same name, which method will work?

Order depends on the classification of the compilation, the compilation of the classification method of the same name in the end, and will be overwritten before (there is not really a cover, because the rest of the way still exist, but can’t access, because in a dynamic way to add class is traversal methods list in reverse chronological order, and finally the classification of the compilation method will be put in front of the list of methods, If you declare a method with the same name as the method of the original class, you will override the method of the original class.

5. What if you declare two categories of the same name?

It will report an error, so the third party categories, usually have a naming prefix

6. Can we add member variables to categories?

Can’t. Member variables can only be simulated by associating objects (objc_setassociateObject), but their essence is associating content, The associated contents of all objects are placed in the same global container hash table: AssociationShashMap, which is managed by AssociationsManager.

II. Extension

1. The role of extension?

Declare private properties, and declare private member variables

2. Features of the extension?

Compile-time resolution, can only exist as a declaration, most of the time in the host class in the.m, cannot add extensions to the system class

III. Delegate

Proxy is a design pattern in which a client declares a protocol, defines the interfaces that need to be implemented, and the agent implements methods according to the protocol

It is common to use weak to avoid circular references

IV. NSNotification

Use the Observer pattern to implement a mechanism for passing information across layers. Delivery is one-to-many.

KVO (key-value-observing)

KVO is another implementation of an observer

ISA -swizzling is used to implement KVO

KVO will take effect if you change the value using a setter method, and KVC will take effect if you change the value using KVO, because KVC will call the setter method

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

Assigning a member variable directly does not trigger KVO because the setter method is not called and you need to add WillChangEvalueForKey and DidChangEvalueForKey

6. KVC (key-value-coding)

KVC can directly access objects’ properties through keys, or assign values to objects’ properties, which can be dynamically accessed or modified at run time

When the code for setValue: attribute value forKey: @ “name” is called, the underlying execution mechanism is as follows:

1, The program calls the set<Key>: attribute value method first, the code completes the setting through the setter method. Notice that the < key > refers to the member variable name, the first letter case to conform to the KVC naming rules, the same below

If no setName is found: Methods, mechanism of KVC will check + (BOOL) have accessInstanceVariablesDirectly method returns YES, by default, this method returns YES if you rewrite the method make its return NO, so in this step KVC will perform setValue: ForUndefinedKey: Method, which is not what most developers do. So the KVC mechanism will search the class for a member variable named <key>. Regardless of whether the variable is defined at the interface of the class or at the implementation of the class, and no matter what access modifier is used, KVC can assign a value to that member variable only if it is named <key>.

3. If the class has neither set<key> : method nor _<key> member variable, the KVC mechanism will search for _is< key> member variable.

4. As above, if the class has neither set<Key> : methods nor _< Key> and _is<Key> member variables, then the KVC mechanism will continue to search for <Key> and is<Key> member variables. I’m going to assign values to them.

5. If none of the methods or member variables listed above exist, the setValue: forUndefinedKey: method on the object will be executed, throwing an exception by default.

If you want to disable KVC, rewrite + (BOOL) accessInstanceVariablesDirectly method to return NO, so if the KVC had not found the set < Key > : the property name, can directly use setValue: forUndefinedKey: method.

When valueForKey: @ “name” is called, KVC searches forKey in a way that is different from setValue: attribute value forKey: @ “name”. It searches as follows:

Get <Key>,< Key>, is<Key>, get<Key>, is<Key> If it’s a BOOL or an Int equivalent, it wraps it as an NSNumber object

If the getter above is not found, KVC will look for methods in the format countOf<Key>,objectIn<Key>AtIndex, or <Key>AtIndexes. If countOf<Key> and one of the other two methods is found, then it returns a set of proxies that can respond to all methods of NSArray (it’s NKEYValueArray, a subclass of NSArray), calls methods of the proxies set, Or if you send a method belonging to an NSArray to the proxy collection, it will be called as a combination of countOf<Key>,objectIn<Key>AtIndex, or <Key>AtIndexes. There is also an optional get<Key>:range: method. So if you want to redefine some of the functionality of KVC, you can add these methods, and it’s important that your method names conform to the standard naming methods of KVC, including method signatures.

3. If the above methods are not found, then countOf<Key>, enumeratorOf<Key>,memberOf<Key> will be found at the same time. If all three methods are found, then a proxy set is returned that can respond to the methods in the NSSET. As above, send an NSSET message to the proxy set, which will be called in the form of a combination of countOf<Key>, enumeratorOf<Key>, and memberOf<Key>.

4, if you haven’t found, check class method + (BOOL) accessInstanceVariablesDirectly, if return YES (default behavior), then and previously set value, The member variable name is searched in the order _<key>,_is< key>, <key>,is< key>. This is not recommended because direct access to instance variables breaks encapsulation and makes the code more fragile. If you rewrite the class method + (BOOL) accessInstanceVariablesDirectly return NO, then can call directly valueForUndefinedKey: method, default is an exception

Seven, attribute keywords

1, read and write permissions: readonly, readwrite (default)

(1) atomic (2) atomic (3) nonatimic Atomic reads and writes are safe, but they are inefficient. They are not absolutely safe, such as manipulating arrays, adding or removing them. In this case, mutexes can be used to ensure thread safety

3. Reference counting


Assign modifies the base data type

The weak pointer does not change the reference count of the modifier object. When the object is freed, the weak 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 to the same memory space, reference count increased

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

A mutable object’s copy and mutableCopy are deep copies

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

Copy returns immutable objects

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

NSString copy decorates instead of strong, it decorates a name property with strong, if we assign to a mutable object, when the value of the mutable object changes, the value of the name will change, which is not what we would expect, because the name with strong decorates, If the name of the mutable object is changed, it will not affect the name of the mutable object.

— — — — — — — —

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

I beg the attention of those who like iOS! If you like it, give it a like! Thank you very much! Thank you very much! Thank you very much!

IOS Interview Basics – Never Stop Blog -CSDN Blog – iOS Interview Questions