gpt4 book ai didi

ios - RestKit 不在 64 位模拟器中映射 BOOL

转载 作者:可可西里 更新时间:2023-11-01 05:21:32 27 4
gpt4 key购买 nike

我需要帮助来理解下面的错误。

我的 NSObject 中有一个 BOOL 定义为:

@property (nonatomic) BOOL isOpen;

在我的 reskit 类(class)中,我使用:

 [mapping addAttributeMappingsFromDictionary:@{......
@"isOpen": @"isOpen"
......}];

在我使用 iPhone4S 和原装 iPhone5 进行的所有测试中,它都完美运行了数周。作为最后一次测试,我在 64 位设备上的模拟器上进行了测试,但出现以下错误:

 restkit.object_mapping:RKMappingOperation.m:440 Failed transformation of value at keyPath 'isOpen' to representation of type '__NSCFBoolean': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value '1' to __NSCFBoolean: none of the 2 value transformers consulted were successful." UserInfo=0x10cba1c00 {NSLocalizedDescription=Failed transformation of value '1' to __NSCFBoolean: none of the 2 value transformers consulted were successful., detailedErrors=(
"Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 \"The given value is not already an instance of '__NSCFBoolean'\" UserInfo=0x10cb86d30 {NSLocalizedDescription=The given value is not already an instance of '__NSCFBoolean'}",
"Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3000 \"Expected an `inputValue` of type `NSNull`, but got a `__NSCFNumber`.\" UserInfo=0x10cbeb400 {NSLocalizedDescription=Expected an `inputValue` of type `NSNull`, but got a `__NSCFNumber`.}"
)}

最佳答案

正如 Mikael 所说,在 64 位平台上运行的 RestKit 中存在一些故障,阻止了从 NSNumber 到 BOOL 的默认转换(反之亦然)。

不过,由于 RestKit Value Transformers 模块化架构,有一种方法可以使这项工作正常进行。因此,您只需创建一个专用的转换器类并将其注册到 RestKit 默认转换器。

这就是变压器的样子:

@interface RKCustomBOOLTransformer : NSObject  <RKValueTransforming>

+ (instancetype)defaultTransformer;

@end

@implementation RKCustomBOOLTransformer

+ (instancetype)defaultTransformer {
return [RKCustomBOOLTransformer new];
}

- (BOOL)validateTransformationFromClass:(Class)inputValueClass toClass:(Class)outputValueClass {
return ([inputValueClass isSubclassOfClass:[NSNumber class]] &&
[outputValueClass isSubclassOfClass:[NSNumber class]]);
}

- (BOOL)transformValue:(id)inputValue toValue:(id *)outputValue ofClass:(Class)outputValueClass error:(NSError **)error {
RKValueTransformerTestInputValueIsKindOfClass(inputValue, (@[ [NSNumber class] ]), error);
RKValueTransformerTestOutputValueClassIsSubclassOfClass(outputValueClass, (@[ [NSNumber class] ]), error);

if (strcmp([inputValue objCType], @encode(BOOL)) == 0) {
*outputValue = inputValue?@(1):@(0);
} else if (strcmp([inputValue objCType], @encode(int)) == 0) {
*outputValue = ([inputValue intValue] == 0) ? @(NO) : @(YES);
}
return YES;
}

@end

你这样注册:

[[RKValueTransformer defaultValueTransformer]
insertValueTransformer:[RKCustomBOOLTransformer defaultTransformer] atIndex:0];

在定义映射之前注意进行注册。

关于ios - RestKit 不在 64 位模拟器中映射 BOOL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22175313/

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