【发布时间】:2019-02-25 05:43:13
【问题描述】:
我想知道为什么地图格式必须是{( )} 而不仅仅是{ }
func intersect(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
// the following is right
var num1Reduce = nums1.reduce(0){ $0 + $ 1}
/// the following is wrong ??
var num2Dict = Dictionary(nums2.map{ $0, 1 }, uniquingKeysWith : +)
// the following is right
var num1Dict = Dictionary(nums1.map{ ($0, 1) }, uniquingKeysWith : +)
}
我什至看到以下格式({ })。我完全糊涂了!
let cars = peopleArray.map({ $0.cars })
print(cars)
【问题讨论】:
-
( )正在创建一个元组。请参阅您正在使用的Dictionary初始化程序的文档。 -
@rmaddy,有什么有用的网址作为参考吗?
-
我在this answer 中解释了闭包的每个语法糖(除了捕获表达式)。它一步一步地从冗长到 Swifty。值得一看!
标签: swift