Question

I have an iOS application I'm writing. I've moved away from #define to create my constant values. I have a few questions regarding the use of these style declarations: NSString *const segueToMainMenu

  1. If I'm using these internally, I'm placing them inside the .m file. However, should I be placing these wrapped around the @implementation block or outside of it or does it matter? I'm thinking inside, because they are specific to the implementation and not global, but I'm not sure. Any details are appreciated.

  2. If I'm creating a more global scope using the extern keyword and I'm using a Constants file pair (Constants.h/Constants.m) do I need to place those in the @interface section and then define them in the @implementation section? What is the benefit of that vs the old way of just using a Constants.h file and including it with other headers? Why do I now need two files?

  3. Is the standard practice still to name the constants with a "k" prefix (e.g. kAnimationDuration) or should I now be doing something like MainMenuViewControllerAnimationDuration? I'm imagining yes and if so, does it matter for the constants from number 1 (i.e. not extern) how I name them? In other words, are those visible outside of my implementation?

Clarification is much appreciated.

Was it helpful?

Solution

Doesn't matter whether you place them inside the implementation block or not—only methods are part of the class implementation, so the scope of constants won't change regardless.

The k prefix is a bit dated now. The usual way is to name constants as <prefix><name>, such as "MDSomeConstant".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top