gpt4 book ai didi

iphone - 如何将文件保存到文档文件夹中?

转载 作者:行者123 更新时间:2023-12-03 19:02:39 25 4
gpt4 key购买 nike

我正在尝试将数据保存在 iPhone 5.1 模拟器上的文档文件夹中。

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"myData.json"];

if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath]) {
NSLog(@"Writable");
}else {
NSLog(@"Not Writable");
}

我总是“不可写”。任何想法?请帮助我。

最佳答案

可能是因为您还没有创建该文件,所以您测试的文件不存在。 :)

您可以这样做来查找问题,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"myData.json"];
NSLog(@"filePath %@", filePath);

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { // if file is not exist, create it.
NSString *helloStr = @"hello world";
NSError *error;
[helloStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}

if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath]) {
NSLog(@"Writable");
}else {
NSLog(@"Not Writable");
}

关于iphone - 如何将文件保存到文档文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11053842/

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