我是Objective C和XML的新手,所以这看起来非常粗糙。
我的代码如下:
#import "dbCommunicator.h"
#import "BookInfo.h"
#import "TouchXML.h"
@implementation dbCommunicator
-(void)getNextBooks {
if(self.bookListing == nil) {
for(int i = 0; i < 5; i++) {
BookInfo *newBook = [[BookInfo alloc] init];
[self.bookListing addObject:newBook];
}
self.currentPage = 0;
}
if(self.currentPage == 10) {
self.currentPage = 0;
}
NSString *test = @"<Authors><Book.Author><Id>1026</Id><Name>Mark Twain</Name></Book.Author></Authors>";
NSString *bookQueryString = [NSString stringWithFormat:@"http://bookworm.azurewebsites.net/api/book/list/5/%d",_currentPage];
NSURL *bookQueryURL = [NSURL URLWithString: bookQueryString];
self.currentPage++;
NSError *theError = NULL;
NSDictionary *mappings = [NSDictionary dictionaryWithObjectsAndKeys:
@"http://schemas.datacontract.org/2004/07/Bookworm.Models.ResponseModels",
@"datacontract",
nil];
CXMLDocument *xmlReturn = [[CXMLDocument alloc] initWithXMLString:test options:0 error:&theError];
NSArray *returnedBooks = [xmlReturn nodesForXPath:@"//Authors" error:&theError];
for(CXMLElement *resultElement in returnedBooks) {
NSLog(@"%s", "We actually got here");
}
}
目前那里有很多垃圾代码。目的是从数据库中提取XML文件并将其信息放入BookInfo类的数组中。目前,我通过使用测试XMLstring来简化,以确保它不是数据库发送给我的问题。这使得字典(处理TouchXML的命名空间问题)变得多余。反正。
此行总是因无法识别的选择器错误而崩溃:
[theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];
在此背景下:
if (xmlXPathNodeSetIsEmpty(theXPathObject->nodesetval))
theResult = [NSArray array]; // TODO better to return NULL?
else {
NSMutableArray *theArray = [NSMutableArray array];
int N;
for (N = 0; N < theXPathObject->nodesetval->nodeNr; N++) {
xmlNodePtr theNode = theXPathObject->nodesetval->nodeTab[N];
[theArray addObject:[CXMLNode nodeWithLibXMLNode:theNode freeOnDealloc:NO]];
}
}
与此同时,我完全不知所措。我已经尝试了很多东西,搜索每个StackOverflow帖子甚至密切相关并尝试修复,没有任何工作。有什么建议吗?
答案 0 :(得分:0)
array
是NSArray上的一个静态方法,它是一个返回NSArray对象的超类NSMutableArray。 NSArray不响应方法addObject:
请改为NSMutableArray *theArray = [NSMutableArray new];
答案 1 :(得分:0)
问题可能出在这个选择器上:
nodeWithLibXMLNode:freeOnDealloc:
如果你试图在控制台中看到你的崩溃日志,应该写入选择器无法识别,应该是这个。如果是,请检查CXMLNode
的类引用。
我现在检查了,该方法不存在。只是一种方法可能对您有用,即:
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;
享受;)