【问题标题】:Same code showing different behaviour in XCode相同的代码在 XCode 中显示不同的行为
【发布时间】:2014-02-13 11:26:00
【问题描述】:
-(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 可达。任何人都可以提出解决方案?我错过了什么吗?我试过清洁等......但没有用。以下是我使用的 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

【问题讨论】:

    标签: ios nsurl nsurlrequest


    【解决方案1】:

    - (void)downloadURL:destPath: 中,您创建了一个名为theConnection 的局部变量:

    NSURLConnection *theConnection = [...];
    

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

    您想要的是以下内容,使用您的属性来持久化连接对象:

    self.theConnection = [...];
    

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

    return self.theConnection != nil;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      相关资源
      最近更新 更多