我正在尝试从响应中获取数据。
我正在使用NSURLConnectionDelegateNSURLConnectionDataDelegate
该项目使用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

也许你会发现我的错误

谢谢 :)

最佳答案

您的数据指针是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

希望有帮助!

关于ios - NSMutableData-无法识别的选择器发送到实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18742758/

10-16 15:07