I have a Swift dictionary with keys as Strings and values as Swift tuples. I would like to send this dictionary over to the other device so I need to implement NSCoding on this dictionary. Can anybody help me as to how can I achieve that. Following is my dictionary code.
class STCTruthDict: NSObject, NSCoding, SequenceType {
typealias IpRelationshipTuple = (String, String?)
private var truthDict: [String : IpRelationshipTuple] = [ : ]
subscript(key: String) -> IpRelationshipTuple? {
get {
return self.truthDict[key]
}
set {
truthDict[key] = newValue
}
}
// MARK: - Initializers
override init() {
super.init()
}
required init(coder aDecoder: NSCoder) {
self.truthDict = aDecoder.decodeObjectForKey("truthDict") as! [String : IpRelationshipTuple]
let key = aDecoder.decodeObjectForKey("user_id") as? String
let ip = aDecoder.decodeObjectForKey("tupleIp") as? String
let groupId = aDecoder.decodeObjectForKey("tupleGroupId") as? String
}
func encodeWithCoder(aCoder: NSCoder) {
for (key, tuple) in self.truthDict {
aCoder.encodeObject(key, forKey: "user_id")
aCoder.encodeObject(tuple.Ip, forKey: "tupleIp")
aCoder.encodeObject(tuple.groupId, forKey: "tupleGroupId")
}
}
func generate() -> DictionaryGenerator <String, IpRelationshipTuple> {
return self.truthDict.generate()
}
}