我有這樣的功能,將通過一個請求操作得到的xml:返回字符串
-(id)xmlRequest:(NSString *)xmlurl
{
AFKissXMLRequestOperation *operation = [AFKissXMLRequestOperation XMLDocumentRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:xmlurl]] success:^(NSURLRequest *request, NSHTTPURLResponse *response, DDXMLDocument *XMLDocument) {
NSLog(@"XMLDocument: %@", XMLDocument);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, DDXMLDocument *XMLDocument) {
NSLog(@"Failure!");
}];
[operation start];
return operation;
}
這是我的代碼調用該函數:
Request *http=[[Request alloc] init];
NSString *data=[http xmlRequest:@"http://legalindexes.indoff.com/sitemap.xml"];
NSError *error;
DDXMLDocument *ddDoc=[[DDXMLDocument alloc] initWithXMLString:data options:0 error:&error];
NSArray *xmlItems=[ddDoc nodesForXPath:@"//url" error:&error];
NSMutableArray *returnArray = [[NSMutableArray alloc] initWithCapacity:[xmlItems count]];
for(DDXMLElement* itemElement in xmlItems){
DDXMLElement *element = [[itemElement nodesForXPath:@"loc" error:&error] objectAtIndex:0];
NSLog(@"valueasstring %@", element);
[returnArray addObject:element];
}
我需要的xmlRequest到返回一個字符串,所以我可以得到的XML,但[操作開始]創建正確的輸出,但我不能把它放在一個字符串。我怎樣才能將輸出導入字符串?
你試過'返回[操作開始]'? – 2012-07-10 19:22:38
(我不知道AFKissXMLRequestOperation是什麼,或者它的'operation'方法。) – 2012-07-10 19:23:51
好吧,它是NSOperation的子類。所以你開始一個NSOperation,並且沒有辦法同步獲得異步操作的結果。您需要監視完成的操作,然後在此例程之外獲取結果。 – 2012-07-10 19:27:14