- (id)initWithFrame:(CGRect)frame viewController:(LevelViewController *)aController;
What's the small a for? When I see a small k, I'm assuming it's for constant.
- (id)initWithFrame:(CGRect)frame viewController:(LevelViewController *)aController;
What's the small a for? When I see a small k, I'm assuming it's for constant.
"a" means it is an "argument" when used as Hungarian. Alternatively it can be treated as an English article, and so will be become "anObject" or "someThings" (which would be aObject and aThings in Hungarian).
In either case it is to make it clear that you were passed this object, and so should be careful about modifying it in unexpected ways.
Regarding "k", yes that's for constant.
It's only convention. Usually, we name our instance variables in a clear/logical fashion:
@interface Foo : NSObject {
Bar * bar;
Baz * baz;
}
@end
However, we also like to be verbose with our method names, so that they are also explicit in their purpose:
- (id) initWithBar:(Bar *)bar baz:(Baz *)baz;
The problem with this is that when implementing this method, we run in to naming conflicts:
- (id) initWithBar:(Bar *)bar baz:(Baz *)baz {
if (self = [super init]) {
bar = [bar retain];
baz = [baz retain];
}
return self;
}
The problem here is that the parameter "bar" hides the instance variable named "bar", leading to a conflict and undesired behavior. To rectify this, we tack on an article (when necessary) to the parameter to indicate that it can be any Bar, or any Baz:
- (id) initWithBar:(Bar *)aBar baz:(Baz *)aBaz {
if (self = [super init]) {
bar = [aBar retain];
baz = [aBaz retain];
}
return self;
}
By convention, variables (and arguments to a methods are also variables) start with a lowercase letter. In this case, the "aController" argument could also be named just "controller" or "fluffyBunny". The author chose "aController" most likely because of aesthetic reasons.
The Apple convention to start constant variable like kCFStreamStatusAtEnd
is also just that, a convention. They could have chosen any other character instead or none at all (e.g. the constants for Cocoa start with NS
without a lowercase character, as in NSStreamStatusAtEnd
).
Then there's the so-called Hungarian Notation. It's a convention that encodes meta-data into the variable, like its type. It's use is quite controverse, there are lots of people who like and use it and others who hate it. See the Wikipedia article for arguments for both sides.