Save a tuple in NSUserDefaults

后端 未结 5 1653
梦毁少年i
梦毁少年i 2021-01-12 13:14

I\'m using a tuple to store something like this.

var accessLavels: (hasInventoryAccess: Bool, hasPayrolAccess: Bool)
accessLavels = (hasInventoryAccess: true         


        
5条回答
  •  遥遥无期
    2021-01-12 13:47

    I encountered a similar scenario trying to encode a tuple with NSCoder. The way I am solving it is by manually converting the tuple to a Dictionary. This is not a great solution as the keys need to be changed in several places if the tuple ever changes.

    I had a nested enum in my tuple and gave it a base type (String) from which I converted the raw value. It was a little extra work but thankfully yours is only primitives.

    # SerializeableTuple.swift
    
    typealias AccessTuple = (hasInventoryAccess: Bool, hasPayrolAccess: Bool)
    typealias AccessDictionary = [String: Bool]
    
    let InventoryKey = "hasInventoryAccess"
    let PayrollKey = "hasPayrollAccess"
    
    func serializeTuple(tuple: AccessTuple) -> AccessDictionary {
        return [
            InventoryKey : tuple.hasInventoryAccess,
            PayrollKey : tuple.hasPayrolAccess
        ]
    }
    
    func deserializeDictionary(dictionary: AccessDictionary) -> AccessTuple {
        return AccessTuple(
            dictionary[InventoryKey] as Bool!,
            dictionary[PayrollKey] as Bool!
        )
    }
    

    # Encoding / Decoding
    
    var accessLavels: AccessTuple = (hasInventoryAccess: true, hasPayrolAccess: false)
    
    // Writing to defaults
    let accessLevelDictionary = serializeTuple(accessLavels)
    NSUserDefaults.standardUserDefaults().setObject(accessLevelDictionary, forKey: "AccessLevelKey")
    
    // Reading from defaults
    let accessDic = NSUserDefaults.standardUserDefaults().dictionaryForKey("AccessLevelKey") as AccessDictionary
    let accessLev = deserializeDictionary(accessDic)
    

提交回复
热议问题