问题标签 [nskeyedunarchiver]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
15150 浏览

ios - Swift only way to prevent NSKeyedUnarchiver.decodeObject crash?

NSKeyedUnarchiver.decodeObject will cause a crash / SIGABRT if the original class is unknown. The only solution I have seen to catching this issue dates from Swift's early history and required using Objective C (also pre-dated Swift 2's implementation of guard, throws, try & catch). I could figure out the Objective C route - but I would prefer to understand a Swift-only solution if possible.

For example - the data has been encoded with NSPropertyListFormat.XMLFormat_v1_0. The following code will fail at unarchiver.decodeObject() if the class of the encoded data is unknown.

I am looking for a Swift 2 only way to test whether the data is valid before attempting .decodeObject - since .decodeObject has no throws - which means that try - catch does not seem to be an option in Swift (methods without throws cannot be wrapped AFAIK). Or else an alternative way of decoding the data which will throw an error I can catch if the decode fails. I want the user to be able to import a file from iCloud drive or Dropbox - therefore it needs to be properly validated. I cannot assume that the encoded data is safe.

The NSKeyedUnarchiver methods .unarchiveTopLevelObjectWithData & .validateValue both have throws. Is there perhaps some way that these could be used? I cannot work out how to even begin to attempt to implement validateValue in this context. Is this even a possible route? Or should I be looking to one of the other methods for a solution?

Or does anyone know an alternative Swift 2 only way of addressing this issue? I believe that the key I am interested in is probably entitled $classname - but TBH I am out of my depth with respect to trying to work out how to implement validateValue - or even whether that would be the correct route to persevere with. I have the sense that I am missing something obvious.


EDIT: Here is a solution - thanks to rintaro's great answer(s) below

The initial answer solved the issue for me - i.e. implementing a delegate.

For now however I have gone with a solution built around rintaro's additional edited response as follows:

0 投票
1 回答
158 浏览

ios - iOS 9 中的 UIView 复制问题

使用下面的技术我正在复制一个视图,一切都很好,但在 iOS 9 中它无法复制 cloneView 中使用的图像。

谁能帮我解决这个问题。任何建议都将是可观的。

0 投票
1 回答
948 浏览

ios - iOS SWIFT - 文件存档 - 参数类型“[String?]”:不符合预期类型'AnyObject'

我正在尝试写入存档并收到此错误。

我的技术堆栈是:XCode 7.1 Beta 和 SWIFT。如果你们中的任何人都可以分享确切的代码来解决这个问题,不胜感激。提前致谢。

参数类型“[String?]”:不符合预期的类型'AnyObject'

谢谢

0 投票
0 回答
79 浏览

objective-c - 使用 Save-/Open-Panel 保存 NSNumber 数组

我正在尝试保存一个 NSNumber 数组,但我不知道该怎么做。

我的代码适用于单个 NSNumber:

由于它不适用于 NSNumber 数组,因此我尝试使用 NSMutableArray 代替,但它也不起作用:

那么我做错了什么?

编辑:似乎不需要 encodeWithCoder 和 initWithCoder

0 投票
6 回答
3433 浏览

ios - 重命名 Xcode 项目后出现 NSKeyedUnarchiver 错误

我刚刚重命名了我的 Xcode 项目,当我运行它时,我得到了这个错误:

2015-11-14 05:32:42.337 Buck Tracker[3537:1456100] * 由于未捕获的异常“NSInvalidUnarchiveOperationException”而终止应用程序,原因:“* -[NSKeyedUnarchiver decodeObjectForKey:]:无法解码类的对象 (iBudgeter.Record)键(NS.objects);该类可以在源代码或未链接的库中定义'

Buck Tracker新名称,iBudgeter也是原始名称。RecordNSObject我为存储一些数据而创建的自定义。

我尝试将项目重命名为,iBudgeter但没有成功。在 git 中恢复到以前的版本确实有帮助,但是当我再次重命名它时,我得到了同样的错误。

那么有什么建议吗?

0 投票
1 回答
4475 浏览

ios - -[NSKeyedUnarchiver decodeObjectForKey:]:无法解码类的对象

更新我的应用程序后,我有一个我不明白的错误。我在应用商店里已经有一个应用。我有一些特定于应用程序的数据存档并存储在应用程序支持目录内的文件中。每次启动应用程序并将其发送到后台时,我都在读取和存储数据。到目前为止一切都很好。现在我发布了应用程序的更新。更新后,当我启动应用程序并尝试取消归档数据时,它会崩溃并出现以下异常。

如果我删除该应用程序并再次安装该应用程序,一切正常。

我的原始应用程序和更新都是用 Swift 编写的。我也没有将文件名从版本 1 更改为 2。有人知道发生了什么吗?

0 投票
1 回答
29 浏览

objective-c - 将未归档的对象分配给不同的对象有效,为什么/如何?

分配存档字典对象但未存档并分配给数组似乎不会产生异常,并且从数组中注销的数据是字典的数据,如果可以分配给不同的返回类型,则根本不是数组格式,有点链接投射,而是您直接取消存档和分配。为什么这行得通?如果有人能更深入地解释它,它将有助于更好地理解取消归档过程及其工作方式。

0 投票
1 回答
242 浏览

ios - NSKeyedUnarchiver 是否仍然在 iOS9 中抛出异常?

考虑以下尝试NSKeyedUnarchiver从无效NSData对象创建的片段。

当我在 iOS 8.4 模拟器上运行它时,我得到一个NSInvalidArgumentException. 然而,在 iOS 9.1 模拟器上,初始化程序只返回nil.

但是,根据官方文档,该方法应始终对无效数据抛出异常。

如果数据不是有效的存档,此方法将引发异常。

为什么它只在 iOS8 上抛出异常而不在 iOS9 上抛出异常?iOS 9 中的这种新行为是否还没有记录?

0 投票
0 回答
508 浏览

ios - 取消归档返回空值的数据

我创建了一个基于以前的应用程序的应用程序。我正在尝试将数据移过来。我目前正在使用 swift,而之前的应用程序正在使用对象 c。我正在使用这个全局对象 C 函数,它本质上是从其他应用程序复制过来的。它能够抓取数据并且消息计数是正确的,但是当我去查找一个值时它返回 null。见下文。

0 投票
1 回答
262 浏览

swift - 使用 Swift 归档、取消归档和清理缓存中的对象

我在处理缓存中某些类的持久性时遇到问题。这是我尝试过的代码,它在恢复存档数据或删除它时根本不起作用:

我什至尝试在保存对象后立即检索它,结果为零: