开发者

iPhone - defining a conditional variable without a method

I have a file called myConstants.h that I use to put all the constants used by the app. This file is imported in any class that needs to access one of the variables.

I am building a universal app and I have variables that I need to have different values if they are running on iPad or iPhone.

I give you a generic example of what I mean.

In my main code I may have something like:

self.DEVICE = @"iphone";
self.IPAD = NO;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    self.DEVICE = @"ipad";
    self.IPAD = YES;
}

But this is define on the main code. If I need a cla开发者_如何学Css to use one of these two variables I need to construct complex methods to reference the main class and read the variables there.

My idea is to include this in myConstants.h.

But how can I do that if myConstants.h is just a plain file where every line is a #define entry without any method?

My idea is to have DEVICE and IPAD variables as globals for the entire app and make them available to any class by just #import "myConstants.h".

Is this possible? How?

thanks


I don't think #define blocks will work in this case since those are interpreted at compile time. How would your compiler know which device the user is using?

I do something in one of my apps that uses UIDevice extension to detect the device type at run time (in if statements). This works fine.


You can make use of the precompiler:

#if TARGET_OS_IPHONE
NSString *device_ = @"iphone"; 
#endif TARGET_OS_IPHONE

etc.

Then define your directives in the Build properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜