我只是喜欢研究更多的东西,而不是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 forDictionary<String, Int>
.Dictionary<String, Int>
is just a simple example of a generic type.Dictionary
has two generic type parameters, calledKey
andValue
.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 asKey
andValue
.Dictionary<String, Int>
is a specific type, aDictionary
whoseKey
type isString
, and whoseValue
type isInt
.In both cases, the trailing
()
are a call to an initializer, syntactic sugar for[String: Int].init()
andDictionary<String, Int>.init()
, respectively.