首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在NSTimeZone中将RestKit转换为Json

在NSTimeZone中将RestKit转换为Json
EN

Stack Overflow用户
提问于 2014-02-05 03:35:57
回答 1查看 323关注 0票数 0

在我的iOS应用程序中,我有一个具有NSTimeZone类型的timeZone属性的对象。我希望使用RestKit 0.22.0将其作为JSON (以及其他数据)发布。

我设置了一个值转换器块,但是它导致了一个错误,我一直无法弄清楚这个错误。

代码语言:javascript
运行
复制
RKObjectMapping *eventMapping = [RKObjectMapping requestMapping];

RKValueTransformer *timeZoneTransformer = [RKBlockValueTransformer valueTransformerWithValidationBlock:^BOOL(__unsafe_unretained Class sourceClass, __unsafe_unretained Class destinationClass) {

        // We transform a `NSTimeZone` into `NSString`
        return ([sourceClass isSubclassOfClass:[NSTimeZone class]] && [destinationClass isSubclassOfClass:[NSString class]]);

    } transformationBlock:^BOOL(id inputValue, __autoreleasing id *outputValue, Class outputValueClass, NSError *__autoreleasing *error) {

        // Validate the input and output
        RKValueTransformerTestInputValueIsKindOfClass(inputValue, [NSTimeZone class], error);
        RKValueTransformerTestOutputValueClassIsSubclassOfClass(outputValueClass, [NSString class], error);

        // Perform the transformation
        *outputValue = [((NSTimeZone *)inputValue) name];
        return YES;
    }];

    RKAttributeMapping *timeZoneAttributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"timeZone" toKeyPath:@"event.zone"];

    timeZoneAttributeMapping.valueTransformer = timeZoneTransformer;

    [eventMapping addPropertyMapping:timeZoneAttributeMapping];

我想提交给API的值是该区域的地缘政治ID。对纽约来说,这将是America\New_York

我在Xcode中添加了一个断点,以显示异常发生的位置。它发生在RK库的以下文件中,在这一行return [NSJSONSerialization dataWithJSONObject:object options:0 error:error];中。

代码语言:javascript
运行
复制
#import "RKNSJSONSerialization.h"

@implementation RKNSJSONSerialization

+ (id)objectFromData:(NSData *)data error:(NSError **)error
{
    return [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
}

+ (NSData *)dataFromObject:(id)object error:(NSError **)error
{
    return [NSJSONSerialization dataWithJSONObject:object options:0 error:error];
}

例外是'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSTimeZone)'

有趣的是,当Xcode在上面提到的返回语句处中断时,在变量视图中,dataFromObject消息的dataFromObject参数实际上包含了我希望提交给服务器的值(America/New_York)。看来挺管用的。

此外,在变量视图中,error参数的dataFromObject值为"error: summary string parsing error"

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-05 18:05:54

仔细检查Xcode的变量视图中的变量时,我试图映射到JSON的NSTimeZone对象实际上并不是映射到NSString。我认为它是正确的映射,因为变量的值是应该是的(即America/New_York)。然而,类型仍然是NSTimeZone。因此,根本没有调用转换代码块。

然后,我在RKCLLocationValueTransformer示例这里中看到了这个示例代码。

代码语言:javascript
运行
复制
RKObjectMapping *userRequestMapping = [RKObjectMapping requestMapping];
[userRequestMapping addAttributeMappingsFromArray:@[ @"name" ]];
RKAttributeMapping *attributeMapping = [RKAttributeMapping
    attributeMappingFromKeyPath:@"location" toKeyPath:@"location"];
attributeMapping.propertyValueClass = [NSDictionary class];
attributeMapping.valueTransformer = [RKCLLocationValueTransformer
    locationValueTransformerWithLatitudeKey:@"latitude" longitudeKey:@"longitude"];
[userRequestMapping addPropertyMapping:attributeMapping];

NSError *error = nil;
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor
    requestDescriptorWithMapping:userRequestMapping objectClass:[User class]
    rootKeyPath:@"user" method:RKRequestMethodAny];

问题是我没有指定propertyValueClass属性。

attributeMapping.propertyValueClass = [NSDictionary class];

propertyValueClass属性指定希望将映射的值表示为或转换为的类型。

代码应该是这样的。

代码语言:javascript
运行
复制
RKAttributeMapping *timeZoneAttributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"timeZone" toKeyPath:@"event.zone"];

timeZoneAttributeMapping.propertyValueClass = [NSString class];

timeZoneAttributeMapping.valueTransformer = timeZoneTransformer;

[eventMapping addPropertyMapping:timeZoneAttributeMapping];

这里是指向此属性的文档的链接。

有关NSInvalidArgumentException原因的详细信息可以找到这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21568162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档