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.
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.
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.
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.