RestKit XML 映射 - Objective C

RestKit XML Mapping - Objective C

我一直在尝试将天气映射到 xml。 xml 看起来像这样

 <current_observation>
    <observation_epoch>1433740800</observation_epoch>
    <weather>Clear</weather>
    <temp_c>24</temp_c>
    <relative_humidity>61%</relative_humidity>
    <wind_dir>North</wind_dir>
    <wind_mph>0</wind_mph>
    <visibility_km>N/A</visibility_km>
  </current_observation>

我的weather.h

@interface Weather : NSObject

@property (nonatomic , copy) NSString* weather;
@property (nonatomic , copy) NSString* temp_c;
@property (nonatomic , copy) NSString* relative_humidity;
@property (nonatomic , copy) NSString* wind_dir;
@property (nonatomic , copy) NSString* wind_mph;
@property (nonatomic , copy) NSString* visibility_km;
@property (nonatomic , copy) NSString* observation_epoch;

@end

我的映射函数

- (RKObjectManager*) makeWeatherXMLMappingwithURL:(NSString*)mLinkURL{

    //Map the Weather class
    RKObjectMapping* weatherMapping = [RKObjectMapping mappingForClass:[Weather class]];
    [weatherMapping addAttributeMappingsFromDictionary:@{
                                                         @"weather.text":@"weather",
                                                         @"temp_c.text":@"temp_c",
                                                         @"relative_humidity.text":@"relative_humidity",
                                                         @"wind_dir.text":@"wind_dir",
                                                         @"wind_mph.text":@"wind_mph",
                                                         @"visibility_km.text":@"visibility_km",
                                                         @"observation_epoch.text":@"observation_epoch"
                                                           }];

    //register mappings with the provider using a response descriptor
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping: weatherMapping
                                                                                            method: RKRequestMethodAny
                                                                                       pathPattern: nil
                                                                                           keyPath: @"current_observation"
                                                                                        //keyPath:@"rss.channel.item"
                                                                                       statusCodes: RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    //Initialize RestKit for xml (rss/feed) parsing & mapping
    NSURL *baseURL = [NSURL URLWithString: mLinkURL];
    //make a new instance of RKObjectManager (Parser which inherits from RKObjectManager)
    RKObjectManager *objectManager = [Parser managerWithBaseURL:baseURL];
    [objectManager setRequestSerializationMIMEType:RKMIMETypeTextXML];
//    [objectManager setAcceptHeaderWithMIMEType:@"application/rss+xml"];
    [objectManager setAcceptHeaderWithMIMEType:@"text/xml"];
//    [RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"application/rss+xml"];
    [RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"text/xml"];

    //add the responseDescriptor to RKObjectManager
    [objectManager addResponseDescriptor:responseDescriptor];


    return objectManager;
}

我的电话

- (void) parseWeatherXMLwithURL:(NSString*)mLinkURL{

    //Make the XML Mapping
    RKObjectManager* objectManager = [self makeWeatherXMLMappingwithURL:mLinkURL];

    //asychronous mapping (Calling getObjectsAtPath doesn't block the thread until it has completed)
    [objectManager getObjectsAtPath:@""
                         parameters:nil
     //Asynchronous Success block
                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                                NSArray *weather = mappingResult.array;
                                if (weather!=nil && [weather count]>0){
                                    //Delegate response to processAsynchronousLiveStreamingRSSComplete Handler
                                    [self.delegate performSelector:@selector(processAsynchronousXMLWeatherComplete:) withObject:(Item*)[weather objectAtIndex:0]];
                                }
                                else{
                                    NSMutableDictionary* details = [NSMutableDictionary dictionary];
                                    [details setValue:@"Data is not available!" forKey:NSLocalizedDescriptionKey];
                                    NSError *error = [NSError errorWithDomain:@"Data is not available!" code:200 userInfo:details];
                                    //Delegate response to processAsynchronousLiveStreamingRSSFailed Handler
                                    [self.delegate performSelector:@selector(processAsynchronousXMLWeatherFailed:) withObject:error];
                                }
                            }
     //Asynchronous Failed block
                            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                //Delegate response to processAsynchronousLiveStreamingRSSFailed Handler
                                [self.delegate performSelector:@selector(processAsynchronousXMLWeatherFailed:) withObject:error];
                            }];
}

调用后我得到 1 个键/值对,即@"current_observation":@“0 个对象”。我认为这意味着它映射到根元素 - current-observation - 但它不能映射到其余元素。

终于找到解决办法了。我不得不将我的 class "weather" 重命名为 "WeatherCO" - 可以重命名为任何东西 - 清理并重建我的项目,一切都已修复!