2017-09-26 1 views
0

ich nach einem Tutorial über Ray Wenderlich, so habe ich diese Klasse einige Funktionen in meinem Urlaub app bin erstellen:Swift: Wert vom Typ hat kein Mitglied kann nicht Wert von Nicht-Funktionstyp nennt

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() 
     } 
    } 
} 

Aber ich habe zwei Fehler.

Für diese Strecke:

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

habe ich den Fehler:

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

und in dieser Zeile:

UIApplication.sharedApplication().networkActivityIndicatorVisible = true 

habe ich den Fehler:

Cannot call value of non-function type 'UIApplication'

Ich habe versucht, im Internet nach einer Lösung für diese Probleme zu suchen, aber nichts. Kann mir jemand sagen, wie ich diese einstellen kann?

+0

Suche stringByAddingPercentEncodingWithAllowedCharacters + Swift 3: https://stackoverflow.com/questions/32064754/how-to-use-stringbyaddingpercentencodingwithallowedch aracters-for-a-url-in-swi (sonst ist die Swift 3 Signatur in der Dokumentation) – Larme

Antwort

1

Diese Funktionsnamen mit Swift 3. Für die erste geändert, sollten Sie String.addingPercentEncoding(withAllowedCharacters:) verwenden:

urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 

Auch wurde UIApplication.sharedApplication()UIApplication.shared und networkActivityIndicatorVisible bekam ein is Präfix, ähnlich wie bei anderen Boolesche Werte in Swift 3:

UIApplication.shared.isNetworkActivityIndicatorVisible = true 
+0

danke es funktioniert! Ich nehme nur dieses letzte Problem: "Art des Ausdrucks ist mehrdeutig ohne mehr Kontext" in dieser Zeile lassen Sie heruntergeladenePhoto = UIImage (Daten: NSData (contentsOfURL: url)!) – bero

+0

@bero Die Swift 3-Syntax dafür wäre "Lassen Sie heruntergeladenePhoto = UIImage (Daten: Daten (contentsOf: url)) ' – the4kman

+0

es funktioniert, danke! – bero

Verwandte Themen