Why do constants in all examples 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. Is there some tutorial somewhere that explains these sorts of things without assuming knowledge of C?
No reserved words start with k so it is easier as a search target. Alternatively, from a graphical aspect, as a leading character it provides a very clear flag in front of the rest of the name.
A constant is a name given to the variable whose values can't be altered or changed. A constant is very similar to variables in the C programming language, but it can hold only a single variable during the execution of a program.
Constants in C are the fixed values that are used in a program, and its value remains the same during the entire execution of the program. Constants are also called literals. Constants can be any of the data types.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With