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


Steve Christensen <punster@...>
 

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.

Foo.h

extern NSString* const __nonnull FooNotification;


Foo.m

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.

Steve

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 {cocoa@apple-dev.groups.io to automatically receive all group messages.