Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在NSURLConnection中委托并声明变量receivedData_Ios_Objective C_Nsurlconnection - Fatal编程技术网

Ios 在NSURLConnection中委托并声明变量receivedData

Ios 在NSURLConnection中委托并声明变量receivedData,ios,objective-c,nsurlconnection,Ios,Objective C,Nsurlconnection,我想在iOS版的Xcode 4.5.2中创建一个NSURLConnection委托,因为。现在,我将以下代码(直接从文档中获取)放入AppDelegate.m中的方法application didfishlaunchingwithoptions: // Create the request. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]

我想在iOS版的Xcode 4.5.2中创建一个NSURLConnection委托,因为。现在,我将以下代码(直接从文档中获取)放入AppDelegate.m中的方法
application didfishlaunchingwithoptions:

// 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 hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
}
如何在AppDelegate.h中创建委托?
在何处以及如何声明变量
receivedData

您不应该在AppDelegate中执行此操作,但为了使其正常工作,以下是您需要执行的操作

1) 在AppDelegate.h中,将接口声明替换为:

@interface AppDelegate : UIResponder <UIApplicationDelegate, NSURLConnectionDataDelegate> {
    NSMutableData *receivedData;
}  

不要这样做。这很糟糕。您应该创建一个新对象,该对象拥有NSURLConnection并且是委托等。。。您不应该在AppDelegate中执行此类操作。您还应该实现
ConnectionDiFinishLoading:
连接:didFailWithError:
,否则您不知道下载何时完成。。。。它应该是
[receivedData appendData:data]
,或者如果数据是成片的,您会遇到问题。我可以在我的ViewController中执行此操作,并由用户在文本字段中输入URL触发,这样地址就不是apple.com了吗?viewcontroller.h中已经有
@interface-BSViewController:UIViewController{IBOutlet-UIButton*chooseImage;
。我建议使用上述方法“只是为了让它工作”,他可能根本不应该在AppDelegate中这样做,只需添加到现有方法中:@interface-BSViewController:UIViewController
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData setData:data];
    NSLog(@"receivedData : %@", receivedData);
}