【发布时间】:2014-08-23 16:43:01
【问题描述】:
我有如下代码,用于获取已归档对象的路径
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let path = paths[0] as String
let archivePath = path.stringByAppendingString("archivePath")
当我运行此代码时,它在 NSSearchPathForDirectoriesInDomains 调用时崩溃并显示 lldb
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
在 Xcode 的变量视图中,我看到了我所期望的路径字符串设置。在 Swift 中获取用于归档/取消归档对象的用户目录的正确方法是什么?
更新:
看来这实际上在我使用 NSKeyedUnarchiver 时崩溃了:
stopwatches = NSKeyedUnarchiver.unarchiveObjectWithFile(archivePath) as Stopwatch []
Stopwatch 是一个实现 NSCoding 的类,stopwatches 是执行取消归档的视图所拥有的数据源(一个 Stopwatches 数组)。
更新 2:
被归档的对象图是一个秒表数组。 NSCoding 实现如下:
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()
}
更新 3:
expandTilde 设置为 true 我的路径是 /Users/Justin/Library/Developer/CoreSimulator/Devices/FF808CCD-709F-408D-9416-EE47B306309D/data/Containers/Data/Application/B39CCB84-F335-4B70-B732-5C3C26B4F6AC/Documents/ArchivePath
如果我将expandTilde设置为false,我不会崩溃,但文件没有存档和未存档,路径是@"~/Documents/ArchivePath"
删除应用程序文件夹会导致应用程序的首次启动不会崩溃,但不允许它在之后重新打开。另外,删除应用程序文件夹后,我现在可以读取 lldb 中的存档路径,而不必打印它。
【问题讨论】:
-
在我的 iOS 模拟器中正常工作。但是 stringByAppendingString() 应该是 pathByAppendingPathComponent()。
-
@MartinR 更新了新信息
-
您能告诉我们传入的
archivePath是什么吗? -
@JackWu 当我使用 println 时,它是
/Users/Justin/Library/Developer/CoreSimulator/Devices/FF808CCD-709F-408D-9416-EE47B306309D/data/Containers/Data/Application/B39CCB84-F335-4B70-B732-5C3C26B4F6AC/Documents/ArchivePath,但如果我尝试使用 lldb 打印描述,我会得到Printing description of ArchivePath: (String) ArchivePath = <variable not available> -
有趣的是,当您归档它时,它是从
Stopwatch[]变量归档的吗?还是NSArray?
标签: cocoa-touch swift nscoding nskeyedunarchiver