我试图在Swift中使用MKLocalSearch Api。但我不能让它工作。错误来自
var search:MKLocalSearch = MKLocalSearch.init(request)
我读了文档,它状态的方法名称是init(请求:)我不确定我做错了什么。请建议。:)
var request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchTextFiled.text
println(searchTextFiled.text)
request.region = self.mapView.region //need to define region later
var search:MKLocalSearch = MKLocalSearch.init(request)
search.startWithCompletionHandler {
(response:MKLocalSearchResponse!, error:NSError!) in
if !error {
var placemarks:NSMutableArray = NSMutableArray()
for item in response.mapItems {
placemarks.addObject(placemarks)
}
self.mapView.removeAnnotations(self.mapView.annotations)
self.mapView.showAnnotations(placemarks, animated: true)
} else {
}
}
改变这一行:
var search:MKLocalSearch = MKLocalSearch.init(request)
:
var search:MKLocalSearch = MKLocalSearch.init(request: request)
在这种情况下,它的命名有点令人困惑-第一部分,request:
是一个标签,告诉Swift调用哪个init函数-在这种情况下,它映射回Objective-C initWithRequest:
方法。
第二部分,request
是传递给init函数的MKLocalSearchRequest
变量。Objective-C中等价的是:
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
在苹果的 Swift编程语言书中有一整章标题为"初始化",在iBooks商店中可以找到,PDF格式在这里,尽管PDF格式更差(接近糟糕)。
init的语法不正确。它们是Swift隐含的,所以你应该使用
var x = MKLocalSearch(request: request)
不var x = MKLocalSearch.init(request)