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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.