-1

I would like to define "constant" global variables according to whether the user is on an iPad or an iPhone. For now, I am trying to include this file, globals.h inside my constants.pch file:

    //  globals.h
    //  BJ


    #ifndef BJ_globals_h
    #define BJ_globals_h

    float LARGEST_FONT_SIZE = 30.0f;
    float LARGE_FONT_SIZE = 20.0f;
    float SMALL_FONT_SIZE = 16.0f;
    float SMALLEST_FONT_SIZE = 12.0f;
    float FONT_SIZE = 18.0f;
    float CELL_CONTENT_WIDTH = 320.0f;
    float CELL_MIN_HEIGHT = 50.0f;
    float CELL_CONTENT_MARGIN = 10.0f;
    float MIN_CELL_HEIGHT = .2f;
    float SCROLL_VIEW_OFFSET = 0.1f;
    float TABLE_VIEW_HEIGHT = 0.45f;

    #endif

But when I include this in constants.pch with this call at the top of that file:

    //  constants.pch

    #import "globals.h"

    #ifndef BJ_constants_pch
    #define BJ_constants_pch
    /*
    #define LARGEST_FONT_SIZE 30.0f
    #define LARGE_FONT_SIZE 20.0f
    #define SMALL_FONT_SIZE 16.0f
    #define SMALLEST_FONT_SIZE 12.0f
    #define FONT_SIZE 18.0f
    #define CELL_CONTENT_WIDTH 320.0f
    #define CELL_MIN_HEIGHT 50.0f
    #define CELL_CONTENT_MARGIN 10.0f
    #define MIN_CELL_HEIGHT .2f
    #define SCROLL_VIEW_OFFSET 0.1f
    #define TABLE_VIEW_HEIGHT 0.45f
    */


 ...
    #endif

I actually get a Mach-O Linker Error:

linker command failed with exit code 1 (use -v to see invocation)

I do not get this error if I try to include globals.h in other files, say in custom classes, but I don't want to have to include it individually in each file. Is there something different I have to do to include this file in constants.pch? Is there another way to easily and conditionally define global variables across the app? I want to set font and cell sizing depending on whether the user is on an iphone or an ipad.

Thank you for any suggestions.

Ps the ultimate goal of this is to conditionally set global variables for font size across the application. So I need a file that can be processed at run time with conditional statements.

2 Answers2

0

I include my constants file in the .pch after #import <UIKit/UIKit.h> and #import <Foundation/Foundation.h>.

The difference between yours and mine is that mine is like this:

KRConstants.h:

#import <Foundation/Foundation.h>

FOUNDATION_EXPORT const int ddLogLevel;

// ...

KRConstants.m

const int ddLogLevel = LOG_LEVEL_VERBOSE;

// ...
Kyle Robson
  • 3,060
  • 24
  • 20
  • Why do you put it in a class? – Amin Negm-Awad Dec 24 '14 at 21:12
  • That's a good question. It doesn't need to be in a class. I guess I just created the file via Xcode that way and didn't give it much thought since it works either way. Here's the Stack Overflow question that taught me my way of doing constants: http://stackoverflow.com/a/539191/620577 Also I'm editing my answer to remove the class. – Kyle Robson Dec 24 '14 at 22:40
0

You can't include the constant like this way. You can't include a header file to pch, that contains constants like:

float CELL_CONTENT_MARGIN = 10.0f;

You either need to define it using #define or extern

Like:

#define CELL_CONTENT_MARGIN 10.0f

or

extern const float CELL_CONTENT_MARGIN;
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • So my original question is declaring global variables not constants. Can I put global variables in constants.pch? Ultimately I want to set global variables for font size conditionally depending on what device the user has. – Sunnyside Productions Dec 26 '14 at 20:51
  • @SunnysideProductions: No, you can't. You need to include them in AppDelegate or any singleton object in that case – Midhun MP Dec 26 '14 at 21:38