gpt4 book ai didi

ios - 不尊重 JSONModel Optional 和 Ignore

转载 作者:行者123 更新时间:2023-11-29 12:25:42 25 4
gpt4 key购买 nike

我在头文件中有这些属性。

@property (copy, nonatomic) NSString<Optional>* guidId;
@property (copy, nonatomic) NSDate* created_at;
@property (copy, nonatomic) NSNumber<Ignore>* longitude;
@property (copy, nonatomic) NSNumber<Ignore>* latitude;
@property (copy, nonatomic) NSNumber* altitude;
@property (copy, nonatomic) NSNumber* relative_altitude;
@property (copy, nonatomic) NSNumber* value;
@property (copy, nonatomic) NSString<Optional>* scale;
@property (copy, readonly, nonatomic, getter=getReadingTypeDescription) NSString* name;
@property (assign, nonatomic) WEEReadingType readingType;
@property (copy, nonatomic) NSString* name;
@property (strong, nonatomic) NSMutableArray* location;

虽然很奇怪 Optional 和 Ignore 没有被尊重。

在实现文件中我覆盖了这两个方法。

// This one because when converting to JSON from Model I need specific properties only and Ignore is not for this purpose

- (NSString *)toJSONString
{
return [super toJSONStringWithKeys:
@[
@"guidId",
@"location",
@"altitude",
@"relative_altitude",
@"scale",
@"name",
@"value",
@"created_at"
]];
}


// And this one for the enum property that I want to ignore, but I found out I get an error when using [JSONModel arrayOfModelsFromDictionaries:json error:&error] that Optional or Ignore is not respected.

+ (BOOL)propertyIsIgnored:(NSString *)propertyName
{
if ([propertyName isEqualToString:@"readingType"] ||
[propertyName isEqualToString:@"guidId"] ||
[propertyName isEqualToString:@"longitude"] ||
[propertyName isEqualToString:@"latitude"])
{
return YES;
}
return NO;
}

我需要转换为我的模型的 JSON 示例。

[
{
"created_at": "2015-03-20T19:26:28.000Z",
"location": [
40.70739851802653,
-74.005788154969
],
"altitude": 4.063048774263429,
"relative_altitude": 0.0,
"name": "pressure",
"value": 1014.718704223633,
"scale": "mb"
},
{
"created_at": "2015-03-20T19:26:28.000Z",
"location": [
40.70739851802653,
-74.005788154969
],
"altitude": 4.063048774263429,
"relative_altitude": 0.0,
"name": "pressure",
"value": 1014.718704223633,
"scale": null
}
]

上面的 [JSONModel arrayOfModelsFromDictionaries:json error:&error] 将抛出以下错误。

Error Domain=JSONModelErrorDomain Code=1 "Invalid JSON data: Value of required model key scale is null" UserInfo=0x170265cc0 {NSLocalizedDescription=Invalid JSON data: Value of required model key scale is null, kJSONModelKeyPath=[46].scale}

知道哪里出了问题吗?

最佳答案

只需尝试使用 +(BOOL)propertyIsOptional:(NSString*)propertyName

关于ios - 不尊重 JSONModel Optional 和 Ignore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29217260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com