NSURLConnection下载文件在另一个类中

|| 我可以使用NSURLConnection设法下载一些内容,但是我想使用另一个类返回NSURLConnection结果(NSData)。我无法解决。 可以说我有这个班:
@interface testData : NSData
@end
我已经实现了NSURLConnection,该过程可以下载,也可以在connectionDidFinishLoading触发时看到数据。但我无法将数据返回到我的主班。我希望我能说出我的需要。谢谢。
@implementation testData

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   //doSomething
}

@end
    
已邀请:
        保存对委托(您要将数据发送到的对象)的引用,然后在该委托中创建一个接受NSData *对象的方法。 testData.h
@interface testData : NSData
    TestDataDelegate * delegate;
    NSMutableData * receivedData;

@end
testData.m @实现testData
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   [delegate doSomethingWithData:receivedData];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //be sure to alloc/init receivedData when you create the connection.
    [receivedData appendData:data];
}

@end
TestDataDelegate.h
@interface TestDataDelegate : NSData
@end
-(void)doSomethingWithData:(NSData *)data;
TestDataDelegate.m
-(void)doSomethingWithData:(NSData *)data
{
 //dosomethingwith data
}
    

要回复问题请先登录注册