相同的代码显示XCode中的不同行为

时间:2014-02-13 11:26:00

标签: ios nsurl nsurlrequest

-(IBAction)onButtonClick:(id)sender
{
    DowloadFilesManager* downManager1=[[DowloadFilesManager alloc] init];
    DowloadFilesManager* downManager2=[[DowloadFilesManager alloc] init];
    DowloadFilesManager* downManager3=[[DowloadFilesManager alloc] init];
    [downManager1 downloadURL:@"http://localhost/banners/banner1.jpg" destPath:@"/Users/varunisac/Desktop/samples/godisgreat.jpg"];
    [downManager2 downloadURL:@"http://localhost/banners/banner1.jpg" destPath:@"/Users/varunisac/Desktop/samples/godisgreat1.jpg"];
    [downManager3 downloadURL:@"http://localhost/banners/banner1.jpg" destPath:@"/Users/varunisac/Desktop/samples/godisgreat2.jpg"];
    NSLog(@"Finished Succesfully");
}  

1)以上代码的作品是完美的 2)在触发以下事件函数后下载jpgs。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    [receivedData writeToFile:toFile  atomically:YES];
    theConnection = nil;
    receivedData = nil;    
}

但是在导入“DowloadFilesManager.h”和“DowloadFilesManager.m”之后尝试使用另一个程序时它不会触发任何事件方法,该程序在同一台Mac机器上的同一个XCode上运行且服务器URL可以访问。任何人都可以提出解决方案?我错过了什么吗?我试过Clean等......但是没有用。以下是我使用的DowloadFilesManager类:

#import "DowloadFilesManager.h"
@implementation DowloadFilesManager
@synthesize toFile;
@synthesize toURL;
@synthesize  theConnection;
-(void) downloadURL:(NSString *) urlStr destPath:(NSString *) destPath
{
    toURL=urlStr;
    toFile=destPath;
    // Create the request.
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:toURL]
    cachePolicy:NSURLRequestUseProtocolCachePolicy
    timeoutInterval:60.0];
    receivedData = [[NSMutableData alloc] init];
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    // [receivedData dataWithCapacity: 0];
    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (!theConnection) {
    // Release the receivedData object.
    receivedData = nil;
     // Inform the user that the connection failed.
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to receivedData.
    // receivedData is an instance variable declared elsewhere.
    [receivedData appendData:data];  
}
- (void)connection:(NSURLConnection *)connection
    didFailWithError:(NSError *)error
    {
    theConnection = nil;
    receivedData = nil;
    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
    [error localizedDescription],
    [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    [receivedData writeToFile:toFile  atomically:YES];
    theConnection = nil;
    receivedData = nil;   
}
@end

1 个答案:

答案 0 :(得分:0)

- (void)downloadURL:destPath:内,您创建了一个名为theConnection的本地变量:

NSURLConnection *theConnection = [...];

当方法结束时,这将超出范围并被销毁。

使用您的属性来保存连接对象,您需要以下内容:

self.theConnection = [...];

此外,更好的信号失败方法是使该方法返回BOOL并使用此语句:

return self.theConnection != nil;