I have the following code, used to get the path of an object that has been archived
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let path = paths[0] as String
let archivePath = path.stringByAppendingString("archivePath")
When I run this code, it crashes at the NSSearchPathForDirectoriesInDomains
call with lldb showing
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
In Xcode's Variables View I see the path String set as I would expect. What is the proper way to get a user directory in Swift for archiving/unarchiving objects?
Update:
It appears this is actually crashing on my use of the NSKeyedUnarchiver:
stopwatches = NSKeyedUnarchiver.unarchiveObjectWithFile(archivePath) as Stopwatch []
Stopwatch is a class that implements NSCoding, stopwatches is the datasource (an array of Stopwatches) owned by the view doing the unarchiving.
Update 2:
The object graph being archived is an array of Stopwatches. NSCoding is implemented as follows:
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeBool(self.started, forKey: "started")
aCoder.encodeBool(self.paused, forKey: "paused")
aCoder.encodeObject(self.startTime, forKey: "startTime")
aCoder.encodeObject(self.pauseTime, forKey: "pauseTime")
aCoder.encodeInteger(self.id, forKey: "id")
}
init(coder aDecoder: NSCoder!) {
self.started = aDecoder.decodeBoolForKey("started")
self.paused = aDecoder.decodeBoolForKey("paused")
self.startTime = aDecoder.decodeObjectForKey("startTime") as NSDate
self.pauseTime = aDecoder.decodeObjectForKey("pauseTime") as NSDate
self.id = aDecoder.decodeIntegerForKey("id")
super.init()
}
Update 3:
With expandTilde
set to true my path is /Users/Justin/Library/Developer/CoreSimulator/Devices/FF808CCD-709F-408D-9416-EE47B306309D/data/Containers/Data/Application/B39CCB84-F335-4B70-B732-5C3C26B4F6AC/Documents/ArchivePath
If I set expandTilde
to false, I don't get the crash, but the file is not archived and unarchived, and the path is @"~/Documents/ArchivePath"
Deleting the Application folder causes the first launch of the application to not crash, but does not allow it to reopen afterwards. Also, after deleting the application folder, I am now able to read the archive path in lldb rather than having to println it.
archivePath
that is being passed in? – Veld/Users/Justin/Library/Developer/CoreSimulator/Devices/FF808CCD-709F-408D-9416-EE47B306309D/data/Containers/Data/Application/B39CCB84-F335-4B70-B732-5C3C26B4F6AC/Documents/ArchivePath
, but if I try to print the description using lldb I getPrinting description of ArchivePath: (String) ArchivePath = <variable not available>
– PyreneesStopwatch[]
variable? Or is it anNSArray
? – VeldNSKeyedArchiver.archiveRootObject(self.stopwatches, toFile: ArchivePath)
andself.stopwatches
is defined in the class asvar stopwatches: Stopwatch []
– PyreneesNSString
instead ofString
? – Chidesuper.init()
in your init method? – Chideclass Stopwatch: NSObject, NSCoding {
– Pyreneesstopwatches
is just aStopwatch[]
? – Tampon