使用无法识别的选择器NSUserDefaults保存枚举

时间:2018-10-29 21:24:45

标签: swift enums nsuserdefaults unrecognized-selector

我有这个枚举:

@objc enum Groups: Int {
    case large = 0
    case medium = 1
    case small = 2
    case micro = 3
    case all = 4
    case favoriten = 5
}

enum Ordering:String {
    case ascending
    case descending
    case nothing
}

enum QuantityStats:String {
    case one
    case two
    case three
    case four
    case six
}

enum KeysState: String {
    case listTableViewState
    case listLabelState
}

这些是此类的一部分:

class ListTableViewState: State, NSCoding {

    private enum Keys: String {
        case group
        case statIntervalBase
        case order
        case quantityStats
    }

    var group: Groups {
        didSet {
            if initialized {

                saveUserDefaults(withKey: KeysState.listTableViewState.rawValue, myType: self)

            }
        }
    }
    var statIntervalBase: StatIntervalBaseModel
    var order: Ordering
    var searchParameter: String?
    var quantityStats: QuantityStats


    init(group: Groups, statIntervalBase: StatIntervalBaseModel, order: Ordering, searchParameter: String?, quantityStats: QuantityStats) {
        self.group = group
        self.statIntervalBase = statIntervalBase
        self.order = order
        self.searchParameter = searchParameter
        self.quantityStats = quantityStats

        print("Group", Keys.group, "order", Keys.order)

        super.init()
        initialized = true
    }


    required convenience init?(coder aDecoder: NSCoder) {
//        self.stage = Stage(rawValue: aDecoder.decodeObject(forKey: "stage") as String)
        let group = Groups(rawValue: aDecoder.decodeObject(forKey: Keys.group.rawValue) as! Int)
        let statIntervalBase = aDecoder.decodeObject(forKey: Keys.statIntervalBase.rawValue) as! StatIntervalBaseModel
        let order = Ordering(rawValue: aDecoder.decodeObject(forKey: Keys.order.rawValue) as! String)
        let quantityStats = QuantityStats(rawValue: aDecoder.decodeObject(forKey: Keys.quantityStats.rawValue) as! String)
        self.init(group: group!, statIntervalBase: statIntervalBase, order: order!, searchParameter: "", quantityStats: quantityStats!)

    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(group, forKey: Keys.group.rawValue)
        aCoder.encode(statIntervalBase, forKey: Keys.statIntervalBase.rawValue)
        aCoder.encode(order, forKey: Keys.order.rawValue)
        aCoder.encode(quantityStats, forKey: Keys.quantityStats.rawValue)
    }

}

这是我制作的State Base类:

class State: NSObject {

    var initialized = false

    func saveUserDefaults<T>(withKey key: String, myType: T){

        let archiver:Data = NSKeyedArchiver.archivedData(withRootObject: myType)

        UserDefaults.standard.set(archiver, forKey: key)

        UserDefaults.standard.synchronize()
    }

    func loadUserDefaults<T>(withKey key: String) -> T? {
        var output: T?
        let decoded = UserDefaults.standard.object(forKey: key)
        if decoded != nil {
            let data = decoded as! Data
            output = (NSKeyedUnarchiver.unarchiveObject(with: data) as! T)
        } else {
            output = nil
        }

        return output
    }

}

我想用NSUserDefaults保存ListTableViewState,但是我一直出错,我认为我把枚举保存错了。但是我不知道该怎么解决。

这是我得到的错误:

  

2018-10-29 18:53:22.906915 + 0000 APP [40545:8188629]-[_ SwiftValue   encodeWithCoder:]:无法识别的选择器已发送到实例   0x600001c7d7a0 2018-10-29 18:53:22.912060 + 0000 APP [40545:8188629]    *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[_ SwiftValue   encodeWithCoder:]:无法识别的选择器已发送到实例   0x600001c7d7a0'   * 首先抛出调用堆栈:

1 个答案:

答案 0 :(得分:2)

您可以从init(coder:)中的rawValue构造每个枚举值。为什么不对rawValue进行编码?

required convenience init?(coder aDecoder: NSCoder) {
    let group = Groups(rawValue: aDecoder.decodeInteger(forKey: Keys.group.rawValue))
    let statIntervalBase = aDecoder.decodeObject(forKey: Keys.statIntervalBase.rawValue) as! StatIntervalBaseModel
    let order = Ordering(rawValue: aDecoder.decodeObject(forKey: Keys.order.rawValue) as! String)
    let quantityStats = QuantityStats(rawValue: aDecoder.decodeObject(forKey: Keys.quantityStats.rawValue) as! String)
    self.init(group: group!, statIntervalBase: statIntervalBase, order: order!, searchParameter: "", quantityStats: quantityStats!)
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(group.rawValue, forKey: Keys.group.rawValue)
    aCoder.encode(statIntervalBase, forKey: Keys.statIntervalBase.rawValue)
    aCoder.encode(order.rawValue, forKey: Keys.order.rawValue)
    aCoder.encode(quantityStats.rawValue, forKey: Keys.quantityStats.rawValue)
}

我认为您可以将loadUserDefaults(withKey:)写为:

func loadUserDefaults<T>(withKey key: String) -> T? {
    var output: T? = nil

    if let data = UserDefaults.standard.data(forKey: key) {
        output = (NSKeyedUnarchiver.unarchiveObject(with: data) as! T)
    }

    return output
}

(这不是static吗?)