iOS Reference Library Apple Developer
Search

Naming Instance Variables and Data Types

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

Instance Variables

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

If an instance variable is to be an accessible attribute of objects of the class, make sure you write accessor methods for it.

Constants

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

Enumerated constants

Constants created with const

Other types of constants

Exceptions and Notifications

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

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 has traditionally not used them to handle regular, expected error conditions. For these cases, use returned values such as nil, NULL, NO, or error codes. It typically reserves exceptions for programming errors such an array index being out of bounds.

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

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



Last updated: 2010-05-05

Did this document help you? Yes It's good, but... Not helpful...