我遵循this Swift Post(由蘋果公司於2016年9月發佈)。爲什麼這個在Swift中的Apple例子不適合我?
的崗位實現了以下如下擴展:
extension Restaurant {
private let urlComponents: URLComponents // base URL components of the web service
private let session: URLSession // shared session for interacting with the web service
static func restaurants(matching query: String, completion: ([Restaurant]) -> Void) {
var searchURLComponents = urlComponents
searchURLComponents.path = "/search"
searchURLComponents.queryItems = [URLQueryItem(name: "q", value: query)]
let searchURL = searchURLComponents.url!
session.dataTask(url: searchURL, completion: { (_, _, data, _)
var restaurants: [Restaurant] = []
if let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
for case let result in json["results"] {
if let restaurant = Restaurant(json: result) {
restaurants.append(restaurant)
}
}
}
completion(restaurants)
}).resume()
}
}
當我嘗試在我自己的項目,以重新創建此我得到以下信息:
不能援引「dataTask 'with an argument list of type'(with:String, completionHandler:() - >())'
爲什麼這個帖子和XCode 8.1告訴我的內容有什麼不一致?他們都在同一時間發佈。
我用的雨燕3.0
這不是Swift3兼容(擴展屬性,++)。 – shallowThought