I came across a similar scenario trying to encode a tuple using NSCoder . The way I solve this is to manually convert the tuple to 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 the base type (String) from which I converted the raw value. It was a little extra work, but, fortunately, yours are 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)
source share