0

Is there any way to simply declare static NSString as defined int identifier ? I want to do something like in C++ #define MY_SIMPLE_ID 4.

EDIT: Where should I declare this? In C++ I have global access to resource file with it. Is there a way to do that in Objective-C?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jakub
  • 13,712
  • 17
  • 82
  • 139

3 Answers3

5

Why not using :

#define MY_STRING @"MyString"
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • But which class i should declare this macro? In visual c++ you have a global access to defined resources. Is there a something like this in objective-c? – Jakub Apr 10 '12 at 08:05
  • 2
    Just make an H file with all your defines and include that file in your precompiled header (.pch file). That's what I do. – borrrden Apr 10 '12 at 08:07
1

You can also go into your Project or Target Build Settings, and add to Preprocessor Macros or Preprocessor Macros Not Used In Precompiled Headers. See Xcode Preprocessor Macros for the distinction between these two options.

Community
  • 1
  • 1
Cowirrie
  • 7,218
  • 1
  • 29
  • 42
0

Personally i use this in a separate class with only static values:

in the constants.h file:

extern int const MY_SIMPLE_ID;
extern NSString const MY_SIMPLE_STRING;

in the constants.m file:

int const MY_SIMPLE_ID = 4;
NSString const MY_SIMPLE_STRING = @"thestring";

With this it's a global static value in the entire application.

Manuel
  • 10,153
  • 5
  • 41
  • 60