我正在尝试从响应中获取数据
我正在使用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
也许你可以找到我的错误
谢谢:)
答案 0 :(得分: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
希望有所帮助!