使用 RESTkit 从 JSON 映射来自 NSManagedObject 的 NSString 数组
Posted
技术标签:
【中文标题】使用 RESTkit 从 JSON 映射来自 NSManagedObject 的 NSString 数组【英文标题】:Mapping array of NSString's from NSManagedObject from JSON using RESTkit 【发布时间】:2014-04-10 12:06:55 【问题描述】:一些先决条件:
JSON 正文:
"returnCode":“messages”[], "something":1
应该映射到:
@interface ReturnCodeEntity : NSMutableObject
@property (nonatomic, retain) id messages; // Should be defined as Transformable?
@property (nonatomic, retain) NSString * something;
问题:
你好,我猜这是两个问题:
-
为核心数据实体中的“消息”数组定义正确的类型
使用 RKEntityMapping 类映射此数组
现在映射操作完成后出现异常:
2014-04-10 12:12:05.661 [4811:3f07] D restkit.object_mapping:RKMappingOperation.m:929 Finished mapping operation successfully...
2014-04-10 12:12:05.661 [4811:3f07] -[__NSDictionaryM _stateFlags]: unrecognized selector sent to instance 0x10990d0b0
2014-04-10 12:12:08.786 [4811:3f07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM _stateFlags]: unrecognized selector sent to instance 0x10990d0b0'
First throw call stack:
(
0 CoreFoundation 0x0000000102af8495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010285799e objc_exception_throw + 43
2 CoreFoundation 0x0000000102b8965d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000102ae9d8d ___forwarding___ + 973
4 CoreFoundation 0x0000000102ae9938 _CF_forwarding_prep_0 + 120
5 CoreData 0x00000001008ec8f8 -[NSRelationshipDescription(_NSInternalMethods) _nonPredicateValidateValue:forKey:inObject:error:] + 232
6 CoreData 0x00000001008eba27 -[NSManagedObject(_NSInternalMethods) _validateValue:forProperty:andKey:withIndex:error:] + 375
7 CoreData 0x00000001009651d7 -[NSManagedObject validateValue:forKey:error:] + 135
8 Foundation 0x0000000100f19ed5 -[NSObject(NSKeyValueCoding) validateValue:forKeyPath:error:] + 336
9 0x0000000100212cb5 -[RKMappingOperation validateValue:atKeyPath:] + 341
10 0x000000010021334d -[RKMappingOperation shouldSetValue:forKeyPath:usingMapping:] + 861
11 0x00000001002177dd -[RKMappingOperation mapOneToOneRelationshipWithValue:mapping:] + 1661
12 0x000000010021b14a -[RKMappingOperation applyRelationshipMappings] + 6186
13 0x000000010021d2f6 -[RKMappingOperation main] + 4006
14 Foundation 0x0000000100f2bd34 -[__NSOperationInternal _start:] + 623
15 0x000000010020ca5e -[RKMapperOperation mapRepresentation:toObject:atKeyPath:usingMapping:metadata:] + 1886
16 0x000000010020b3ff -[RKMapperOperation mapRepresentation:atKeyPath:usingMapping:] + 1871
17 0x000000010020dd75 -[RKMapperOperation mapRepresentationOrRepresentations:atKeyPath:usingMapping:] + 805
18 0x000000010020e5a8 -[RKMapperOperation mapSourceRepresentationWithMappingsDictionary:] + 1944
19 0x000000010020eef5 -[RKMapperOperation main] + 1365
在groups.google.com 上进行了讨论,但最终得到了解决方法。
感谢您的建议。
编辑
我在体内引入了台风。是的,这篇文章中缺少分号
"returnCode":"messages":[], "something":1
我对模型进行了一些更改,因此 ReturnCodeEntity 包含引用新类型 MessageEntity 的 NSSet,该类型将消息存储在“文本”属性中:
@interface ReturnCodeEntity : NSMutableObject
@property (nonatomic, retain) NSString * something;
@property (nonatomic, retain) NSSet *messages; // --> relationship 1 to many MessageEntitie's
@end
@interface ReturnCodeEntity (CoreDataGeneratedAccessors)
- (void)addMessagesObject:(MessageEntity *)value;
- (void)removeMessagesObject:(MessageEntity *)value;
- (void)addMessages:(NSSet *)values;
- (void)removeMessages:(NSSet *)values;
@end
@interface MessageEntity : NSMutableObject
@property (nonatomic, retain) NSString * text;
@property (nonatomic, retain) ReturnCodeEntity *returnCode; // --> relationship 1 to 1 ReturnCodeEntity
@end
我已经为 MessageEntity 创建了映射:
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"MessageEntity" inManagedObjectStore:managedObjectStore];
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"text"]];
mapping.identificationAttributes = @[ @"text" ];
... 并将其添加到 returnCodeEntityMapping 中
[returnCodeEntityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"messages" toKeyPath:@"messages" withMapping:messageApiMapping]];
跟踪表明映射在层次结构中成功,但后来它在 RKMappingOperation.m 方法中崩溃 *- (BOOL)validateValue:(id *)value atKeyPath:(NSString )keyPath
success = [self.destinationObject validateValue:value forKeyPath:keyPath error:&validationError];
谢谢你的帮助。
【问题讨论】:
想必JSON实际上是"returnCode":“messages”:[], "something":1
?
【参考方案1】:
我的问题是为 MessageEntity 映射而不是 RKAttributeMapping 实例化 RKObjectMapping。
这是因为我从另一个使用 [RKObjectMapping requestMapping] 的实体类中复制了代码(因为请求)。我必须补充一点(从 Restkit 用户的角度来看)使用 RKObjectMapping 进行请求是令人困惑的,即使在后台我不需要使用 RKAttributeMapping。我更喜欢这里一致的api(RKAttributeMapping)。但无论如何,Restkit 是避免手动解析/映射的好工具。
Wain 的这个解决方案很有用
RestKit: mapping JSON array of strings
【讨论】:
以上是关于使用 RESTkit 从 JSON 映射来自 NSManagedObject 的 NSString 数组的主要内容,如果未能解决你的问题,请参考以下文章
RestKit“无法在没有数据源的情况下执行关系映射”来自本地 JSON 文件