如何从Swift中的Closure返回值?

Aja*_*ngh 11 twitter closures ios swift twitter-fabric

所以我使用fabric plugin/Twitter-kit在我的应用程序中使用twitter api.我想获得一个名人的个人资料图片的图片网址.这是我的代码如下.

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

}
Run Code Online (Sandbox Code Playgroud)

我不知道如何返回值,因为该方法在闭包执行之前完成.我是新手,很快,任何帮助表示赞赏.

我想在tableview中的cellForRowIndexPath中使用此方法来从imageURl下载图像.

caf*_*ode 19

你需要从@escaping闭包返回.改变功能

func getImageURL(celebrity: String) -> String {

}
Run Code Online (Sandbox Code Playgroud)

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

      // your code
      completion(imgURL)
}
Run Code Online (Sandbox Code Playgroud)

你可以按照下面的说明使用它

getImageURL(celebrity: String) { (imgURL) in

      self.imgURL = imgURL // Using self as the closure is running in background
}
Run Code Online (Sandbox Code Playgroud)

这是一个如何用闭包来编写多个方法来完成的例子.

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")
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

下面是我如何使用它来传递值的示例.

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()
      }
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助任何人寻找相同的解决方案.


Lou*_*nco 7

你是正确的,sendTwitterRequest将在你的函数返回后返回,因此外部函数无法返回imageURL.

相反,在闭包中,获取单元格中所需的返回值并将其存储在某处(在成员变量中)并让tableView自行更新它(例如with tableView.reloadData()).

这将导致它再次获取单元格(cellForRow ...).更改实现以使用存储调用值的成员变量.