我正在嘗試創建一個尚不存在的UIManagedDocument。這裏是我的代碼:UIManagedDocument saveToURL總是返回false
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。無論如何,我可以進一步調試,爲什麼會發生這種情況?
編輯:
好了,所以我不能寫入到該網址。我現在試着這樣做:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *url = [NSURL URLWithString:documentsDirectory];
NSLog(@"The URL is %@", [url absoluteString]);
當它運行時,日誌似乎返回URL爲空。還有什麼我做錯了嗎?
我已經更新我的回答,請使用我已經張貼 – Manu