我对象之间的通信有问题。我得到api json字符串,并在restkit中分成对象。有时我会得到绑物,有时候不是。我有一个对象专题,它是对象文章的一部分,有时我得到专题,有时不是。
Articles.h
#import "Article.h"
NS_ASSUME_NONNULL_BEGIN
@interface Article (CoreDataProperties)
@property (nullable, nonatomic, retain) NSNumber *count;
@property (nullable, nonatomic, retain) NSNumber *dateCreate;
@property (nullable, nonatomic, retain) NSNumber *dateJournal;
@property (nullable, nonatomic, retain) NSString *imagePath;
@property (nullable, nonatomic, retain) NSNumber *likes;
@property (nullable, nonatomic, retain) NSNumber *localTop;
@property (nullable, nonatomic, retain) NSNumber *objectId;
@property (nullable, nonatomic, retain) NSNumber *pressed;
@property (nullable, nonatomic, retain) NSString *subtitle;
@property (nullable, nonatomic, retain) NSString *text;
@property (nullable, nonatomic, retain) NSString *title;
@property (nullable, nonatomic, retain) NSNumber *top;
@property (nullable, nonatomic, retain) NSNumber *type_cell;
@property (nullable, nonatomic, retain) NSString *url;
@property (nullable, nonatomic, retain) NSSet<Author *> *authors;
@property (nullable, nonatomic, retain) NSSet<Thematic *> *thematics;
@end
@interface Article (CoreDataGeneratedAccessors)
- (void)addAuthorsObject:(Author *)value;
- (void)removeAuthorsObject:(Author *)value;
- (void)addAuthors:(NSSet<Author *> *)values;
- (void)removeAuthors:(NSSet<Author *> *)values;
- (void)addThematicsObject:(Thematic *)value;
- (void)removeThematicsObject:(Thematic *)value;
- (void)addThematics:(NSSet<Thematic *> *)values;
- (void)removeThematics:(NSSet<Thematic *> *)values;
@end
NS_ASSUME_NONNULL_END
Thematic.h
#import "Thematic.h"
NS_ASSUME_NONNULL_BEGIN
@interface Thematic (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *name;
@property (nullable, nonatomic, retain) NSNumber *objectId;
@end
NS_ASSUME_NONNULL_END
MappingProvider.h
+(RKMapping *) articlesMapping {
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Article" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[mapping addAttributeMappingsFromDictionary:@{ @"id":@"objectId", @"title":@"title", @"text":@"text", @"date_create":@"dateCreate",@"likes":@"likes",@"avatar":@"imagePath", @"pressed":@"pressed", @"subtitle":@"subtitle",@"url":@"url",@"count":@"count",@"top":@"top",@"local_top":@"localTop"}];
mapping.identificationAttributes = @[@"objectId"];
[mapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"thematics"
toKeyPath:@"thematics"
withMapping:[self thematicsMapping]]
];
[mapping addPropertyMapping:
[RKRelationshipMapping relationshipMappingFromKeyPath:@"authors"
toKeyPath:@"authors"
withMapping:[self authorsMapping]]
];
return mapping;
}
+ (RKMapping *) thematicsMapping {
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Thematic" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[mapping addAttributeMappingsFromDictionary:@{@"id":@"objectId", @"name": @"name" }];
mapping.identificationAttributes = @[@"objectId"];
return mapping;
}
我在网络上获得的主题这一事实,但是restkit编写了thematics关系错误。
JSON示例
{
"id": 104,
"title": "test message",
"url": "test message",
"subtitle": "test message",
"top": 1,
"status": 1,
"thematics": [
{
"id": 4,
"name": "photo"
}]
}