下面的代码archivedTimes()在swift4中成功构建。它在安装了ios10.3的设备上运行良好。
typealias Time = CMTime
typealias Times = [Time]
static let times: Times = Array<Int64>.init(1...9).map({ CMTime.init(value: $0, timescale: 100) })
static func archivedTimes() -> Data {
return archived(times: times)
}
static func archived(times: Times) -> Data {
let values = times.map({ NSValue.init(time: $0) })
return NSKeyedArchiver.archivedData(withRootObject: values) // ERROR here
// -- ideally would instead be:
// return NSKeyedArchiver.archivedData(withRootObject: times)
// -- but probably not compatible with ios 9.3
}然而,当在安装了ios9.3的设备上运行它时,它崩溃了,它说:
由于“NSInvalidArgumentException”异常终止应用程序,原因:‘* -NSKeyedArchiver encodeValueOfObjCType:at::此归档程序无法编码结构’
我的猜测是,这可能与新的可编码协议和旧的NSCoder协议之间的冲突有关。但我不知道是什么!
请注意,该问题与数组无关。作为存档,一个简单的CMTime也会导致这样的错误。然而,我这样发布它,因为归档CMTime数组最终是我的目标。
发布于 2017-10-21 10:33:25
我认为Codable协议只在ios10中可用,因此在ios9上,CMTime不实现Codable。
因此,对于ios9,我使用了一个CMTime的包装类,它实现了NSCoding协议。
这可以通过导入AVFoundation来实现,后者将扩展声明为NSValue和NSCoder,以便对CMTime进行编码。
然后,我使用了一个WrappedTime.init($0)数组,而不是一个NSValue.init(time: $0)数组。
class WrappedTime: NSObject, NSCoding {
enum EncodeKey: String {
case time = "time"
}
let time: CMTime
// ...
func encode(with aCoder: NSCoder) {
aCoder.encode(time, forKey: EncodeKey.time.rawValue)
}
required init?(coder aDecoder: NSCoder) {
time = aDecoder.decodeTime(forKey: EncodeKey.time.rawValue)
}
init(_ time: Time) {
self.time = time
}
}https://stackoverflow.com/questions/46834728
复制相似问题