0

だから私はクラスを取得しました:
DataFetcher.h

#import <Foundation/Foundation.h>

@interface DataFetcher : NSObject 
{
    NSURLConnection *networkConnection;
    NSMutableData *recievedData;
    BOOL isFetchingFinished;
}

@property (nonatomic, retain) NSMutableData *recievedData;
@property (nonatomic, retain) NSURLConnection *networkConnection;

- (void)fetchDataWithRequest:(NSMutableURLRequest *)request;

DataFetcher.m:

#import "DataFetcher.h"

@implementation DataFetcher

@synthesize recievedData;
@synthesize networkConnection;

- (id)init
{
    self = [super init];
    if (self) {
        NSMutableData *data = [[NSMutableData alloc] initWithLength:0];
        self.recievedData = data;
        [data release];
    }

    return self;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection Error:%@\n Failure Reason:%@", [error localizedDescription], [error localizedFailureReason]);
    [networkConnection release];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.recievedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [networkConnection release];
    NSLog(@"%@", self.recievedData.length);
    [self processData];
}

- (void)fetchDataWithRequest:(NSMutableURLRequest *)request
{
    NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    self.networkConnection = aConnection;
    [aConnection release];
    [self.networkConnection start];
    if( !self.networkConnection )
    {
        NSLog(@"Connection failed! recieved nil at alloc point");
    }
}

- (void)dealloc
{
    [super dealloc];
    [recievedData release];
}

@end



通常、EXC_BAD_ACCESS のアカウントでクラッシュします (NSZombie は何も検出しません)。connectionDidFinishLoadingに切り替えると、クラッシュせず、そこにデータがあるように見えることself.recievedData.lengthに気付きました。self.recievedData私が得たのと同じことself.networkConnection-印刷するだけself.networkConnectionなら大丈夫ですが、印刷するとself.networkConnection.retainCount(たとえば、ivarが機能します)、同じ信号でクラッシュします。

問題がどこにあるのか把握できないようで、しばらく静かに探していました。これについての助けに感謝します:)
Tnx!

ああ、私はこのコードでそれを呼び出します:
DataFetcher *fetcher = [[DataFetcher alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]]; [fetcher fetchDataWithRequest:request]; [fetcher release]; [request release];

4

1 に答える 1

0
NSLog(@"%@", self.recievedData.length);

ここで、%@形式指定子は、objective-cオブジェクトをログに記録する一方で、実際にはプレーン整数を渡すことを意味します。これにより、アプリがクラッシュします。ログをに変更します

 NSLog(@"%d", self.recievedData.length); 
于 2011-07-27T19:26:52.940 に答える