-8

I receive from from the server the following JSON:

{
   .....
   "id":"predifined_id",
   ...
}

In the code I have an int definition for the predifined_id:

#define predifined_id   5

Which means that if I use predifined_id in code it will be translated to int = 5.

How can I fetch this value (5) when I am parsing the JSON?

I have converted the JSON to NSDictionary, but the value that I receive for @"id" key, is NSString

Luda
  • 7,282
  • 12
  • 79
  • 139
  • Why don't you just `#define predefined_id @"5"` ? – Paulw11 Nov 04 '14 at 12:03
  • the macros for _compiler time_ only, you cannot deal with them during _runtime_ – they just don't exist in runtime. – holex Nov 04 '14 at 12:06
  • 1
    @l0gg3r, totally different question – Luda Nov 04 '14 at 12:09
  • @Paulw11, predefined_id will still not be recognised as macro when parsing the JSON. It only be a NSString – Luda Nov 04 '14 at 12:13
  • I don't understand what you are trying to do. All #define does is cause the compiler to make a substitution when you compile the program. Do you want to convert the string value from the dictionary to an int? If so, then @mityaika07 is correct – Paulw11 Nov 04 '14 at 12:16
  • @Paulw11 No, I want to convert the string value from the dictionary to the macro – Luda Nov 04 '14 at 12:18
  • You can't convert a string to a macro. A macro is just a compile-time text substitution – Paulw11 Nov 04 '14 at 12:19

2 Answers2

1

in my code I use this:

NSInteger integerValue = [dict[@"id"] integerValue];
mityaika07
  • 652
  • 5
  • 14
0

You could simply define it as string

#define predifined_id  @"5"

or convert it later:

[NSString stringWithFormat:@"%d",predefined_id]
nicael
  • 18,550
  • 13
  • 57
  • 90