2014-09-30 212 views
0

我是新來編碼和嘗試構建我的第一個應用程序。在Swift Playground中創建UIManagedDocument - 信號SIGABRT錯誤

我嘗試使用Swift Playgrounds創建一個UIManagedDocument,遵循CS193 Standford iOS課程從去年開始的第12講中列出的步驟。它們在目標C中,我試圖將它們轉換爲Swift。我花了一整天敲我的頭撞在牆上試圖找到我在做什麼錯了:/

import UIKit 
import CoreData 


let fileManager = NSFileManager.defaultManager() 
let directoryArray = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) 
let directoryUrl = directoryArray[0] as NSURL 
let documentName = "MyDocument" 
let url:NSURL = directoryUrl.URLByAppendingPathComponent(documentName) 
var document = UIManagedDocument(fileURL: url) 
var path:NSString = url.path! 
println(path) 

let context:NSManagedObjectContext = document.managedObjectContext 

//document.openWithCompletionHandler(nil) 


var fileExists = fileManager.fileExistsAtPath(path) 

document.fileURL 


document.saveToURL(url, forSaveOperation: UIDocumentSaveOperation.ForCreating, completionHandler: nil) 

我得到那個說「執行被中斷,原因:信號SIGABRT」的錯誤。控制檯輸出似乎表明,我創建的URL存在問題,或者它是零,但我不確定這是爲什麼。

/var/folders/tc/9f9rjh1d0pn2r1qbg4qhqbqw0000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-CB14C6DC-51ED-4C53-987E-4616DD3A09FA/Documents/MyDocument 
2014-09-30 20:50:18.648 MyPlayground[11196:3191065] failed to get type for URL (file:///var/folders/tc/9f9rjh1d0pn2r1qbg4qhqbqw0000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-CB14C6DC-51ED-4C53-987E-4616DD3A09FA/Documents/MyDocument) error: The operation couldn’t be completed. (Cocoa error 260.) 
2014-09-30 20:50:18.648 MyPlayground[11196:3191065] failed to get type for URL (file:///var/folders/tc/9f9rjh1d0pn2r1qbg4qhqbqw0000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-CB14C6DC-51ED-4C53-987E-4616DD3A09FA/Documents/MyDocument) error: The operation couldn’t be completed. (Cocoa error 260.) 
2014-09-30 20:50:18.649 MyPlayground[11196:3191065] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil.' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00000001016bf3f5 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x00000001031cabb7 objc_exception_throw + 45 
    2 CoreFoundation      0x00000001016bf32d +[NSException raise:format:] + 205 
    3 Foundation       0x00000001010ef39f -[NSURL(NSURLPathUtilities) URLByAppendingPathExtension:] + 78 
    4 UIKit        0x000000010201240d -[UIDocument saveToURL:forSaveOperation:completionHandler:] + 151 
    5 ???         0x000000010cf2904c 0x0 + 4512190540 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

在我看來,在操場上我創建的網址不是零,所以我真的迷路了。

我非常感謝任何幫助或建議:)非常感謝!

回答

1

在此示例中,UIManagedDocument沒有可傳遞到持久性存儲的模型配置。 UIManagedDocument的文檔說:「文檔對象然後創建一個Core Data堆棧,用於使用應用程序主包中的託管對象模型訪問文檔的持久性存儲。」但在這種情況下,沒有主包,也沒有找到對象模型。你可能能夠在操場內建立所有這些,但我不知道該怎麼做。所以,這可能只是遊樂場的一個限制,不允許你在那裏嘗試。