Iphone 指向NSData的弱指针无法使用NSURLConnection读取数据?
我并不真正理解弱指针和强指针之间的区别。 直到现在,它还没有引起任何大的问题,我正在遵循文档中的一个示例,生成一个NSURLRequest,并在NSURLConnection中使用它来接收数据 代码如下所示:Iphone 指向NSData的弱指针无法使用NSURLConnection读取数据?,iphone,objective-c,ios5.1,Iphone,Objective C,Ios5.1,我并不真正理解弱指针和强指针之间的区别。 直到现在,它还没有引起任何大的问题,我正在遵循文档中的一个示例,生成一个NSURLRequest,并在NSURLConnection中使用它来接收数据 代码如下所示: //create the request with url NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/students.
//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(强,非原子)NSMutableData*接收数据代码>
//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]);
}
我上面发布的代码正在运行:),因为我刚刚修复了它
我的问题是-指针的类型是原始的弱
。在我将指针类型从弱更改为强之前,我总是从[self.receivedData length]
获取0字节的数据,我不明白为什么它不能保存数据。弱引用不能保存其内容。您告诉receivedData
变量查看您创建的可变数据对象,但不要保留它。因此,当if
块完成时,数据的作用域结束并被释放;由于没有其他东西持有它,所以它被解除分配,receivedData被设置为零,因为它不再有任何指向[self.receivedData length]
返回0(技术上为零),因为self.receivedData
为零。通过声明receivedData strong,您告诉它抓住它所指向的对象,确保它在处理完它之前一直在附近 弱引用无法保留其内容。您告诉receivedData
变量查看您创建的可变数据对象,但不要保留它。因此,当if
块完成时,数据的作用域结束并被释放;由于没有其他东西持有它,所以它被解除分配,receivedData被设置为零,因为它不再有任何指向[self.receivedData length]
返回0(技术上为零),因为self.receivedData
为零。通过声明receivedData strong,您告诉它抓住它所指向的对象,确保它在处理完它之前一直在附近 尝试使用它来获取数据
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:3000/students.json"]]];
NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *strResponse = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// NSLog(@"%@",strResponse);
SBJSON *sbJason = [[SBJSON alloc] init];
NSMutableDictionary *getNameList = [sbJason objectWithString:strResponse];
尝试使用它来获取数据
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:3000/students.json"]]];
NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *strResponse = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// NSLog(@"%@",strResponse);
SBJSON *sbJason = [[SBJSON alloc] init];
NSMutableDictionary *getNameList = [sbJason objectWithString:strResponse];
deligates不是这样称呼的。有什么原因吗?deligates不是这么叫的。有什么原因吗?