Ios 如何在NSCoding状态恢复期间引用现有对象
我有一个实现NSCoding并持有对UIView对象的引用的类Ios 如何在NSCoding状态恢复期间引用现有对象,ios,nscoding,nscoder,state-restoration,Ios,Nscoding,Nscoder,State Restoration,我有一个实现NSCoding并持有对UIView对象的引用的类 class A: NSObject, NSCoding { var view: UIView! init(view: UIView) { super.init() commonInit(view) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder)
class A: NSObject, NSCoding {
var view: UIView!
init(view: UIView) {
super.init()
commonInit(view)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit(/* <Need to pass the existing view somehow> */)
}
override func encodeWithCoder(aCoder: NSCoder) {
super.encodeWithCoder(aCoder)
}
}
但是我如何才能使现有视图可用于
A.init?(coder-aDecoder:NSCoder)
?简而言之,您不能。您必须在序列化后提供某种修复。Dang,我想可能是这样的,某种两步初始化。NSCoder只需要允许类似于setObjectReference(obj:AnyObject,forKey:String)
override func decodeRestorableStateWithCoder(coder: NSCoder) {
// How to get self.customView to A.init?(coder aDecoder: NSCoder) where
// it is needed?
let view = self.customView
self.a = coder.decodeObjectForKey("aKey") as! A
}