`
ydbc
  • 浏览: 720600 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

ios 变量命名规范

 
阅读更多

This section describes the naming conventions for declared properties, instance variables, constants, notifications, and exceptions.

Declared Properties and Instance Variables

A declared property effectively declares accessor methods for a property, and so conventions for naming a declared property are broadly the same as those for naming accessor methods (see “Accessor Methods”). If the property is expressed as a noun or a verb, the format is:

@property (…) type nounOrVerb;

For example:

@property (strong) NSString *title;
@property (assign) BOOL showsAlpha;

If the name of a declared property is expressed as an adjective, however, the property name omits the “is” prefix but specifies the conventional name for the get accessor, for example:

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

In many cases, when you use a declared property you also synthesize a corresponding instance variable.

Make sure the name of the instance variable concisely describes the attribute stored. Usually, you should not access instance variables directly; instead you should use accessor methods (you do access instance variables directly in init and dealloc methods). To help to signal this, prefix instance variable names with an underscore (_), for example:

@implementation MyClass {
    BOOL _showsTitle;
}

If you synthesize the instance variable using a declared property, specify the name of the instance variable in the @synthesize statement.

@implementation MyClass
@synthesize showsTitle=_showsTitle;

There are a few considerations to keep in mind when adding instance variables to a class:

  • Avoid explicitly declaring public instance variables.

    Developers should concern themselves with an object’s interface, not with the details of how it stores its data. You can avoid declaring instance variables explicitly by using declared properties and synthesizing the corresponding instance variable.

  • If you need to declare an instance variable, explicitly declare it with either @private or @protected.

    If you expect that your class will be subclassed, and that these subclasses will require direct access to the data, use the @protected directive.

  • If an instance variable is to be an accessible attribute of instances of the class, make sure you write accessor methods for it (when possible, use declared properties).

Constants

The rules for constants vary according to how the constant is created.

Enumerated constants

  • Use enumerations for groups of related constants that have integer values.

  • Enumerated constants and the typedef under which they are grouped follow the naming conventions for functions (see “Naming Functions”). The following example comes from NSMatrix.h :

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

    Note that the typedef tag (_NSMatrixMode in the above example) is unnecessary.

  • You can create unnamed enumerations for things like bit masks, for example:

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

Constants created with const

  • Use const to create constants for floating point values. You can use const to create an integer constant if the constant is unrelated to other constants; otherwise, use enumeration.

  • The format for const constants is exemplified by the following declaration:

    const float NSLightGray;

    As with enumerated constants, the naming conventions are the same as for functions (see “Naming Functions”).

Other types of constants

  • In general, don’t use the #define preprocessor command to create constants. For integer constants, use enumerations, and for floating point constants use the const qualifier, as described above.

  • Use uppercase letters for symbols that the preprocessor evaluates in determining whether a block of code will be processed. For example:

    #ifdef DEBUG
  • Note that macros defined by the compiler have leading and trailing double underscore characters. For example:

    __MACH__
  • Define constants for strings used for such purposes as notification names and dictionary keys. By using string constants, you are ensuring that the compiler verifies the proper value is specified (that is, it performs spell checking). The Cocoa frameworks provide many examples of string constants, such as:

    APPKIT_EXTERN NSString *NSPrintCopies;

    The actual NSString value is assigned to the constant in an implementation file. (Note that the APPKIT_EXTERN macro evaluates to extern for Objective-C.)

Notifications and Exceptions

The names for notifications and exceptions follow similar rules. But both have their own recommended usage patterns.

Notifications

If a class has a delegate, most of its notifications will probably be received by the delegate through a defined delegate method. The names of these notifications should reflect the corresponding delegate method. For example, a delegate of the global NSApplication object is automatically registered to receive an applicationDidBecomeActive: message whenever the application posts an NSApplicationDidBecomeActiveNotification.

Notifications are identified by global NSString objects whose names are composed in this way:

[Name of associated class] + [Did | Will] + [UniquePartOfName] + Notification

For example:

NSApplicationDidBecomeActiveNotification
NSWindowDidMiniaturizeNotification
NSTextViewDidChangeSelectionNotification
NSColorPanelColorDidChangeNotification

Exceptions

Although you are free to use exceptions (that is, the mechanisms offered by the NSException class and related functions) for any purpose you choose, Cocoa reserves exceptions for programming errors such an array index being out of bounds. Cocoa does not use exceptions to handle regular, expected error conditions. For these cases, use returned values such as nil, NULL, NO, or error codes. For more details, see Error Handling Programming Guide.

Exceptions are identified by global NSString objects whose names are composed in this way:

[Prefix] + [UniquePartOfName] + Exception

The unique part of the name should run constituent words together and capitalize the first letter of each word. Here are some examples:

NSColorListIOException
NSColorListNotEditableException
NSDraggingException
NSFontUnavailableException
NSIllegalSelectorException
分享到:
评论

相关推荐

    iOS软件代码规范

    iOS软件代码规范目录 前 言 4 1. 指导原则 5 2. 布局 5 2.1. 文件布局 6 2.2. 基本格式 8 2.3. 对齐 9 2.4. 空行空格 11 2.5. 断行 13 3. 注释 14 4. 命名规则 17 4.1. 基本规则 17 4.2. 资源命名 19 5. 变量,常量...

    IOS开发编码及命名规范.doc

    IOS开发编码及命名规范 目录 1、目的 3 2、适用范围 3 3、编码规范 3 3.1、文件 3 3.2、注释 3 3.3、编码排版格式 4 3.4、命名规范 6 3.4.1、保留字 6 3.4.2、方法 7 3.4.3、变量 7 3.4.4、常量 8 3.4.5、类 8 3.5...

    iOS-Coding-Style:iOS编码规范

    即使他们很长1.4 避免产生歧义的命名变量命名-2.1 变量名首字母小写,后续首字母大写,"*"和变量名之间不空格@property (atomatic, retain) NSString * myDevice (不合适,"*"和变量名之间不空格)@property (atomatic...

    纽约时报 移动团队 Objective-C 规范指南

    这份规范指南概括了纽约时报 iOS 团队的代码约定。 介绍 关于这个编程语言的所有规范,如果这里没有写到,那就在苹果的文档里: Objective-C 编程语言 Cocoa 基本原理指南 Cocoa 编码指南 iOS 应用编程指南 目录 ...

    资料库

    2命名规范 根据Cocoa编码规范里的描述,命名应该遵循以下基本原则:清晰,一致,没有自我参照,即清晰性,一致性,不要自我指涉。 2.1通用命名规范 通用命名规则适用于类名,变量,常量,属性,参数,方法,函数等。...

    仿Zaker首页的拖动排序类(修正了部分bug)

    修正了iOS5 下 删除按钮不响应、编辑后不能滑动的bug 规范了变量命名、修正了部分不好理解的方法名

    Orion3:适用于Code的Sketch插件(iOSSwift,Android,CSS)

    1.在sketch文件中,选中一个对象(例如文本),按规则重新命名:Type@Name, 其中Type为对象实现类型,Name为对象名(对于iOS/Swift, 为代码中变量名,对于Android,为对象id,对于CSS,则为标签class(暂时没有用到...

    LGiOSQuestions:总结iOS常见面试题,以及BAT大厂面试分享

    2020-逻辑教育iOS面试题集合总结iOS常见面试题,以及BAT大厂面试...注意,这里的是指成员变量名,首字母大小写要符合KVC的命名规则,下同2,如果没有找到setName:方法,KVC机制会检查+ (BOOL)accessInstanceVariable

    NYTimes-Objective-C-Style-Guide-ZH:纽约时报移动团队 Objective-C 项目风格指南中文版

    这份规范指南概括了纽约时报 iOS 团队的代码约定。 介绍 关于这个编程语言的所有规范,如果这里没有写到,那就在苹果的文档里: 目录 错误处理 方法 变量 命名 注释 Init 和 Dealloc 字面量 CGRect 函数 常量 枚举...

    现代C++程序设计

    2.5.1 C++的命名规则 2.5.2 在哪里声明变量 2.6 C++中的运算符 2.6.1 计算路程的程序 2.6.2 从键盘输入程序所需数据 2.6.3 赋值运算符 2.6.4 运算符的优先级 2.6.5 数据类型及其存储的值 2.6.6 算术运算符 2.6.7 ...

    【全新正版】现代C++程序设计(原书第2版)

    2.5.1 C++的命名规则 2.5.2 在哪里声明变量 2.6 C++中的运算符 2.6.1 计算路程的程序 2.6.2 从键盘输入程序所需数据 2.6.3 赋值运算符 2.6.4 运算符的优先级 2.6.5 数据类型及其存储的值 2.6.6 算术运算符 2.6.7 ...

Global site tag (gtag.js) - Google Analytics