在下面的方法中,我在包含"urlString“变量的行上收到了"EXC_BAD_ACCESS”。我的研究表明,当程序向已经释放的变量发送消息时,就会发生此错误。但是,由于我使用的是ARC,所以我不会手动释放内存。如何防止ARC过早释放此变量?
-(NSMutableArray *)fetchImages:(NSInteger *)count {
//prepare URL request
NSString *urlString = [NSString stringWithFormat:@"http://foo.example.com/image?quantity=%@", count];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
//Perform request and get JSON as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//Parse the retrieved JSON to an NSArray
NSError *jsonParsingError = nil;
NSArray *imageFileData = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];
//Create an Array to store image names
NSMutableArray *imageFileNameArray;
//Iterate through the data
for(int i=0; i<[imageFileData count];i++)
{
[imageFileNameArray addObject:[imageFileData objectAtIndex:i]];
}
return imageFileNameArray;
}发布于 2012-08-05 16:06:32
您可能还想分配和初始化
NSMutableArray *imageFileNameArray;在添加对象到它之前,否则你会一直崩溃。所以你就会有
//Create an Array to store image names
NSMutableArray *imageFileNameArray = [[NSMutableArray alloc] init];https://stackoverflow.com/questions/11813901
复制相似问题