views:

1348

answers:

3

How how constants in all examplese I've seen always start with k? And should I #define constants in header or .m file? I'm new to objective c, and I don't know c. All tutorials and books assume you know c, so I don't understand these things, is there some tutorial somewhere that explains stuff like this? Thanks.

+6  A: 

Starting constants with a "k" is a legacy of the pre-Mac OS X days. In fact, I think the practice might even come from way back in the day, when the Mac OS was written mostly in Pascal, and the predominant development language was Pascal. In C, #define'd constants are typically written in ALL CAPS, rather than prefixing with a "k".

As for where to #define constants: #define them where you're going to use them. If you expect people who #import your code to use the constants, put them in the header file; if the constants are only going to be used internally, put them in the .m file.

mipadi
Interestingly, Google Code Style Guide requires constants to start with a 'k' as well. I am amused when people who hate hungarian notation use this convention. :)
jeffamaphone
I decided to put my constant in the .m file, after the #imports and before the @implementation.
Mk12
Can't you also make a .h file for constans and then do extern const something something? How does that work?
Mk12
You can, if you want. #defines work the same way. If you want a C "const" variable, you can do something like `extern NSString * const MY_CONST` in a .h file, and then in a .m file make a global `NSString * const MY_CONST = @"My Constant";` -- the `extern` keyword just means the constant is defined in another file.
mipadi
Are there any advantages to using that method?
Mk12
`#define` is basically a search-and-replace; so if you `#define MYCONST @"My Constant"`, then a string will be allocated everywhere you use `MYCONST`. However, if you use `extern NSString * const MYCONST = @"My Constant";`, the string will only be allocated once, and all uses of `MYCONST` will point to the same string. For other `#define`'d datatypes (e.g., `#define MYCONST 4.0`), it makes little difference.
mipadi
+5  A: 

The question of what the "k" means is answered in this question.

And if you intend for files other than that particular .m to use these constants, you have to put the constants in the header, since they can't import the .m file.

You might be interested in Cocoa Dev Central's C tutorial for Cocoa programmers. It explains a lot of the core concepts.

Chuck
+3  A: 

k for "konvention". Seriously; it is just convention.

You can put a #define wherever you like; in a header, in the .m at the top, in the .m right next to where you use it. Just put it before any code that uses it.

The "intro to objective-c" documentation provided with the Xcode tool suite is actually quite good. Read it a few times (I like to re-read it once every 2 to 5 years).

However, neither it nor any of the C books that I'm aware of will answer these particular questions. The answers sort of become obvious through experience.

bbum