0

I am new to coding and attempting to build my first app.

I am trying to create a UIManagedDocument using Swift Playgrounds by following the steps outlined in the CS193 Standford iOS course from last year, lecture 12. They are in objective C and I am trying to convert them to Swift. I have spent all day banging my head against the wall trying to find what I am doing wrong :/

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)

I get an error that says 'Execution was interrupted, reason: Signal SIGABRT'. The console output seems to indicate that there is a problem with the URL I have created, or that it is nil, but I am not sure why that is.

/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

it appears to me in the playground that the url I have created not nil so I am really lost.

I would GREATLY appreciate any help or advice :) thanks a lot!

TaeVitae
  • 9
  • 1

1 Answers1

1

In this example, the UIManagedDocument does not have a Model Configuration that it can pass to the persistent store. The documentation for UIManagedDocument says, "The document object then creates a Core Data stack to use to access the document’s persistent store using a managed object model from the application’s main bundle." But in this case there is no main bundle and no object model can be found. You may be able to build all of that inside of the playground, but I'm not sure how to do that. So, this may simply be a limitation of the playground that will not allow you to try this there.

DataJock
  • 226
  • 2
  • 6