Friday, 15 August 2014

ios - NSCoding swift dictionary with swift tuple as values -


I have a swift dictionary with keys such as strings and values ​​swift tuples. I would like to send this dictionary to another device so that I need to implement the NSCOD on this dictionary. Can someone help me how can I get this? After my dictionary code is

  class STCTruthDict:. NSObject, NSCoding, SequenceType {typealias IpRelationshipTuple = (string, string?) Private var TruthDict: [string: IpRelationshipTuple] =: (subscript key []: String) - & gt; IpRelationshipTuple? {Self.truthDict return got {[key]} set {TruthDict [key] = newValue}} // Mark: - initializers init () override {super.init ()} init Proteus aDecoder required (cipher: NSCoder) { Self.truthDict = ADecoder.decodeObjectForKey ("truthDict") as! [String: IpRelationshipTuple] Let's key = aDecoder.decodeObjectForKey ("user_id") as? As string ip = aDecoder.decodeObjectForKey ("tupleIp")? Give string group id = aDecoder.decodeObjectForKey ("tupleGroupId")? String} function encodeWithCoder (aCoder: NSCoder) {(key tuple) self.truthDict {for aCoder.encodeObject (key, forKey: "user_id") aCoder.encodeObject (tuple.Ip, forKey: "tupleIp") aCoder. EncodeObject (Tuple.groupId, forKey: "tupleGroupId")}} func generated () - & gt; DictionaryGrenator & lt; String, iptlxtplay & gt; {Return self.truthDict.generate ()}}  

The problem you are running in This is that Tuples are not structs and class (object) types when you try to do the following: you will notice this issue:

  If the argument = aDecoder.decodeObjectForKey ("truthDict") as? [String: Relationship] {...}  

When trying to deal with the price type in Swift, this problem unfortunately comes a little bit up. To achieve this limit, you can type your value like normal box class:

  square box & lt; T & gt; {Var Value: T? init (_ value :? t) {self.value = value}}  

The box class, you encode and decode its tuples Dictionary can be used for:

  class TruthDictionary: NSObject, NSCoding, SequenceType {typealias RelationshipType = (IP: string Group: string) private var dictionary = [string: RelationshipType] () subscript (Key: string) - & gt; RelationshipType? Initializers init () Override {super.init ()} // Mark: - -: NSCoding required init (coder // mark {return self.dictionary [key]} set {self.dictionary [key] = newValue} get {} Adikoder: Anssiodiar) to each tuple Anboks the dictionary were decoded {// if Boksed dictionary = aDecoder.decodeObjectForKey (as "trueDict")? [String: box & lt; RelationshipType & gt;] {{self.dictionary [key to boxedDictionary (key, boxedTuple)] = boxedTuple.value}}} function encodeWithCoder (aCoder: NSCoder) {var boxedDictionary = [string box & LT; RelationshipType & gt;] () // box to be {boxedDictionary [key to encode in self.dictionary for each tuple (key tuple) in the dictionary] = box (tuple)} aCoder.encodeObject (boxedDictionary, forKey: "TruthDict") // mark: - sequence type func generated () - & gt; DictionaryGrenator & lt; String, Relationships & gt; {Return.g.g./generate ()}}  

No comments:

Post a Comment