如何使用NSCoder在swift中编码枚举

如何使用NSCoder在swift中编码枚举

本文介绍了如何使用NSCoder在swift中编码枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我正在尝试使用NSCoding协议对String风格的枚举进行编码,但是我遇到错误转换为从String返回。



解码和编码时出现以下错误:



字符串不是 b
$ b

额外的参数ForKey:在调用中



代码

 枚举阶段:String 
{
case DisplayAll =显示所有
case HideQuarter =Hide Quarter
case HideHalf =Hide Half
case HideTwoThirds =Hide Three Thirds
case HideAll =Hide All
}

class AppState:NSCoding,NSObject
{
var idx = 0
var stage = Stage.DisplayAll

override init ){}

必需的init(coder aDecoder:NSCoder){
self.idx = aDecoder。 decodeIntegerForKey(idx)
self.stage = aDecoder.decodeObjectForKey(stage)as String // ERROR
}

func encodeWithCoder(aCoder:NSCoder){
aCoder.encodeInteger(self.idx,forKey:idx)
aCoder.encodeObject(self.stage as String,forKey:stage)// ERROR
}

// ...

}


解决方案

您需要将枚举转换为原始值和从原始值转换。在Swift 1.2(Xcode 6.3)中,这将如下所示:

  class AppState:NSObject,NSCoding 
{
var idx = 0
var stage = Stage.DisplayAll

override init(){}

必需的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)
}

// ...

}
/ pre>

Swift 1.1(Xcode 6.1)使用作为而不是 as! / code>:

  self.stage = Stage(rawValue:(aDecoder.decodeObjectForKey(stage)as String ))?? .DisplayAll 

Swift 1.0(Xcode 6.0)使用 toRaw() fromRaw()这样:

  self。 stage = Stage.fromRaw(aDecoder.decodeObjectForKey(stage)as String)? .DisplayAll 

aCoder.encodeObject(self.stage.toRaw(),forKey:stage)


Background

I am trying to encode a String-style enum using the NSCoding protocol, but I am running into errors converting to and back from String.

I get the following errors while decoding and encoding:

String is not convertible to Stage

Extra argument ForKey: in call

Code

    enum Stage : String
    {
        case DisplayAll    = "Display All"
        case HideQuarter   = "Hide Quarter"
        case HideHalf      = "Hide Half"
        case HideTwoThirds = "Hide Two Thirds"
        case HideAll       = "Hide All"
    }

    class AppState : NSCoding, NSObject
    {
        var idx   = 0
        var stage = Stage.DisplayAll

        override init() {}

        required init(coder aDecoder: NSCoder) {
            self.idx   = aDecoder.decodeIntegerForKey( "idx"   )
            self.stage = aDecoder.decodeObjectForKey(  "stage" ) as String    // ERROR
        }

        func encodeWithCoder(aCoder: NSCoder) {
            aCoder.encodeInteger( self.idx,             forKey:"idx"   )
            aCoder.encodeObject(  self.stage as String, forKey:"stage" )  // ERROR
        }

    // ...

    }
解决方案

You need to convert the enum to and from the raw value. In Swift 1.2 (Xcode 6.3), this would 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() like this:

    self.stage = Stage.fromRaw(aDecoder.decodeObjectForKey( "stage" ) as String) ?? .DisplayAll

    aCoder.encodeObject( self.stage.toRaw(), forKey:"stage" )

这篇关于如何使用NSCoder在swift中编码枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:44