Академический Документы
Профессиональный Документы
Культура Документы
Once you've figured out the basics of Objective-C and Cocoa, you're ready to start
writing some code. But the conventions of naming and formatting Objective-C code
are not the same as traditional C, C++ or even Java programs.
Apple has laid out a set of guidelines as to how code should be written for
maximum clarity and integration with the frameworks. In addition, there are some
undocumented conventions that Cocoa developers often use.
In essence, the human factor present in Apple's products for the end user can be
found in Objective-C and Cocoa themselves.
1 of 8
In addition to simple accessors, classes or objects can also return objects based
on various conditions or input. The format is simple:
[object/class thing+condition];
[object/class thing+input:input];
[object/class thing+identifer:input];
Examples
// class methods
newString = [NSString stringWithFormat:@"%f",1.5];
newArray = [NSArray arrayWithObject:newString];
If I wrote some of my own, this is what they might look like.
Sometimes, you want a variation on a value. In that case, the format is generally:
[object adjective+thing];
[object adjective+thing+condition];
[object adjective+thing+input:input];
Examples
2 of 8
Avoid Ambiguity
Ambiguous Messages
-sortInfo
-refreshTimer
-update
-fetchInfo:
On the surface, these methods may seem good. For example, they all have vowels.
But what they share is the potential for different interpretations because of the
language used.
Clear Messages
Note that the -updateMenuItem method doesn't put the NS prefix on menu item
3 of 8
Global C Functions
Prefix + Value ()
Prefix + Value + With/From/For + Input ()
Prefix + Action ()
Prefix + Action + Type ()
Just like classes, functions are prefixed with initials in order to prevent
namespace issues. Since C functions are free-standing by nature, the function name
needs to indicate data types they interact with, if any.
Functions from Cocoa
NSHomeDirectory()
NSHomeDirectoryForUser()
NSClassFromString()
NSBeginAlertSheet()
NSDrawGrayBezel()
In Core Services frameworks such as Core Foundation and Core Graphics, functions
are prefixed with the most-relevant opaque type, and the "get" prefix is used even
when there's no indirection involved:
Core Foundation
CGImageCreate()
CGImageGetHeight()
CGImageGetTypeID()
4 of 8
There are several types of symbols with global scope in addition to C functions.
For example:
Constants
Typedef'd structs
Typedef'd enums
Individual enum values
Objective-C Protocols
Protocols, constants, structs, enums and enum values are basically named according
to the same rules as classes or C functions. Capitalized with initials at the
front:
Structs
NSLiteralSearch
NSCaseInsensitiveSearch
// exception names (constants)
NSMallocException
NSInvalidArgumentException
NSTaskDidTerminateNotification
NSWindowWillMoveNotification
Also, note that notification names have a slightly different formula:
Dynamic Typing
Declaring a type for a varible is merely a promise which can be broken at runtime
if the code leaves room for such a thing. You can declare your variables as type
id, which is suitable for any Objective-C object.
Dynamically-Typed Variables
id hostName;
id ipAddress;
id keyedAccountNames;
id theObject;
A delegate or datasource
Object for a notification
Contents for a generic container
Objects involved in target/action
So no hard rules here, but a good basic practice is to specify the type if you
have reason to believe other types of objects wouldn't make sense in a given
situation.
6 of 8
When to Use Accessors
Why not just directly access instance variables within a class? It's a lot less
code, right?
The reason is flexibility. If your code gets to data via accessors, it's easy to
change how that data is manipulated later.
For example, it gives you a good entry point for registering the previous value
with NSUndoManager. Or when debugging, it might help to log when a particular
instance variable is fetched.
But there's an even better reason to create accessors: Key-Value Coding. KVC is a
protocol used throughout Cocoa, most notably in bindings. It will look for
accessors for your keys first, and only access data directly as a last resort. You
can shorten KVC's search and thus speedup data access by implementing accessors.
Also be aware that if you set instance variables directly, in the form of var =
value, Key-Value Observing will not notice the change and bound objects will not
get the new value.
7 of 8
Naming Parameters
Let's take a quick visit to method parameters. What's the standard here? The
guidelines are considerably more loose, but typically you prefix the input name
with "the", "an" or "new"
Also worth noting is the naming convention for a loop. Typically, the current
object in the loop is prefixed with "one" or "a/an". Some also simply refer to the
individual object as "item":
Classes that return one and only one instance of themselves (singletons) are named
and implemented as follows:
Singleton
@implementation CDCMailboxManager
+ (id) sharedMailboxManager
{
static CDCMailboxManager * shared = nil;
if ( !shared )
shared = [[self alloc] init];
return shared;
}