1

我正在尝试从响应中获取数据。
我正在使用NSURLConnectionDelegate, NSURLConnectionDataDelegate
该项目使用ARC。

@interface MainMenu()

@property (nonatomic, unsafe_unretained) NSMutableData* wpData;

@end 


@implementation

-(void)sendRequest{

    NSURL* url = [[NSURL alloc] initWithString:@"http://smthing"];

    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];


    NSString* reqBody = @"Block";

    NSData* reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding];

    [request setURL:url];
    [request setHTTPBody:reqData];
    [request setHTTPMethod:@"POST"];

    self.wpData = [NSMutableData data];
    NSURLConnection* conection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [conection start];
}

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

//Here iI have [__NSMallocBlock__ appendData:]: unrecognized selector sent to instance 0x95f3fb0
        [self.wpData setLength:0];


}

@end

也许你能发现我的错误

谢谢 :)

4

1 回答 1

7

您的数据指针是一个unsafe_unretained属性,

@property (nonatomic, unsafe_unretained) NSMutableData* wpData;

并且您正在为其分配一个自动释放的实例,

self.wpData = [NSMutableData data]; //Returns autoreleased object

由于您正在发出异步下载请求,因此您需要维护数据对象。你永远不知道什么时候自动释放池会被刷新,而未保留的对象会超出范围。在这种情况下,您应该retain使用自动释放的对象。更改属性strong并分配数据对象,

@property (nonatomic, strong) NSMutableData* wpData;

//...

self.wpData = [[NSMutableData alloc] init]; //Better practice

希望有帮助!

于 2013-09-11T13:45:16.460 回答