【发布时间】:2015-12-13 20:47:53
【问题描述】:
我正在为 iOS 应用程序做一个商店部分,它有两个页面(第一页显示商店中的语音包列表,第二页显示语音包内的语音详细信息)。
当单击voicePackList 中的任何单元格时,转到下一页,并且在下一页中存在一个名称为: DOWNLOAD 的按钮,当我单击该按钮时,声音下载并保存在文件夹中。这是我在按钮按下处理中制作的代码:
- (void)downloadingVoice {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Starting Download ...");
NSString *downloadUrl = @"10.3.1.228:9000/files";
NSURL *url = [NSURL URLWithString:downloadUrl];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if (urlData) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *voiceDirectory = [paths objectAtIndex:0];
NSString *voicePaths = [NSString stringWithFormat:@"%@%@", voiceDirectory,@"voice.mp3"];
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:voicePaths atomically:YES];
NSLog(@"Saved voice files");
});
}
});
}
- (void)btnDownloadClicked:(id)sender {
NSLog(@"Downloading Voice . . .");
[self downloadingVoice];
}
这是我将按钮放在声音列表下方的方式:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 60.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
// if(tableView == self.shopTableView) {
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
UIButton *download = [UIButton buttonWithType:UIButtonTypeCustom];
[download setTitle:@"Download" forState:UIControlStateNormal];
[download addTarget:self action:@selector(btnDownloadClicked:) forControlEvents:UIControlEventTouchUpInside];
[download setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
download.frame = CGRectMake(0, 0, 130, 30);
[footerView addSubview:download];
return footerView;
}
当我单击按钮并放置一些断点时,当我检查 urlData 和 downloadUrl 时,它在 if (urlData) 之后结束,它说:
2015-09-17 10:53:01.926 Selfie[87197:674517] Starting Download ...
(lldb) po urlData
error: Couldn't materialize: couldn't get the value of variable urlData: no location, value may have been optimized out
Errored out in Execute, couldn't PrepareToExecuteJITExpression
(lldb) po downloadUrl
error: Couldn't materialize: couldn't get the value of variable downloadUrl: variable not available
Errored out in Execute, couldn't PrepareToExecuteJITExpression
任何人请帮我解决这个问题..我会非常感谢你的帮助..
【问题讨论】:
标签: ios objective-c json nsdata nsurl