gpt4 book ai didi

ios - 以 swift 元组为值的 NSCoding swift 字典

转载 作者:可可西里 更新时间:2023-11-01 00:53:18 26 4
gpt4 key购买 nike

我有一个 Swift 字典,键是字符串,值是 Swift 元组。我想将这本字典发送到其他设备,所以我需要在这本字典上实现 NSCoding。任何人都可以帮助我如何实现这一目标。以下是我的字典代码。

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()
}

}

最佳答案

您遇到的问题是元组是 structs 而不是 class(对象)类型。当您尝试执行以下操作时,您会注意到此问题:

if let dictionary = aDecoder.decodeObjectForKey("truthDict") as? [String : RelationshipType] { ... }

不幸的是,在尝试处理 Swift 中的值类型时会出现这个问题。要绕过此限制,您可以使用通用 Box 类来装箱您的值类型,如下所示:

class Box<T>
{
var value : T?

init(_ value: T?)
{
self.value = value
}
}

通过 Box 类,您可以使用它来编码和解码您的字典元组:

class TruthDictionary : NSObject, NSCoding, SequenceType
{
typealias RelationshipType = (ip: String, groupId: String?)

private var dictionary = [String : RelationshipType]()

subscript(key: String) -> RelationshipType?
{
get { return self.dictionary[key] }
set { self.dictionary[key] = newValue }
}

// MARK: - Initializers

override init()
{
super.init()
}

// MARK: - NSCoding

required init(coder aDecoder: NSCoder)
{
// Unbox each tuple from the decoded dictionary
if let boxedDictionary = aDecoder.decodeObjectForKey("truthDict") as? [String : Box<RelationshipType>]
{
for (key, boxedTuple) in boxedDictionary
{
self.dictionary[key] = boxedTuple.value
}
}
}

func encodeWithCoder(aCoder: NSCoder)
{
var boxedDictionary = [String: Box<RelationshipType>]()

// Box each tuple to the dictionary to be encoded
for (key, tuple) in self.dictionary
{
boxedDictionary[key] = Box(tuple)
}

aCoder.encodeObject(boxedDictionary, forKey: "truthDict")
}

// MARK: - SequenceType

func generate() -> DictionaryGenerator<String, RelationshipType>
{
return dictionary.generate()
}
}

关于ios - 以 swift 元组为值的 NSCoding swift 字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29107924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com