我正試圖在我的應用程序中映射JSON數組。我面臨的問題是不是獲取NSString類型的對象,而是獲取對象鍵入RKMappingSourceObject。Restkit 0.2返回RKMappingSourceObject類型用於一對多關係的對象
響應的形式是
{
message = {
Appetite = {
Breakfast = 4;
Dinner = 4;
Lunch = 4;
};
PainSymptom = {
Aching = 1;
AtRest = 0;
Awakes = 0;
Burning = 0;
Cramping = 0;
Location = (
Head,
"Right Upper Leg",
"Left Upper Leg",
"Left Ankle/Foot",
"Right Ankle/Foot",
"Mid Back",
"Upper Back",
Tailbone
);
};
}
}
我的核心數據類是:
DTAttribute.h
@interface DTAttribute : NSManagedObject
@property (nonatomic, strong) NSString * attribute;
@property (nonatomic, strong) DTPainSymptom *painSymptom;
@end
DTAttribute.m
@implementation DTAttribute
@dynamic attribute;
@dynamic painSymptom;
@end
DTPainSymptom.h
@interface DTPainSymptom : NSManagedObject
@property (nonatomic, retain) NSNumber * aching;
@property (nonatomic, retain) NSNumber * atRest;
@property (nonatomic, retain) NSNumber * awakes;
@property (nonatomic, retain) NSNumber * burning;
@property (nonatomic, retain) NSNumber * cramping;
@property (nonatomic, retain) DTMyDay *myDay;
@property (nonatomic, retain) NSSet *painLocations;
@end
@interface DTPainSymptom (CoreDataGeneratedAccessors)
- (void)addPainLocationsObject:(DTAttribute *)value;
- (void)removePainLocationsObject:(DTAttribute *)value;
- (void)addPainLocations:(NSSet *)values;
- (void)removePainLocations:(NSSet *)values;
@end
DTPainSymptom.m
@implementation DTPainSymptom
@dynamic aching;
@dynamic atRest;
@dynamic awakes;
@dynamic burning;
@dynamic cramping;
@dynamic myDay;
@dynamic painLocations;
@end
DTMyDay.h
@interface DTMyDay : NSManagedObject
@property (nonatomic, retain) DTPainSymptom *painSymptom;
@end
DTMyDay.m
@implementation DTMyDay
@dynamic painSymptom;
@end
映射:
@implementation DTMyDay(映射)
+ (id) mapping {
RKEntityMapping* myDayMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([DTMyDay class]) inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
RKEntityMapping* attributeMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([DTAttribute class]) inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
[attributeMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"attribute"]];
//Pain Symptom
RKEntityMapping* painSymptomMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([DTPainSymptom class]) inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];
[painSymptomMapping addAttributeMappingsFromDictionary:@{
@"Awakes": @"awakes",
@"AtRest": @"atRest",
@"Burning": @"burning",
@"Cramping": @"cramping",
@"Aching": @"aching",
}];
[painSymptomMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Location"
toKeyPath:@"painLocations"
withMapping:attributeMapping]];
[myDayMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"message.PainSymptom"
toKeyPath:@"painSymptom"
withMapping:painSymptomMapping]];
NSDateFormatter* dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
myDayMapping.dateFormatters = [NSArray arrayWithObject: dateFormatter];
return myDayMapping;
}
+ (id)responseDescriptor:(NSString *)pathPattern {
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[self mapping] pathPattern:pathPattern keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
return responseDescriptor;
}
@end
這裏的問題是對應於message.PainSymptom.Location
值的陣列不正確映射。我在DTMyDay
類中獲得對應於painLocations
的DTAttribute類型對象的數組。但是,當我試圖訪問painLocations數組中的每個DTAttribute類的屬性屬性時,我得到RkMappingSourceObject
類型的對象而不是NSString
類型的對象。
我很抱歉代碼這個巨大的牆,但現在我真的不知道該怎麼做。
這使我一個問題,因爲我送dtAttribute.attribute一種方法,它返回我NSNotFound因爲傳遞的對象是類型RKMappingSourceObject的:
#define BODY_PARTS_NAMES @"Head", @"Right Shoulder", @"Neck", @"Left Shoulder", @"Right Chest", @"Left Chest", @"Right Arm", nil
- (BodyPart)bodyPartStringToEnum:(NSString *)strVal
{
NSArray *bodyPartsArray = [[NSArray alloc] initWithObjects:BODY_PARTS_NAMES];
NSUInteger index = [bodyPartsArray indexOfObject:strVal];
return index;
}
RKMappingSourceObject代理類會導致什麼問題?它只是將所有的方法調用轉發給真實的對象。你是否試圖在某處使用字符串描述(與你期望的相比,這看起來很奇怪)? – Wain
我有一個函數,它需要一個NSString參數並返回給我一些值。當我向它發送dtAttribute.attribute時,它會返回我NSNotFound,因爲傳遞的對象是RKMappingSourceObject類型。 – tek3
您可以將該方法的代碼添加到您的問題中,我們將着手修復它嗎? – Wain