我使用RestKit從資源加載XML數據。這工作完美,但現在我想改變我的整個程序來讀取本地XML文件。我發現讀本地XML文件,並在谷歌RestKit組解析它通過RestKit可能性:使用本地XML的RestKit
NSString *string = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"testing" ofType:@"xml"] encoding:NSUTF8StringEncoding error:nil];
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeXML];
id result = [parser objectFromString:string error:nil];
NSLog(@"%@",[[[[result objectForKey:@"sample"] objectForKey:@"nested"] objectForKey:@"tags" ] description]);
但現在我希望加載這個對象與我的映射。我的舊代碼從服務器加載:
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKURL *URL = [RKURL URLWithBaseURL:[objectManager baseURL] resourcePath:[xmlFile path] queryParameters:nil];
[objectManager.mappingProvider setMapping:myMapping forKeyPath:myKeyPath];
[objectManager loadObjectsAtResourcePath:[NSString stringWithFormat:@"%@", [URL resourcePath]] usingBlock:^(RKObjectLoader *loader) {
loader.delegate = self;
}];
之後,我在RestKit的didLoadObjects委託中獲得了我的映射對象。我怎樣才能爲我的本地XML對象做同樣的事情?