2017-09-26 76 views
0

我被繼雷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() 
     } 
    } 
} 

但我有兩個錯誤。

對於此行:

urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! 

我得到了錯誤:

Value of type 'String' has no member 'stringByAddingPercentEncodingWithAllowedCharacters'

,並在這條線:

UIApplication.sharedApplication().networkActivityIndicatorVisible = true 

我得到了錯誤:

Cannot call value of non-function type 'UIApplication'

我試圖在網上尋找這些問題的解決方案,但沒有。有人能告訴我如何調整這些嗎?

+0

搜索stringByAddingPercentEncodingWithAllowedCharacters +夫特3:https://stackoverflow.com/questions/32064754/how-to-use-stringbyaddingpercentencodingwithallowedch aracters-for-a-url-in-swi(否則,Swift 3簽名在文檔中) – Larme

回答

1

這些函數名與斯威夫特3.改變對於第一個,你應該使用String.addingPercentEncoding(withAllowedCharacters:)

urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 

此外,UIApplication.sharedApplication()成爲UIApplication.sharednetworkActivityIndicatorVisible斯威夫特3有一個is前綴,類似於其他布爾值:

UIApplication.shared.isNetworkActivityIndicatorVisible = true 
+0

謝謝你的工作!我只採取這最後一個問題:「這種表達式的類型是不明確的,沒有更多的上下文」在這一行let downloadPhoto = UIImage(data:NSData(contentsOfURL:url)!) – bero

+0

@bero Swift 3的語法是'let downloadedPhoto = UIImage(data:Data(contentsOf:url))' – the4kman

+0

它的工作,謝謝! – bero

相關問題