The following are the notes for learning objective-C 2.0. The original book I purchased is the original English version. Due to my limited level of English, there may be omissions in understanding.
Original book purchase address: Amazon
2. Objects, messaging, And the runtime
Item 6: Understand Properties
Some knowledge points:
(1) never call the self. Method in the init method or dealloc method;
(2) Use nonatomic in iOS, because the use of atomic will seriously affect the performance (it may be because I have incorrect understanding of English, which I do not quite understand ).
Remember:
(1) @ property Syntax provides methods for defining the attributes contained in data.
(2) provide correct semantic attributes for stored data.
(3) Make sure that the instance variables of the attribute are set and the declared semantics are implemented.
(4) Try to use nonatomic in iOS, because using atomic will greatly affect the performance.
Item 7: Access instance variables primarily directly when accessing them internally
Remember:
(1) when operating instance variables internally, you should use the self. Method to read instance variables directly while writing them;
(2) never call the self. Method in the init method or dealloc method;
(3) When a sample variable uses the lazy loading mode, you must use the self. Method to read the variable.
Item 8: understand object equality
Some knowledge points:
(1) usually = compares the pointer rather than the content pointed to by the pointer;
Remember:
(1) If you want to compare objects, you need to provide isequal: method and hash Method for objects.
(2) equal objects must contain the same hash, but objects with the same hash do not need to be equal;
(3) determine the necessary attributes when comparing objects, rather than comparing every attribute in a hasty manner.
(4) Writing hash methods provides a fast and reasonable way to compare different methods.
Item 9: Use the class cluster pattern to hide implementation detail
Remember:
(1) the class cluster mode can be used to hide detailed implementation details and provide a simple interface for calling.
(2) the class cluster mode is often used in the system framework.
(3) Be careful when writing an abstract class subclass of the class cluster mode. If you have any documents, read them carefully.
Item 10: Use Associated objects to attach M data to existing classes
Some knowledge points:
(1) objc_setassociatedobject; objc_getassociatedobject.
Remember:
(1) The correlated object provides a series of methods to connect two objects.
(2) memory management semantics of correlated objects can be defined as simulated ownership (mimic owning does not know how to translate) or not (nonowning is appropriate ?) Link.
(3) The associated object is considered only when other methods are not available, because the associated object may cause hard-to-find bugs.
Item 11: understand the role of objc_msgsend
Some knowledge points:
(1) objc_msgsend
Remember:
(1) A Message usually contains a receiver, a selector, and some parameters. A message is a method that calls an object at the same time.
(2) When a message is called, all messages are searched and executed by the dynamic message distribution system.
Item 12: Understand message forwarding
Some knowledge points:
(1) resolveinstancemethod-> forwardingtargetforselector-> forwardinvocation-> message not handled
Remember:
(1) The message forwarding process refers to the process in which an object is transmitted by flow when no proper selector is found.
(2) the dynamic method solution usually adds some methods dynamically at runtime and when it needs to be called.
(3) another object can be declared as a selector to solve a problem.
(4) Full Forwarding is performed only when all the pre-methods cannot be solved.
Item 13: Consider method swizzling to debug opaque Methods
Some knowledge points:
(1) class_getinstancemethod; method_exchangeimplementations
(2) You can add and exchange methods at runtime.
(3) It is only used for debugging and should not be abused.
Remember:
(1) The method implementation provided for a selector can be added and replaced at runtime.
(2) Cross-mixing is the process of exchanging methods of an object to another. Generally, adding an original implementation to add some functions.
(3) The implementation of the intervention method during running is usually only used for debugging. Do not abuse the intervention because it can be done in this way.
Item 14: understand what a class object is
Remember:
(1) the class hierarchy refers to the type pointer pointing to the model of all object instances of the class. (This translation has a big problem and I don't fully understand it)
(2) The introspection method is required when the object type cannot be determined during compilation.
(3) use the internal method ismemberofclass and iskindofclass to compare two classes instead of = to avoid message forwarding of objects.