1
爲什麼我們在xml解析中使用NSURLConnection對象,即。在調用xml web服務時使用NSURLConnection的目的..?
爲如:
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
and then NSURLConnection delegate methods invoked
like:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[connection release];
[receivedData release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if(ObjNSXMLParser!=nil && [ObjNSXMLParser retainCount]>0){
[ObjNSXMLParser release];
ObjNSXMLParser = nil;
}
ObjNSXMLParser = [[NSXMLParser alloc]initWithData:receivedData];
ObjNSXMLParser.delegate=self;
[ObjNSXMLParser parse];
}
=============================== 但是我們也有數據直接通過:無需初始化NSURLConnection的對象:
NSData *dt = [NSData DatawithcontentsofURL:urlname];
哪種方法是最好的,爲什麼?