我正在尝试创建一个尚不存在的 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 为空。还有什么我做错了吗?