博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nullnullios 变量命名规范
阅读量:7095 次
发布时间:2019-06-28

本文共 6624 字,大约阅读时间需要 22 分钟。

查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!

    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 ). 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 ). 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 ).

    

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 .

    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

文章结束给大家分享下程序员的一些笑话语录: 某程序员对书法十分感兴趣,退休后决定在这方面有所建树。花重金购买了上等的文房四宝。一日突生雅兴,一番磨墨拟纸,并点上了上好的檀香,颇有王羲之风 范,又具颜真卿气势,定神片刻,泼墨挥毫,郑重地写下一行字:hello world.

转载地址:http://ddxql.baihongyu.com/

你可能感兴趣的文章
微信公众号官方文档“入门指引”——我的实际测试记录
查看>>
其他网站几篇日记链接
查看>>
OSPF概述
查看>>
非归档下oracle的备份和恢复
查看>>
zTree 初始化方法使用
查看>>
staruml 提示 max connection attempts reached
查看>>
delphi 辨析 Field、FieldDef、Fields、FieldDefs、FieldList、FieldDefList
查看>>
mysql 查看数据库中所有表的记录数
查看>>
浅谈 Banner 设计与制作
查看>>
字符串中第一个只出现一次的字符
查看>>
如何在多实例基础上再添加一个mysql的实例
查看>>
linux stat命令参数及用法详解
查看>>
wlinux touch命令参数及用法详解---linux修改文件的时间
查看>>
perl XML::RSS创建RSS文件并解析
查看>>
linux配置dhcp服务器时authoritative参数的作用
查看>>
常用图像/编程 词汇
查看>>
学习过程中发现的一些注意事项2
查看>>
F5 cookie值与IP地址(一):将F5插入的cookie转换为IP地址
查看>>
利用vps+frp实现访问公司内网windows远程桌面
查看>>
Hearbeat高可用
查看>>