在我的iOS应用程序中,我有一个具有NSTimeZone类型的timeZone属性的对象。我希望使用RestKit 0.22.0将其作为JSON (以及其他数据)发布。
我设置了一个值转换器块,但是它导致了一个错误,我一直无法弄清楚这个错误。
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];中。
#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"。
发布于 2014-02-05 18:05:54
仔细检查Xcode的变量视图中的变量时,我试图映射到JSON的NSTimeZone对象实际上并不是映射到NSString。我认为它是正确的映射,因为变量的值是应该是的(即America/New_York)。然而,类型仍然是NSTimeZone。因此,根本没有调用转换代码块。
然后,我在RKCLLocationValueTransformer示例这里中看到了这个示例代码。
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属性指定希望将映射的值表示为或转换为的类型。
代码应该是这样的。
RKAttributeMapping *timeZoneAttributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"timeZone" toKeyPath:@"event.zone"];
timeZoneAttributeMapping.propertyValueClass = [NSString class];
timeZoneAttributeMapping.valueTransformer = timeZoneTransformer;
[eventMapping addPropertyMapping:timeZoneAttributeMapping];这里是指向此属性的文档的链接。
有关NSInvalidArgumentException原因的详细信息可以找到这里。
https://stackoverflow.com/questions/21568162
复制相似问题