gpt4 book ai didi

iphone - NSMutableData 保存到文件

转载 作者:行者123 更新时间:2023-12-03 19:05:23 26 4
gpt4 key购买 nike

我使用下面所示的代码下载了该文件。然后我尝试将 NSMutableData 变量保存到文件中,但是,该文件未创建。我究竟做错了什么?我需要将 NSMutableData 转换为 NSString 吗?

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)_response {
response = [_response retain];
if([response expectedContentLength] < 1) {
data = [[NSMutableData alloc] init];
}
else {
data = [[NSMutableData dataWithCapacity:[response expectedContentLength]] retain];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {
[data appendData:_data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];

NSLog(@"saved: %@", filePath);
[data writeToFile:filePath atomically:YES];
NSLog(@"downloaded file: %@", data); //all i see in log is some encoded data here
}

最佳答案

您无法在应用程序包内写入内容。您需要将其保存在其他位置,例如应用程序的文档目录:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"file.txt"];
[data writeToFile:filePath atomically:YES];

关于iphone - NSMutableData 保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9490072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com