gpt4 book ai didi

ios - 使用sendAsynchronousRequest :queue:completionHandler: for URL request

转载 作者:行者123 更新时间:2023-11-29 02:43:33 25 4
gpt4 key购买 nike

我正在更新另一位开发者创建的 iOS 应用程序。他使用 ASIHTTPRequest 来处理 http 请求。但是,我必须使用的应用程序版本崩溃了。由于 ASIHTTPRequest 不再更新,我当时想我应该改用 AFNetworking,但它太复杂了我无法弄清楚。

所以最后我想我可以单独使用 NSURLConnection。

这是我要替换的原始代码:

-(NSArray*)readUTF16LEFeed:(NSURL*) urlToRead{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:urlToRead];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
lastModified = [NSDate date];
NSData *response = [request responseData];//UTF-16LE
NSString* responseString = [[NSString alloc] initWithData:response encoding:NSUTF16LittleEndianStringEncoding];
DLog(@"string is: %@",responseString);
responseString = [responseString stringByReplacingOccurrencesOfString:@"ISO-8859-1" withString:@"UTF16-LE"];
NSData* data = [responseString dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
return [self parseNamesFromXML:data];
}
return nil;
}

下面是我尝试使用的:

-(NSArray*)readUTF16LEFeed:(NSURL*) urlToRead{

NSURLRequest *request = [NSURLRequest requestWithURL:urlToRead];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error ) {

if ([data length] >0 && error == nil)
{
// DO YOUR WORK HERE
lastModified = [NSDate date];

// NSData *response = [request responseData];//UTF-16LE
NSString* responseString = [[NSString alloc] initWithData:data encoding: NSUTF16LittleEndianStringEncoding];
DLog(@"string is: %@",responseString);
responseString = [responseString stringByReplacingOccurrencesOfString:@"ISO-8859-1" withString:@"UTF16-LE"];
NSData* data = [responseString dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
return [self parseNamesFromXML:data];
}

else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];
return nil;
}

但是,我收到错误消息“不兼容的 block 指针类型将 NSArray 发送到类型为 void 的参数。而且“控件可能到达非 void block 的末尾。”

我怎样才能让它工作?

最佳答案

将您的方法的返回类型设置为 void。不要退回任何东西。只需调用 [self parseNamesFromXML:data];方法。

-(void)readUTF16LEFeed:(NSURL*) urlToRead{


NSURLRequest *request = [NSURLRequest requestWithURL:urlToRead];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error ) {

if ([data length] >0 && error == nil)
{

// DO YOUR WORK HERE
lastModified = [NSDate date];

// NSData *response = [request responseData];//UTF-16LE
NSString* responseString = [[NSString alloc] initWithData:data encoding: NSUTF16LittleEndianStringEncoding];
DLog(@"string is: %@",responseString);
responseString = [responseString stringByReplacingOccurrencesOfString:@"ISO-8859-1" withString:@"UTF16-LE"];
NSData* data = [responseString dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
[self parseNamesFromXML:data];
}


else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}

}];
}

parseNamesFromXML 方法中,处理您的结果并将结果数组分配给一个属性。您可以随时随地访问它。

-(void) parseNamesFromXML:(NSData *)xmlData
{
NSError *error;
//DLog(@"data is %@", [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding ]);

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];

if (doc)
{
self.dataArray = [self parseXmlDoc:doc];
}
}

关于ios - 使用sendAsynchronousRequest :queue:completionHandler: for URL request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25422301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com