0

I have a JSONModel class in Objective-C. I am initializing this with JSON returned by a server.

#import "JSONModel.h"

@protocol MyJsonMoodelClass

@end

@interface MyJsonMoodelClass : JSONModel

@property (nonatomic, strong)  NSNumber <Optional>  * idFilm;

This JSON contains a variable called idFilm, and sometimes is returned of type NSNumber but another is returned in array.

For exaple:

idFilm : 5

or

idFilm : [2, 5]

How could I control this and how could I have the same variable defined by two types, NSNumber and NSArray? Is possible control this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3745888
  • 6,143
  • 15
  • 48
  • 97
  • 1
    You could set it to an NSObject which is superclass of both NSNumber and NSArray. But this is not really a good idea in most situations as you'd always have to be checking which type it is before doing anything with it (`isKindOfClass`), and it doesn't naturally convert to JSON, in your case, without extra work. Better would be to always use an array, and if you get a number, put that number in an array and use the array instead (ie, an array with only one element). – Son of a Beach Dec 22 '16 at 01:16

1 Answers1

0

if you override "set" method? something like this:

- (void)setIdFilm:(id)idFilm
{
    if([idFilm isKindOfClass:NSArray])
        ...
    else
        ...
}

I don't know if can work but can be a starting point

francesco.venica
  • 1,703
  • 2
  • 19
  • 54