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
1. accustoming yourself to objective-C
Item 1: familiarize yourself with objective-C's roots
Some knowledge points:
(1) A pointer pointing to a heap memory is stored in the stack memory (note that the pointer is in the stack and the data indicated is in the heap memory)
(2) Objective-C uses the message structure and dynamic binding mode, and determines the object type at runtime rather than at compilation.
Remember:
(1) Objective-C is the superset of C and adds object-oriented attributes. Objective-C uses a dynamically bound message structure, which indicates that the object type is determined at runtime. The runtime is relative to the Compilation Time, And the code to be executed is confirmed by message passing.
(2) Understanding the core concepts of C helps to compile objective-C Programs. In particular, you need to understand the memory management and pointers of the C language.
Item 2: Minimize importing headers in Headers
Some knowledge points:
(1) using the pre-declaration (@ Class) in. h to use # import in. m can reduce unnecessary Compilation Time.
(2) The delegate method can be separately placed in a. h file to avoid # Too many unnecessary parts of Import
(3) If the pre-declaration (@ Class) is not enough, consider using category.
Remember:
(1) try to use the pre-declaration in the. h file and introduce the header file in the. M file to avoid circular reference.
(2) When the pre-declaration is useless, consider using category or separately introducing header files containing only protocol.
Item 3: prefer literal syntax over the equivalent methods
Some knowledge points:
(1) enhanced readability and simplified code while avoiding nil insertion errors
Remember:
(1) try to use the nominal value syntax to create strings, numbers, arrays, and dictionaries. This is clearer and more informative than using conventional object creation methods.
(2) Use keywords and indexes in the dictionary and array for subscript data acquisition.
(3) Inserting nil when using the nominal value syntax throws an exception. Therefore, ensure that the inserted data cannot be nil at all times.
Item 4: prefer typed constants to Preprocessor # define
Some knowledge points:
(1) use static const nstimeinterval kanimationduration = 0.3 instead # define animation_duration 0.3
(2) Static const can be prefixed by class names without the need to use them globally. You can use K as the prefix. If you need to use them globally, you must include the class name as the prefix, do not add a K prefix
(3) for global use requirements, you can. define extern nsstring * const eocstringconstant in. define nsstring * const eocstringconstant = @ "value" in M. This is often used in icationicationname definition.
Remember:
(1) Avoid using preprocessing commands whenever possible. The pre-processing command does not contain any type information. It only performs the replacement operation before compilation. When they are repeatedly defined, no warning is reported, which is easy to generate inconsistent values throughout the program.
(2) The static const type constants defined in the implementation file do not need to be referenced globally, so their names do not need to contain namespaces.
(3) The Global referenced constant defined in the header file must be associated with the part defined in the implementation file. Because they need to be referenced globally, their names need to contain namespaces, which are usually called named prefixes using their class names.
Item 5: Use enumerations for States, options, and status codes
Some knowledge points:
(1) Try to use Enum to define the status
(2) Use ns_enum and ns_options with clear types to prevent the editor from selecting the type.
(3) do not use default for the switch function of Enum, so that the compiler will automatically give a warning when changing Enum to avoid errors.
Remember:
(1) Use enumeration definitions to provide a more readable name value for the state machine, optional, and error code status.
(2) If you can define an enumeration for a method, use the ns_options method as much as possible, so you can use the or method to select multiple.