I have an app that deals with time. I only want to specify how many seconds are in a minute once in my whole project. In fact, as a matter of principle I want my app binary to have as few redundant copies of this datum as possible (so defines are a last resort). Naively I try this:
// appConstants.h
#ifndef appConstants
#define appConstants
extern uint const SecondsInMinute;
#endif
// appConstants.m
#import "appConstants.h"
uint const SecondsInMinute = 60u;
// viewController.m
#import "appConstants.h"
uint const timeout = SecondsInMinute;
This gives me an "initializer element is not a compile-time constant"
error on the timeout
const definition.
I'll avoid recounting the tales of all the red-herrings I've chased down looking for a solution to this problem (google is full of enough of those, yet no actual answers). Instead I'll just put it simply: is it possible to reference a const in another file's const in objective c?
Update
In order to address the 'it should work' style answers I have created a simple sample project on github with the above code. One small modification needed to be made (timeout
const is renamed to timeoutSeconds
). The project was created as an empty iOS project in Xcode 5.0 with the appConstants
and viewController
files added as described above (except for the aforementioned const rename).