2016-07-10 43 views
1

因此,我使用fabric插件/ Twitter-kit在我的應用程序中使用twitter api。我想獲取名人個人資料圖片的圖片網址。這裏是我的代碼如下。如何從Swift中的Closure返回值?

func getImageURL(celebrity :String) -> String{ 

    var imageURL = "" 
    let client = TWTRAPIClient() 
    let statusesShowEndpoint = userSearch 
    let params = ["q": celebrity, 
        "page" : "1", 
        "count" : "1" 
        ] 
    var clientError : NSError? 

    let request = client.URLRequestWithMethod("GET", URL: statusesShowEndpoint, parameters: params, error: &clientError) 

    client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in 
     if connectionError != nil { 
      print("Error: \(connectionError)") 
     } 

     do { 
      let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) 
      print("json: \(json)") 

      let profileImageURL: String? = json[0].valueForKey("profile_image_url_https") as? String 

      imageURL = self.cropTheUrlOfImage(profileImageURL!) 
      print(imageURL) 

      //how do i return the value back. confused ????? 
      return imageURL 

     } catch let jsonError as NSError { 
      print("json error: \(jsonError.localizedDescription)") 
     } 
    } 

    return imageURL 

} 

我不知道如何返回值,因爲方法在閉包完全執行之前完成。我是新來的迅速,任何幫助表示讚賞。

我想在tableview的cellForRowIndexPath中使用這個方法來下載imageURl中的圖像。

+0

請參閱[如何從Swift中的void閉包返回值?](http://stackoverflow.com/questions/37798352/how-to-return-a-value-from-a-void-closure-in -迅速)。 – OOPer

+0

http://stackoverflow.com/questions/38160959/void-inside-of-return-function/38161092#38161092 – penatheboss

回答

4

你是對的,sendTwitterRequest將在你的函數返回後返回,所以外函數沒有辦法返回一個imageURL。

取而代之,在閉包中,將單元格中的所需返回值存儲到某處(在成員變量中)並讓tableView自己更新它(例如使用tableView.reloadData())。

這將導致它再次獲取單元格(cellForRow ...)。將實現更改爲使用存儲該調用值的成員變量。

0

您需要從@escaping閉包返回。 改變功能

func getImageURL(celebrity: String) -> String { 

} 

func getImageURL(celebrity: String, completion:(String)->()) { 

     // your code 
     completion(imgURL) 
} 

你可以用它下面

getImageURL(celebrity: String) { (imgURL) in 

     self.imgURL = imgURL // Using self as the closure is running in background 
} 

給出下面是我寫的怎麼帶密封蓋完成多種方法的例子。

class ServiceManager: NSObject { 

// Static Instance variable for Singleton 
static var sharedSessionManager = ServiceManager() 

// Function to execute GET request and pass data from escaping closure 
func executeGetRequest(with urlString: String, completion: @escaping (Data?) ->()) { 

    let url = URL.init(string: urlString) 
    let urlRequest = URLRequest(url: url!) 

    URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in 
     // Log errors (if any) 
     if error != nil { 
      print(error.debugDescription) 
     } else { 
      // Passing the data from closure to the calling method 
      completion(data) 
     } 
    }.resume() // Starting the dataTask 
} 

// Function to perform a task - Calls executeGetRequest(with urlString:) and receives data from the closure. 
func downloadMovies(from urlString: String, completion: @escaping ([Movie]) ->()) { 
    // Calling executeGetRequest(with:) 
    executeGetRequest(with: urlString) { (data) in // Data received from closure 
     do { 
      // JSON parsing 
      let responseDict = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] 
      if let results = responseDict!["results"] as? [[String:Any]] { 
       var movies = [Movie]() 
       for obj in results { 
        let movie = Movie(movieDict: obj) 
        movies.append(movie) 
       } 
       // Passing parsed JSON data from closure to the calling method. 
       completion(movies) 
      } 
     } catch { 
      print("ERROR: could not retrieve response") 
     } 
    } 
    } 
} 

以下是我如何使用它來傳遞值的示例。

ServiceManager.sharedSessionManager.downloadMovies(from: urlBase) { (movies : [Movie]) in // Object received from closure 
     self.movies = movies 
     DispatchQueue.main.async { 
      // Updating UI on main queue 
      self.movieCollectionView.reloadData() 
     } 
} 

我希望這可以幫助任何人尋找相同的解決方案。