gpt4 book ai didi

iphone - UIManagedDocument saveToURL 始终返回 false

转载 作者:行者123 更新时间:2023-11-29 03:50:53 24 4
gpt4 key购买 nike

我正在尝试创建一个尚不存在的 UIManagedDocument。这是我的代码:

url = [NSURL URLWithString:@"file://ProjectSSDB"];
document = [[UIManagedDocument alloc] initWithFileURL:url];

if ([[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
[document openWithCompletionHandler: ^(BOOL success) {
if (success) [ProjectSSViewController documentIsReady];
if (!success) NSLog(@"Couldn't open document at %@", url);
}];
} else {
[document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
NSLog(@"Returned %d", success);
if (success) [ProjectSSViewController documentIsReady];
if (!success) NSLog(@"Couldn't create document at %@", url);
}];
}

我的问题是该文件尚不存在,并且 saveToURL 操作似乎总是返回 false。无论如何,我可以进一步调试这个问题,以了解为什么会发生这种情况吗?

编辑:

好的,所以我无法写入该 URL。我现在尝试这样做:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSURL *url = [NSURL URLWithString:documentsDirectory];

NSLog(@"The URL is %@", [url absoluteString]);

运行时,日志似乎返回 URL 为空。我还做错了什么吗?

最佳答案

你不能在这个路径“file://ProjectSSDB”写入,你没有权限,你需要通过这种方式获取应用程序的根目录:

NSString* rootPath = NSHomeDirectory();

并将数据保存在 Apple file system guide line 指定的子文件夹之一中

NSString* fullPath = [rootPath stringByAppendingPathComponent:@"subFoldeder/file.extension"];

关于iphone - UIManagedDocument saveToURL 始终返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17082157/

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