Efffective OBJECTIVE-C Study Notes

Source: Internet
Author: User

Article One: Understanding the origins of the Objective-c language

Message structures rather than function calls. The difference is that, using the language of the message structure, the code that the runtime should execute is determined by the runtime environment, and the language used by the function call is determined by the compiler.

Objective-c adds an object-oriented feature to the C language and is a superset of it. Objective-c uses a dynamically bound message structure, which means that the object type is checked at run time. What code should be executed after accepting a message is determined by the run-time environment, not the compiler. Master C's memory model and pointers.

Article two: In the class of the header file as little as possible to introduce other header files

Do not introduce a header file unless it is necessary. In general, you should use forward declarations in the header file of a class to refer to other classes and introduce the header files of those classes into the implementation file. Doing so minimizes the coupling between classes.

It is sometimes impossible to use forward declarations, such as declaring a class to follow a protocol. In this case, try to move the statement "This class follows an agreement" to the "Class-continuation classification". If not, put the protocol alone in a header file and then introduce it.

Article Three: multi-use literal syntax, less use of the equivalent of the method

You should use literal syntax to create strings, numbers, arrays, dictionaries. This is more concise than the usual method of creating such objects.

The element that corresponds to the key in the array subscript or dictionary should be accessed by subscript operation.

When you create an array or dictionary with literal syntax, an exception is thrown if there is nil in the value. Therefore, it is important to ensure that the value does not contain nil.

Fourth: Multi-use type constants, less # define preprocessing directives

Do not define constants with preprocessing directives. The defined constants do not contain type information, and the compiler simply performs a find-and-replace operation on this basis before compiling. Even if a constant value is redefined, the compiler does not produce a warning message, which results in inconsistent constant values in the application.

Use the static const in the implementation file to define constants that are visible only within the compilation unit. Because such constants are not in the global symbol table, you do not need to prefix their names.

Use extern in the header file to declare the global variable and define its value in the relevant implementation file. This constant is to appear in the global symbol table, so its name should be separated, usually prefixed with the class names associated with it.

Fifth: Use enumerations to represent States, options, status codes

Enumerations should be used to represent state machine states, options passed to methods, and status code equivalents, giving these values an understandable name.

If the option passed to a method is expressed as an enumeration type, and multiple options can be used simultaneously, then each option value is defined as a power of 2 to be combined by a bitwise OR operation.

Use Ns_enum and Ns_options macros to define the enumeration type and indicate its underlying data type. Doing so ensures that the enumeration is implemented by the developer's chosen underlying data type, not the type chosen by the compiler.

Do not implement the default branch in a switch statement that handles enumeration types. In this case, after adding the new enumeration, the compiler will prompt the developer: The switch statement does not handle all enumerations.

Efffective OBJECTIVE-C Study Notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.