2014-09-30 213 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堆栈,用于使用应用程序主包中的托管对象模型访问文档的持久性存储。”但在这种情况下,没有主包,也没有找到对象模型。你可能能够在操场内建立所有这些,但我不知道该怎么做。所以,这可能只是游乐场的一个限制,不允许你在那里尝试。