gpt4 book ai didi

swift - 在 Swift 中创建新字典 - 何时使用哪种方法。

转载 作者:搜寻专家 更新时间:2023-11-01 06:18:19 25 4
gpt4 key购买 nike

我看到可以按如下方式在 swift 中创建字典

var dict1 = [String: Int]()
var dict2 = Dictionary<String, Int>()

然后正常使用它。什么时候应该使用哪种创建字典的方法?

最佳答案

苹果出品的书《The Swift Programming Language》说

The type of a Swift dictionary is written in full as Dictionary<Key, Value>, where Key is the type of value that can be used as a dictionary key, and Value is the type of value that the dictionary stores for those keys.

You can also write the type of a dictionary in shorthand form as [Key: Value]. Although the two forms are functionally identical, the shorthand form is preferred and is used throughout this guide when referring to the type of a dictionary.

所以看起来简写形式是首选。可能是因为它看起来更干净一些。

关于swift - 在 Swift 中创建新字典 - 何时使用哪种方法。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364636/

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