ios - 如何根据我们自己的逻辑实现 swift Dictionary
问题描述
我正在尝试实现 swift 字典的实际实现。我想知道如何忽略输出代码中的数组变量框。这也正是字典的实际实现是什么?
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()
//输出: Box(que: 1, ans: "One", boxes: [] )
解决方案
我想知道如何忽略输出代码中的数组变量框
使 Box 符合 CustomStringConvertible 并提供您自己的description
实现。
这也正是字典的实际实现是什么?
不。
推荐阅读
- apache-kafka - 在 SASL 安全的 Confluent 服务器上启动 KSQL,出现错误“未为 JAAS 配置中的键 'â' 指定值”
- office-addins - 使用已经存在的 Web API 项目测试 Office 加载项时出现问题
- android - 当有多个生物识别传感器可用时,如何选择首选类型来使用?
- php - 在由 PHP 代码执行的电子邮件上添加链接
- react-native - 在 ImageBackground 中浮动将背景向右对齐
- visual-studio - 保存时如何防止 Project.params 同步
- vba - vba 中是否有一个 Join 函数来组合多个字段而不是在访问中使用连接函数?
- python - 在移动设备或台式计算机之间部署到 aws elastic beanstalk 的 django 网站有区别吗?
- sql - SQL查询左连接不过滤数据
- sql - 用于跟踪 IPv6/IPv4 地址的关系数据库——我提出的架构可以工作吗?