如何根据我们自己的逻辑实现快速字典

问题描述 投票:0回答:1

我正在尝试实现swift字典的实际实现。我想知道如何忽略输出代码中的数组变量boxes。难道这正是字典的实际实现吗?

struct Box<U,V> where U: Hashable {

    var que: U
    var ans:V

    fileprivate var boxes = [Box]()

    init(_ que: U,ans: V) {
        self.que = que
        self.ans = ans
    }

    mutating func addBox(with que: U, ans: V) {
        boxes.append(Box(que, ans: ans))
    }

    mutating func deleteBox(_ que: U) {
        for (index, value) in boxes.enumerated() {
            if value.que == que {
                boxes.remove(at: index)
            }
        }
    }
}

class Display {

    func show() {
        var dict = Box(1, ans: "One")
        print(dict)
    }
}

let obj = Display()
obj.show()

// Output:Box(que:1,ans:“ One”,boxes:[]

ios swift dictionary generic-programming generic-collections
1个回答
1
投票

我想知道如何忽略输出代码中的数组变量框

使Box符合CustomStringConvertible并提供您自己的description实现。

这也是字典的实际实现吗?

没有

© www.soinside.com 2019 - 2024. All rights reserved.