You need to convert the enumeration to and from the original value. In Swift 1.2 (Xcode 6.3), it will look like this:
class AppState : NSObject, NSCoding { var idx = 0 var stage = Stage.DisplayAll override init() {} required init(coder aDecoder: NSCoder) { self.idx = aDecoder.decodeIntegerForKey( "idx" ) self.stage = Stage(rawValue: (aDecoder.decodeObjectForKey( "stage" ) as! String)) ?? .DisplayAll } func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeInteger( self.idx, forKey:"idx" ) aCoder.encodeObject( self.stage.rawValue, forKey:"stage" ) }
Swift 1.1 (Xcode 6.1) uses as instead of as! :
self.stage = Stage(rawValue: (aDecoder.decodeObjectForKey( "stage" ) as String)) ?? .DisplayAll
Swift 1.0 (Xcode 6.0) uses toRaw() and fromRaw() as follows:
self.stage = Stage.fromRaw(aDecoder.decodeObjectForKey( "stage" ) as String) ?? .DisplayAll aCoder.encodeObject( self.stage.toRaw(), forKey:"stage" )
vacawama Oct 12 '14 at 15:26 2014-10-12 15:26
source share