我正在尝试使用 RestKit 0.24.0 将 JSON NSString 映射到一个对象中,其中我有一个 RKObjectMapping 以及相应的字典。
网上的大部分解决方案都是参考RestKit 0.22及以下。
SO上的一些解决方案导致应用程序崩溃。
将本地字符串转换为对象的最简单方法是什么。考虑以下
代码更新
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:[RJobObject mappingDictionary]];
RJobObject * rrrr = [[RJobObject alloc] init];
mapper.targetObject = rrrr;
//mapper.mappingOperationDataSource = parsedData;
[mapper execute:nil];
这里,mappingDictionary
基本上是 JSON 键和 Object 变量名之间的键值对匹配 所以在上面的代码中,每当我运行execute
时,应用程序就会崩溃。
堆栈跟踪
2015-11-22 09:12:29.193 Help[22427:699579] -[__NSCFConstantString forceCollectionMapping]: unrecognized selector sent to instance 0x1022736a0
2015-11-22 09:12:29.211 Help[22427:699579] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString forceCollectionMapping]: unrecognized selector sent to instance 0x1022736a0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105d7dc65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000105a14bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000105d850ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000105cdb13c ___forwarding___ + 988
4 CoreFoundation 0x0000000105cdacd8 _CF_forwarding_prep_0 + 120
5 Help 0x000000010212f424 -[RKMapperOperation mapRepresentationOrRepresentations:atKeyPath:usingMapping:] + 132
6 Help 0x000000010212fe97 -[RKMapperOperation mapSourceRepresentationWithMappingsDictionary:] + 1959
7 Help 0x00000001021307a2 -[RKMapperOperation main] + 1330
8 Foundation 0x00000001034c4774 -[__NSOperationInternal _start:] + 645
9 Help 0x00000001021310b7 -[RKMapperOperation execute:] + 39
10 Help 0x0000000101f62f8e -[AppDelegate fetchJobsFromDB] + 1102
更多细节
更新到RestKit 0.26.0
,我仍然面临这个问题。
基本上RKMapperOperation.m
,第333行apo mapping.forceCollectionMapping
给出了一个错误,它使应用程序崩溃。
回答:
使用示例解决方案RKMappingOperation
,因为我们不必指定 keyPath
RJobObject * rrrJob = [RJobObject new];
RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:jobEntity.dictionary destinationObject:rrrJob mapping:[RJobObject mapping]];
mappingOperation.dataSource = (id)rrrJob;
[mappingOperation start];