1

我有这个枚举:

@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+01008629 :由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[_SwiftValue encodeWithCoder:]:无法识别的选择器发送到实例 0x600001c7d7a0”*第一次抛出调用堆栈:

4

1 回答 1

4

您从init(coder:). 为什么不编码 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吗?)

于 2018-10-29T21:58:14.010 回答