gpt4 book ai didi

ios - NSURLConnection与ARC不接收数据

转载 作者:行者123 更新时间:2023-12-01 17:10:12 27 4
gpt4 key购买 nike

我是iOS开发的新手,我正在尝试从URL获取内容。我正在使用使用NSURLConnection的Apple教程,一切正常,但没有获取数据。我环顾四周,找不到解决我问题的答案。我也在我的项目中使用ARC,也许这会导致问题?

这是我正在使用的代码,从头文件开始:

@interface ViewController : UIViewController
@property (nonatomic, retain) NSMutableData *receivedData;
@end

实施文件:
@implementation ViewController

@synthesize receivedData;

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"1. viewDidLoad");
// Create the request
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
// Create the connection with the request and start loading the data
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
NSLog(@"2. connection succeeded");
receivedData = [NSMutableData data];
} else {
NSLog(@"2. connection failed");
}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"3. didReceiveResponse");
[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"4. Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"4. Succeeded! Received %d bytes of data", [receivedData length]);
}

所有的NSLog都在工作,但是一直说ReceivedData有0个字节。我找不到我的问题的答案,所以我希望我能找到这个问题的答案。

最佳答案

我已经使用ARC翻译了URL Loading System Programming Guide和NSURLConnection的部分。

//
// MyConnection.h
//

#import <Foundation/Foundation.h>

@interface MyConnection : NSObject

- (void)start;

@property NSMutableData *receivedData;

@end


//
// MyConnection.m
//

#import "MyConnection.h"

@implementation MyConnection

- (void)start {

// Create the request
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

// Create the connection with the request and start loading the data
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (theConnection) {
// Create the NSMUtableData to fold the received data
// ReceivedData is an instance variable declared elsewhere
_receivedData = [NSMutableData data];

}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Inform the user
NSLog(@"Connectionfailed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Do something with the data
NSLog(@"Succeeded! Recevied %d bytes of data", [_receivedData length]);

}

@end

关于ios - NSURLConnection与ARC不接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10086241/

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