【发布时间】:2015-04-04 00:05:21
【问题描述】:
每次我在这里问问题,都是因为我不知道要搜索什么,对此我感到非常抱歉。
无论如何,我正在尝试使用 Google Maps SDK 创建一个应用程序,并且我遵循了 Ron Kliffer 在 Ray Wenderlich 的网站上的一个非常好的教程。但是,我想对其进行大量自定义,因此我尝试阅读每一行并了解它的作用。现在我遇到了一个我不理解语法的块。
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius:
Double, types:[String], completion: (([GooglePlace]) -> Void)) -> ()
这就是被调用的函数,这样:
dataProvider.fetchPlacesNearCoordinate(coordinate, radius:mapRadius, types: searchedTypes) { places in
for place: GooglePlace in places {
我根本不明白完成:位和“放置”位。接下来是,如果我没记错的话,一个“foreach”的东西(或 Swift 语法中的 for in。
编辑:重新格式化
Edit2:是的,在函数体中创建了一个 GooglePlaces 数组,并且在 forin-thing 中使用了相同的数组(我猜)。
【问题讨论】:
-
是的,我对 for-in 很熟悉,但对函数调用末尾的单个“places in”以及未使用完成“argument”感到困惑(因为它是 void? )。
-
然后看看developer.apple.com/library/ios/documentation/Swift/Conceptual/…中的“闭包表达式语法”。 “places”为闭包参数,在
for place: GooglePlace in places { }中使用。 -
非常感谢。这正是我正在寻找的。span>
-
有很多很好的博客文章和资源可以解释闭包。 Martin R 向您指出了最好的 Apple 文档,但这里还有更多:airspeedvelocity.net/2014/06/11/… 和 letvargo.mooo.com/a-beginners-guide-to-closures-in-swift
标签: ios xcode google-maps swift