如何使用两种类型的相同变量在Objective-C中创建JSONModel类?

时间:2022-02-27 20:00:03

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

我在Objective-C中有一个JSONModel类。我正在使用服务器返回的JSON初始化它。

#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.

这个JSON包含一个名为idFilm的变量,有时会返回NSNumber类型,但是在数组中返回另一个。

For exaple:

对于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?

我怎么能控制它,我怎么能有两个类型NSNumber和NSArray定义的相同变量?有可能控制这个吗?

1 个解决方案

#1


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

我不知道是否可以工作,但可以作为一个起点

#1


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

我不知道是否可以工作,但可以作为一个起点