如何解决下面示例代码中发生的泄漏,如下图所示:
import UIKit
import Alamofire
import AlamofireObjectMapper
class ViewController: UIViewController {
@IBAction func action(_ sender: Any) {
load { (posts: [Post]) in
posts.map({ (post: Post) -> Void in
print(post.title)
})
}
}
func load( posts: (([Post]) -> Void)!) {
let urlString = "https://jsonplaceholder.typicode.com/posts"
let url = try! urlString.asURL()
Alamofire.request(url).validate().responseArray { (response: DataResponse<[Post]>) in
guard let postArray = response.result.value else {return}
posts(postArray)
}
}
}