52 effective ways to write high-quality iOS code "ios"

Source: Internet
Author: User

"52 Effective ways to write high-quality iOS and OS X code" notes, read it over again, feeling the impression is not particularly profound, write down notes.

The structure of this number is very clear, 5 chapters 52 suggestions, points to write the recommendations of efficient code, each point after the summary, here simple record these summary, easy to review.

Because of time, write 5 first, there is time to gradually add in.

Chapter One: Familiarity with oc1, understanding of OC Origins
    • OC adds object-oriented features to C language, and is a superset of it. OC uses a dynamically bound message structure, which means that the object type is not checked at run time. What code should be executed after receiving a message is determined by the run-time environment, not the compiler.
    • Understanding the core concepts of C language helps to write good OC programs. In particular, you should master the memory model and pointers.
2, minimize the introduction of other header files in the header file of the class
    • 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 and classes.
    • It is sometimes impossible to use forward declarations, such as declaring a class to follow a protocol. In this case, try to move to the "Class-continuation classification" of the Life of "This class follows an agreement". If not, put the protocol in a single header file and introduce it.
3, use more literal syntax, less of the equivalent syntax

(that is nsarray *array = @[@1,@2,@3];)

    • 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 removing the label operation
    • When you create an array or dictionary with literal syntax, if there is nil, an exception is thrown. Therefore, it is important to ensure that the value does not contain nil.
4, multi-use type constants, less # define preprocessing directives

(Disagree with this ...) )

    • 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 name associated with it.
5, using enumerations to identify status, options, status codes
    • An enumeration should be used to indicate the state of the state machine, the options passed to the method, and the status code equivalent, giving the values an understandable name.
    • If the option passed to a method is expressed as an enumeration type, and multiple options can be used simultaneously, the option value is defined as a power of 2 to be combined by a bitwise OR operation.
    • Use Ns_enum and Nsoptions macros to define the enumeration type and indicate its underlying data type. Doing so ensures that the enumeration is implemented using the underlying data type chosen by the developer, rather than using the type chosen by the compiler.
    • do not implement the default branch in a Swith 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.

52 effective ways to write high-quality iOS code "ios"

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.