Re: Objective-C: What is the current preferred method of declaring constants without a .pch?

Alex Zavatone

Thanks Steve.  That’s one option I’ve been considering.  It’s a balance between knowing where to look for everything and properly scoping some constant to the one place where I use it. If there’s one thing that wastes my time, it’s hunting through my project for a constant, but if it only needs to be in one place, then I probably should learn to expect looking for it in the class that uses it.  

FYI, if anyone needs a little more explanation of how to handle this with modules, I just found this relatively recent link that may help us stay current.

It mentions how to use the umbrella header within modules if you’re using them.  

Also Ash Furrow has some good input.

Thanks for your time everyone.

- Alex Zavatone

On Oct 19, 2017, at 2:54 PM, Steve Christensen <punster@...> wrote:

I use constants a lot, but I put their declarations and definitions into the "owner" class files. For example if one object sources notifications and others consume them.


extern NSString* const __nonnull FooNotification;


NSString* const FooNotification = @"FooNotification";

With Obj-C I have rarely ended up with true globals since I can create singletons as "global" instances of particular classes.

I have created .h files containing some #defines that are used to conditionally compile certain features that may not be quite ready for primetime and so it could get pulled before an app ships.


On Oct 19, 2017, at 12:11 PM, Alex Zavatone <zav@...> wrote:

I’ve read the Apple docs and looked around and can’t find definitive sources on what the preferred method is and how to do it.

Do we use modules?  If so, how?

I understand that a .pch is now a “bad idea”.  OK.  What do we do instead?  Why?

Is it good practice to create one constants.h/m?  Or is that just another way of masking a global (which is bad)?

It does make everything easy to find.

Should we create one .h/.m file with multiple constants classes within?

I can easily go back to using a .pch, but a clear path forward to the current year’s practice would be nice.

Thanks in advance.

- Alex Zavatone

Join to automatically receive all group messages.