Naming Properties and Data Types, "translation" of the naming convention of Apple's official website

Source: Internet
Author: User
Tags naming convention processing instruction



Apple official original: Naming Properties and Data Types



Preface: Practise English and study purely. Please understand and correct the translation errors and non-fluent areas. O (∩_∩) o


Naming of properties and data types


This section describes the common naming conventions for property definitions, variables, constants, notifications, and exceptions.






L Define properties and variables



A property definition affects the definition of the access method for that property. So the naming conventions for attributes are broadly consistent with the naming conventions of accessors (Getter,setter). If an attribute is expressed as a noun or verb, the definition is as follows:


@property (...) type Nounorverb


For example:


@property (Strong) NSString *title; @property (assign) BOOL Showsalpha;


If a property name is an adjective, such as editable (editable), the is prefix is usually omitted, but you need to add a customary name for the get accessor, that is, iseditable, for example:


@property (assign,getter=iseditable) BOOL editable;


In many cases, when you declare a property and synthesize a corresponding instance variable at the same time. Make sure that the instance variable accurately describes the meaning of the attribute. In general, you'd better not directly access variables, you need to use accessors to access the variable. (You can access the variables directly within the Init and Dealloc methods). Typically, an underscore is added to the instance variable to indicate the variable. For example:


 
@implementation MyClass{
        BOOL _showsTitle;
}


If you compose a variable by defining a property, assign it a variable name in @synthesize


 
@implementation MyClass @synthesize showsTitle =_showsTitle;


When you add a variable to a class, you need to keep in mind the following points:



1. Avoid displaying the definition of a common variable, developers should care about an interface instead of focusing on how an interface stores data details. You can avoid defining variables directly by declaring properties and synthesizing the corresponding variables.



2. If you need to define a variable (in the interface), preferably with @private or @protected, if you want your class to be inherited, the inheriting class needs to access the variable directly to get the data, then use the @protected instruction



3. If a variable is accessible. Make sure you write the variable access method. (if possible, define properties, @property will automatically generate getter setter methods)



L Constants



A constant's naming convention usually depends on how the constant is created.



Constants for enumeration types



An enumeration is used to implement a set of constants of the associated integer type.



Enumeration constants are defined in a way that follows the functions naming convention. Let's look at an example in NSMatrix.h:


 
typedef enum _NSMatrixMode {
    NSRadioModeMatrix = 0,
    NSHighlightModeMatrix = 1,
    NSListModeMatrix = 2,
    NSTrackModeMatrix = 3 } NSMatrixMode;


Note that typedef can be omitted, and you can create an enumeration that is not named, for example:


 
enum {
    NSBorderlessWindowMask = 0,
    NSTitledWindowMask = 1 << 0,
    NSClosableWindowMask = 1 << 1,
    NSMiniaturizableWindowMask = 1 << 2,
    NSResizableWindowMask = 1 << 3 };


Create constants with const



You can create a constant of type float with Const. You can also create a constant of type integer if it is not related to other constants. Otherwise, use the enumeration bar. Constants are defined by const in the following way:


Const float Nslightgray;


The naming conventions are the same as enumeration naming conventions.



Other types of constants



Generally, do not create constants with the # define preprocessing commands. For constants of type integer, an enumeration is used for constants of type float, as defined by Const.



Use the preprocessing directives of the uppercase symbols to determine if a piece of code is to be executed, for example:


#ifdef DEBUG


Note There are two underscores before and after a system-defined macro processing instruction, for example:


__mach__


A string-type constant that defines a key for a notification or dictionary, and you can ensure that the compiler can recognize the exact value. (It performs a spell check) The COCOA framework provides many examples:


Appkit_extern NSString *nsprintcopies;


The constants are actually assigned in the. m file. (Note that the Appkit_extern macro is the same as the EXTERN macro of Object-c)



L Notifications and Exceptions



Notifications and exceptions are named in a similar way, but they all have their own naming patterns


Notice


The naming conventions for notifications are as follows:


class] + [did | would] + [Uniquepartofname] + Notification


For example:


 
NSApplicationDidBecomeActiveNotification
NSWindowDidMiniaturizeNotification
NSTextViewDidChangeSelectionNotification
NSColorPanelColorDidChangeNotification


The naming conventions for exceptions are as follows:


[Prefix] + [Uniquepartofname] + Exception


For example:


 
NSColorListIOException
NSColorListNotEditableException
NSDraggingException
NSFontUnavailableException
NSIllegalSelectorException


Pay attention to the Uniquepart section to use the Hump naming method Oh



Remark: Each word is indicative



Conventions specifications, conventions



Property Properties



Instance instances



Variable variable



Constants Constants



Notification Notice



Exception exception



Accessor accessors



Omits ignore



Explicitly clear



Vary different



Naming Properties and Data Types, "translation" of the naming convention of Apple's official website


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.