我并不真正理解弱指针和强指针之间的区别。到目前为止,它不会造成任何大问题,我正在按照文档中的示例进行操作,制作 NSURLRequest 并在 NSURLConnection 中使用它来接收数据。
代码是这样的:
//create the request with url
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/students.json"]];
//create the with the request
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
//create NSData instance to hold data if connection is successfull
self.receivedData = [[NSMutableData alloc]init];
// NSLog(@"%@",@"Connection successfully");
}
else{
NSLog(@"%@",@"Connection failed");
}
所以我将数据附加到receivedData
委托方法的主体中。
@property (strong,nonatomic) NSMutableData *receivedData;
//delegate method
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receivedData appendData:data];
NSLog(@"%@",@"Receiving Data");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Succeeded! Received %d bytes of data",[self.receivedData length]);
}
我上面发布的代码正在运行:) 因为我刚刚修复了它。
我的问题是- 指针的类型是原始的weak
。[self.receivedData length]
在将指针类型从weak
to更改为之前,我总是会得到 0 个字节的数据,strong
我不明白为什么它不能保存数据。