Since "id" is a keyword in Objective-C what alternative name do you use for ID variable (e.g. PK field)?
问题:
回答1:
I think it should be noted that the compiler can distinguish between id
in the type specifier position and id
in the variable name position. That is,
NSUInteger id;
id = 10;
will compile just fine (as, indeed, will id id; id = [NSNumber numberWithInt:10];
). (You could also uppercase it: ID
.) That said, those are all horrible ideas. Don't use them. Forget I even said that.
The style in Cocoa programming is to tend towards verbosity, so (as all the earlier answers have suggested) the best practice is probably to write it all out: identifier
or dinglehopferID
.
回答2:
identifier
, or something more specific like userID
.
回答3:
It often depends on context. Simply, identifier
if I'm not feeling particularly creative.
回答4:
I tend to use either 'identifier' — if there's really no better alternative — or something domain specific like 'programmeID' which sort of says 'this is the primary key, but only with respect to this domain'.
It's actually quite rare that you have to think in terms of primary keys within Cocoa. Core Data preserves object graphs without any nomination of primary keys, NSPredicate
s don't give inherently any additional weight to a field that happens to be unique per object and NSDictionary
s tend to be built in an adhoc fashion.
回答5:
I almost use 'guid' NSString *guid
NSNumber *guid
.
http://en.wikipedia.org/wiki/Globally_unique_identifier
回答6:
Using id is a bad idea for me. I almost use 'uid' or 'cid' for Users and Clients respectively. I use the first letter of the model name to avoid the reserved word.