2011-09-13 7 views
0

NueburgのPrograming ios 4で定義されたMyDownloaderクラスを実装しました。コードを実行すると、スーパーdeallocでexc_bad_accessが取得されます。この本はヘッダーファイルの説明を提供していないので、おそらく私はそこで何か間違ったことをしたでしょう。誰かが私のエラーを引き起こしている参照してください助けてもらえますか?ここでMyDownloader実装のsuper deallocのexc_bad_access

は私のヘッダファイルである:ここで

#import <Foundation/Foundation.h> 


@interface MyDownloader : NSURLConnection { 
    NSURLConnection *connection; 
    NSURLRequest *request; 
    NSMutableData *receivedData; 
} 

-(id) initWithRequest: (NSURLRequest*) req; 

@property (nonatomic, retain) NSURLConnection *connection; 
@property (nonatomic, retain) NSURLRequest  *request; 
@property (nonatomic, retain) NSMutableData  *receivedData; 

@end 

は、(すべての権利本の取り出しdidReceiveResponse、didReceiveData、didFailWithError、およびconnectionDidFinishLoadingのマイナス実装)私の実装です:

#import "MyDownloader.h" 


@implementation MyDownloader 

@synthesize connection; 
@synthesize request; 
@synthesize receivedData; 

-(id) initWithRequest: (NSURLRequest*) req { 
    self = [super init]; 
    if (self) { 
     self->request = [req copy]; 

     // Create a connection, but don't start it yet. The connection will be started with a start message. 
     self->connection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO]; 
     self->receivedData = [[NSMutableData alloc] init]; // initialize where the incoming data will be stored 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [receivedData release]; 
    [request release]; 
    [connection release]; 
    [super dealloc]; 
} 
... 
@end 

そして最後に、ここではクラスの私の使用である:

{ 
    ... 
    if (!self.connections) { 
    self.connections = [NSMutableArray array]; 
    } 
    NSString *s = @"https://www.myserver.com/myfile.txt"; 
    NSURL *url = [NSURL URLWithString:s]; 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 
    MyDownloader *d = [[MyDownloader alloc] initWithRequest:req]; 
    [self.connections addObject:d]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadFinished:) name:@"connectionFinished" object:d]; 
    [d.connection start]; 
    [d release]; 
    ... 
} 

-(void) downloadFinished: (NSNotification *) n 
{ 
    MyDownloader *d = [n object]; 
    NSData *data = nil; 
    if ([n userInfo]) { 
     NSLog(@"MyDownloader returned an error"); 
    } 
    else { 
     data = [d receivedData]; 
     NSLog(@"MyDownloader returned the requested data"); 
    } 
    [self.connections removeObject:d]; 
} 
+1

'NSURLConnection'をサブクラス化するには' MyDownloader'が必要ですか?まずNSObject'のサブクラスにしてみましょう –

+0

Joe、申し訳ありませんが、私は答えを受け入れるために何かをやろうとしていたことに気付きませんでした。私は戻って答えられたすべての回答を受け入れた。私を助けてくれたすべての人に申し訳なくて、私はあなたを失望させた。 – JeffB6688

+0

Baddidi、ありがとう。それがそれを解決しました。私が言ったように、本はヘッダファイルを与えなかったし、クラスがNSURLConnectionのサブクラスである必要があると誤って想定していました。 - (void)connection:(NSURLConnection *)connection didReceiveResponse :, - )connection:(NSURLConnection *)connection didReceiveData :,および - (void)connectionDidFinishLoading:を呼び出します。 NSURLConnectionのサブクラスではなく、これらのメソッドが「接続」にどのように認識されているか分かりません。 – JeffB6688

答えて

0

Baddidiは、NSURLConnectionではなくNSObjectとしてクラスをサブクラス化する必要があるという解決策を示しました。

関連する問題