I'm creating some functionality in my vacation app by following a tutorial on Ray Wenderlich, so I created this class:
我正在按照Ray Wenderlich的教程在我的度假应用程序中创建一些功能,所以我创建了这个类:
import Foundation
import Foundation
import CoreLocation
import SwiftyJSON
class GoogleDataProvider {
var photoCache = [String:UIImage]()
var placesTask: URLSessionDataTask?
var session: URLSession {
return NSURLSession.sharedSession()
}
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, types:[String], completion: (([GooglePlace]) -> Void)) -> ()
{
var urlString = "http://localhost:10000/maps/api/place/nearbysearch/json?location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true"
let typesString = types.count > 0 ? types.joined(separator: "|") : "food"
urlString += "&types=\(typesString)"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
if let task = placesTask, task.taskIdentifier > 0 && task.state == .Running {
task.cancel()
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
var placesArray = [GooglePlace]()
if let aData = data {
let json = JSON(data:aData, options:NSJSONReadingOptions.MutableContainers, error:nil)
if let results = json["results"].arrayObject as? [[String : AnyObject]] {
for rawPlace in results {
let place = GooglePlace(dictionary: rawPlace, acceptedTypes: types)
placesArray.append(place)
if let reference = place.photoReference {
self.fetchPhotoFromReference(reference) { image in
place.photo = image
}
}
}
}
}
dispatch_async(dispatch_get_main_queue()) {
completion(placesArray)
}
}
placesTask?.resume()
}
func fetchPhotoFromReference(reference: String, completion: ((UIImage?) -> Void)) -> () {
if let photo = photoCache[reference] as UIImage? {
completion(photo)
} else {
let urlString = "http://localhost:10000/maps/api/place/photo?maxwidth=200&photoreference=\(reference)"
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
session.downloadTaskWithURL(NSURL(string: urlString)!) {url, response, error in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if let url = url {
let downloadedPhoto = UIImage(data: NSData(contentsOfURL: url)!)
self.photoCache[reference] = downloadedPhoto
dispatch_async(dispatch_get_main_queue()) {
completion(downloadedPhoto)
}
}
else {
dispatch_async(dispatch_get_main_queue()) {
completion(nil)
}
}
}.resume()
}
}
}
But I got two errors.
但我有两个错误。
For this line:
对于这一行:
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
I got the error:
我收到了错误:
Value of type
'String'
has no member'stringByAddingPercentEncodingWithAllowedCharacters'
“String”类型的值没有成员'stringByAddingPercentEncodingWithAllowedCharacters'
and in this line:
在这一行:
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
I got the error:
我收到了错误:
Cannot call value of non-function type
'UIApplication'
无法调用非函数类型'UIApplication'的值
I tried to look on the net to find a solution for these issues, but nothing. Can someone tell me how I can adjust these?
我试图在网上寻找这些问题的解决方案,但没有。有人能告诉我如何调整这些吗?
1 个解决方案
#1
1
These function names changed with Swift 3. For the first one, you should use String.addingPercentEncoding(withAllowedCharacters:)
:
使用Swift 3更改了这些函数名。对于第一个函数名,应该使用String.addingPercentEncoding(withAllowedCharacters :):
urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
Also, UIApplication.sharedApplication()
became UIApplication.shared
and networkActivityIndicatorVisible
got an is
prefix, similar to other boolean values in Swift 3:
此外,UIApplication.sharedApplication()成为UIApplication.shared,networkActivityIndicatorVisible得到一个is前缀,类似于Swift 3中的其他布尔值:
UIApplication.shared.isNetworkActivityIndicatorVisible = true
#1
1
These function names changed with Swift 3. For the first one, you should use String.addingPercentEncoding(withAllowedCharacters:)
:
使用Swift 3更改了这些函数名。对于第一个函数名,应该使用String.addingPercentEncoding(withAllowedCharacters :):
urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
Also, UIApplication.sharedApplication()
became UIApplication.shared
and networkActivityIndicatorVisible
got an is
prefix, similar to other boolean values in Swift 3:
此外,UIApplication.sharedApplication()成为UIApplication.shared,networkActivityIndicatorVisible得到一个is前缀,类似于Swift 3中的其他布尔值:
UIApplication.shared.isNetworkActivityIndicatorVisible = true