2

Using Objective-C, I have a dictionary of 30 entry, I am trying to define it in my Constants file the same way I am defining String or Int:

define key @"value"

Is it possible to define a dictionary in this way?
If yes, what's the syntax to define an Array?

Thanks a lot.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
sazz
  • 3,193
  • 3
  • 23
  • 33
  • 1
    Use shorthand syntax: `@{@"key":@"value"}` or `@[@"item1"]` and you can put them into #define. – Larme Jan 18 '17 at 10:27

1 Answers1

3

You can define whatever you want. If you write

#define identifier replacement

Preprocessor during compilation (actually before he compiles anything) looks for indentifier in your source code and replaces it with replacement.

You can define your dictionary or array in this way

#define kDictionary @{"key" : "value"\
                      "key2" : "value2"}
#define kArray @["item1", "item2"]

Backslash at the end of line is required for multiline defines.

However I don't recommend to use #define. You should rather create class with class method that will provide you with the dictionary/array. It is less error prone.

Joey
  • 164
  • 9