字典非常熟悉有关此代码的问题。这种代码称为什么?

我只是喜欢研究更多的东西,而不是Swift编程的基础知识,而且实际上注意到字典非常熟悉。如果您知道的话,您想向我解释一下这些代码编程的真正含义和名称吗?

代码结果:

// MARK: Dictionaries
var Weather = [String: String]()
Weather["Sunday"] = "Sunny"

// MARK: What does this with angle bracket syntax is called?
// Notice: It's seems familar to Dictionaries.
var Total_Miles = Dictionary<String, Int>()
Total_Miles["Antonio"] = 100

谢谢您带来的帮助! :)

评论
  • 你已被淘汰
    你已被淘汰 回复

    [String: String] is syntactic sugar for Dictionary<String, Int>.

    Dictionary<String, Int> is just a simple example of a generic type. Dictionary has two generic type parameters, called Key and Value.

    In the general case, Dictionary isn't just one data type. It's like a pattern for establishing data types, one per unique set of type parameters provided as Key and Value.

    Dictionary<String, Int> is a specific type, a Dictionary whose Key type is String, and whose Value type is Int.

    In both cases, the trailing () are a call to an initializer, syntactic sugar for [String: Int].init() and Dictionary<String, Int>.init(), respectively.