Domanda

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).

È stato utile?

Soluzione

Either use a function:

uint timeout() {return SecondsInMinute;}

or an enum (in your header) when you need a compile time constant:

enum { SecondsInMinute = 60 };

Altri suggerimenti

// appConstants.h
#ifndef appConstants
#define appConstants
uint const SecondsInMinute = 60u;
#endif

// viewController.m
#import "appConstants.h"
uint const timeout = SecondsInMinute;

It's a non-problem. You should probably take a deep breath and get back to some real work.

You seem to think there is something special about const variables. There isn't. They are variables like any other variables. They are not "compile-time constants" as your comments suggest you think they are. They are ordinary variables, set at runtime in the ordinary way. const is a hint to the compiler that it might like to store this value in a special way because it is guaranteed not to change, but the compiler is not obligated to treat the value specially. So you are not doing yourself any special good worrying about it. It is not even clear why you are using const in your code at all. You seem to be worried about "efficiency", but it is not obvious what you think that means or how const is efficient. #define is certainly efficient in the sense that it is completely inline: it is a text substitution performed before the compiler even comes into play.

Another revealing passage in your comments is "I don't know enough C". That is very clear. I strongly recommend that you stop and read at least the relevant passages of Kernighan and Ritchie before concerning yourself with this sort of thing. I also suggest you read this question:

#define vs const in Objective-C

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top