[iOS note] 52 effective ways to write high-quality iOS and OS X code: 1. Familiar with Objective-c

Source: Internet
Author: User

Brief introduction:

Recently the company project close, you can have time to read the book. "52 Effective ways to write high-quality iOS and OS X code" this book explains many of the skills and specifications of iOS development, highly recommended!

Here are the notes that you took when reading your book, as usual, first on the list:

Directory:

Chapter One: Familiarity with objective-c

Chapter II: Object, Message, Runtime

Chapter III: interface and API design

The fourth chapter: protocol and Category

Fifth chapter: Memory management

The sixth chapter: block and GCD

The seventh chapter: System framework

The first chapter is familiar with Objective-c 1th: Understanding the origins of Objective-c language
    • Origin
      • Smalltalk (originator of the message-based language)
    • Language type
      • Message structure (messaging structure), corresponding function call (functions calling)
    • Differences from the function call language
      • Message structure: The code that should be executed at run time is determined by the operating environment
      • Function call: The code that the runtime should execute is determined by the compiler
    • Points
      • OC has added object-oriented features to C language, which is the superset
      • OC uses a dynamically bound message structure, which means that the object type is checked at run time . What code should be executed after receiving a message, determined by the run-time environment, not the compiler
      • Understanding the core concepts of C language helps to write good OC programs, especially to master memory models and pointers
2nd: Minimize the introduction of other header files in the class header file
    • @class
      • The opportunity to introduce a header file is postponed as much as possible, only when there is a need, which reduces the number of header files that the class's users need to introduce (reducing compilation time)
    • Points
      • Do not introduce a header file unless it is necessary
      • In general, you should use a predecessor declaration in the header file of a class to refer to another class and introduce the header files of those classes into the implementation file, which minimizes coupling between classes
      • It is sometimes not possible to use a predecessor declaration, such as declaring a class to follow a protocol
3rd article: Literal Syntax
  • Literal syntax can make nsnumber, Nsarray, and Nsdictionary code more concise and clear
    • NSNumber
      • Traditional ways to create
        • NSNumber *somenumber = [NSNumber numberwithint:1];
      • literal syntax
        • NSNumber *somenumber = @1;
    • nsarray
      • traditional creation method
        • nsarray *animals = [Nsarray arraywithobjects:@ "Cat", @ "dog", nil];
      • literal syntax
        • "@" "];
    • nsdictionary
      • traditional creation method
      • literal syntax
        • nsdictiona Ry *persondata = @{ @ "Matt": @ "FirstName" };
    • Nsmutablearray and Nsmutabledictionary
      • Traditional off-label modified element value
        • [Mutablearray replaceobjectatindex:1 withobject:@ "Dog"];
        • [Mutabledictionary setobject:@ "Galloway" Forkey: "LastName"];
      • literal syntax
        • MUTABLEARRAY[1] = @ "Dog";
        • mutabledictionary[@ "LastName"] = @ "Galloway";
    • Limitations
      • In addition to strings, the objects created must belong to the foundation framework
      • If you have customized subclasses of these classes, you cannot create their objects with literal syntax
      • To create an instance of a custom subclass, you need to use nonliteral syntax
      • Objects created with literal synatax are immutable, and if a variable version is required, copy
    • Points
      • You should use literal syntax to create NSString, NSNumber, Nsarray, and Nsdictionary, which is more concise than the usual method of creating such objects.
      • The element corresponding 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 synatax syntax, an exception is thrown if there is nil in the value. Therefore, it is important to ensure that the value does not contain NLI
4th Multi-use type variable, less # define preprocessing directives
    • Points
      • 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 causes the constant values in the program to be inconsistent
      • Use static const in an implementation file to define constants that are visible only within the compilation unit (TRANSLATION-UNIT-SPECIFIC constant)
        • 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 the name should be separated, usually prefixed with the class name associated with it.
5th use enumerations to represent States, options, status codes
    • Points
      • An enumeration should be used to indicate the state of the state machine, the options passed to the method, and the status code equivalents, 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, define each option value as a power of 2 to combine it by pressing or manipulating it.
      • Use Ns_enum and Ns_options macros to define enum types and specify their underlying data types
        • Doing so ensures that the enumeration is represented by the underlying data type chosen by the developer, not by the type chosen by the compiler
      • Do not pre-default branches in a switch 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

[iOS note] 52 effective ways to write high-quality iOS and OS X code: 1. Familiar with Objective-c

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.