Runtime/kvo and other face test
1.KVO Internal Implementation Principles
Answer: 1>KVO is implemented based on the runtime mechanism
2> when an object of a class is observed for the first time, a derived class of the class is dynamically created at runtime, overriding the setter method of any observed attribute in the base class in this derived class.
Derived classes implement a true notification mechanism in overridden setter methods (person->nskvonotifying person)
The Person class object P sets the self controller and sets the listening property, and the Observevalueforkeypath method is called, and when the property of the person class is changed, a subclass is dynamically generated that inherits from the Man class (Nskvonotifying_ person), override the Setage method, call the parent class Setage method internally, notify the listener
[Listener observevalueforkeypath:@ "age" ofobject:self change:@{} Context: (void *) context];
The original Age property does not change when the P object Isa points to person, when the Age property changes P object Isa points to Nskvonotifying_person (Setage re-internally passes the variable to the parent class and notifies the listener that the Age property changes)
2. Is it possible to put more time-consuming operations in Nsnotificationcenter
Answer:1> If you send notifications asynchronously, you can perform more time-consuming operations
2> If you send notifications on the main thread, you cannot perform more time-consuming operations
Figure--2
Figure---1
What is the difference between a 3.Foundation object and a core foundation object
Answer: 1>foundation object is OC, Core Foundation is C language object
2>foundation Objects and core Foundation objects can be converted to and from each other, and conversions between data types
Arc:__bridge_retained/__bridge_transfer
Non-Arc:__bridge
4. Swap A and B in two ways without intermediate variables
Answer: a= A + B
b= A-B
A = A-B
Or
A= a^b
b = A ^ b
A = a^b;
5. Simple description of the understanding of the design of a single case pattern?
Answer: Save memory resources, an application is an object
6. What is dynamic, for example
1> operations that are performed only when the program is running, such as the internal implementation principle of KVO, the property value of a class changes as the program runs listening property values
7.runtime implementation mechanism is what, how to use, generally used, you can also remember the relevant header file you use or some method name?
Answer: runtime mechanism, the runtime library contains the class/member variables/methods related to the API, such as to get all the member variables in the class, for the class dynamically add member variables, dynamically change the method implementation of the class, add new methods for the class dynamic, etc., need to import <objc/message.h ><objc/runtime.h>
1>runtime, run-time mechanism, which is a set of C language libraries
What is runtime?
2> in fact, all of the OC code we wrote, eventually turned into the runtime library of things, such as the class into the runtime library inside the structure of the data type, the method into the runtime library in the C language function, peacetime adjustment method is turned into a objc_ Msgsend function (so that OC has a message sending mechanism)
3> Therefore, it can be said that runtime is the lower level of OC implementation, is OC's behind the actors
What's the use of runtime?
1> can dynamically produce a class, a member variable, a method
2> can dynamically modify a class, a member variable, a method
3> can dynamically delete a class, a member variable, a method
Common functions, header files
#import <objc/runtime.h>: Member variables, classes, methods
Class_copyivarlist: Get all member variables inside a class
Class_copymethodlist: Get all the methods inside a class
Class_getinstancemethod: Get a Specific instance method (object method, minus sign start)
Class_getclassmethod: Get a specific class method (plus sign)
Method_exchangeimplementations: Implementation of 2 methods of exchange
#import <objc/message.h> Messaging Mechanisms
Objc_msgsend (...);
Plot: Usually use the OC code to set the value of the variable is turned into as shown
Diagram: Runtime mechanism can see very low-level implementations, even member variables are private.
Diagram: Runtime implements traversal of all member variables
Diagram: Implementation of the UIImage class Exchange 2 methods using the runtime mechanism (Imagewithname and imagenamed)
However, if you call imagenamed within the Imagewithname method, it will cause a dead loop.
The benefits of exchanging 2 methods: a large project in the future, one of the methods used too much, want to move some hands and feet and operation, it can be easily implemented.
Diagram: Modifying the AddObject method is not empty to add to an array
Plot: Modify the Objectatindex method and see if it is out of bounds, such as a cross-border return null,runtime can also prevent a program from crashing
Note: self.book[4] Executes the actual call to the Objectatindex method.
iOS face question 04-runtime