ios interview preparation - objective-c articles

iOS interview preparation - ios
ios interview preparation - objective-c
ios interview preparation - network
IOS interview preparation - C++
iOS interview preparation - other

The difference between Swift and oc

Swift and Objective-C share a runtime environment, and Swift types can be bridged to Objective-C (hereinafter referred to as OC), and vice versa.

Advantages of Swift
Swift is easy to read, with simplified syntax and file structure.
Swift is easier to maintain, and the structure is clearer after the files are separated.
Swift is much safer, it is a type-safe language.
Swift has fewer codes and concise syntax, which can save a lot of redundant codes.
Swift is faster and has higher computing performance.

Shortcomings
of Swift are not used by as many people as OC (now it is not obvious)
because of the cost of language conversion, many companies continue to use OC

KVC

KVC is a mechanism enabled by the NSKeyValueCoding informal protocol adopted by objects to provide indirect access to their properties.
ios13 prohibits kvc from accessing private properties.

kvc setting value
Setting the value is to call the function: setValue:forKey, the internal process is as follows:
first, the corresponding setter method, setkey, _setKey, setIsKey will be called, if not,
check whether the accessInstanceVariablesDirectly method returns YES, if NO, go directly to the setValue:forUndefinedKey: method. If it is YES,
search member variables _key, _isKey, key, isKey in turn. If found, assign it a value, otherwise call setValue:forUndefinedKey:
setValue:forUndefinedKey: throws an exception by default

If you don't want to crash, you can make a classification of NSObject and rewrite setValue:forUndefinedKey: to make an empty implementation.

kvc value
The value is to call valueForKey:, the internal process is as follows:

●First getter method, getKey, key, isKey, _key will be called directly if found. If it is a value type such as BOOL or Int, it will be wrapped into an NSNumber object.

● Check the class method + (BOOL) accessInstanceVariablesDirectly, if it returns YES (the default behavior), then it will search the member variable names in the order of _key, _isKey, key, and isKey as the previous setting.
● If not found, directly call the valueForUndefinedKey: method of the object, which throws an exception by default.

KVO

How does iOS implement KVO for an object? (What is the essence of KVO?)
Answer. When an object uses KVO monitoring, the iOS system will modify the isa pointer of the object to point to a new subclass dynamically created by Runtime. The subclass has its own set method implementation , the set method implementation will call the willChangeValueForKey method, the original setter method implementation, and the didChangeValueForKey method in sequence, and the didChangeValueForKey method will call the observeValueForKeyPath:ofObject:change:context: listening method of the listener.

How to manually trigger KVO?
A. When the value of the monitored property is modified, KVO will be triggered automatically. If you want to manually trigger KVO, you need to call the willChangeValueForKey and didChangeValueForKey methods yourself to manually trigger KVO without changing the property value, and these two methods are indispensable, and the trigger code is still between them.

other

iOS: Understand classes and metaclasses in OC:
https://www.jianshu.com/p/51b4019a8df2

The difference between il, Nil, NULL, and NSNull
nil: a null pointer pointing to an object, and assigns a null value to an objective c id object.
Nil: a null pointer pointing to a class, indicating that the class is assigned a null value.
NULL: points to other types ( Such as: basic type, C type) null pointer, used to assign a null value to a non-object pointer.
NSNull: In a collection object, an object representing a null value.

The return in try is
return in try, and finally will be executed after return, so in this case, there will be problems processing the return value in finally, so don't put return in try.

Self-increment problem
In the following two methods, the value of count will not change. Principle: The value of count is temporarily stored as 0, and then count is incremented by 1 to become 1. Finally, the temporarily stored value is assigned to count, and the final value of count is 0.

int count = 0;
count = (count++);
count = count++;

The value below will be incremented by one

count = ++count;

Guess you like

Origin blog.csdn.net/htwhtw123/article/details/125483868