• 闭包 尾随闭包


    嵌套函数:
    这里写图片描述
    闭包:
    这里写图片描述

    import UIKit
    import AFNetworking
    import MBProgressHUD
    import Toast
    
    struct NetManager {
    
        static let URLStringBooks = "https://api.douban.com/v2/book/search"
        static let KeyBooks = "books"
        static let netError = "网络异常,请检查网络"
        static let pageSize = 10
    
        static func getBooks(tag:String, page:Int,resultClosure:(Bool,[Book]!) -> Void) {
            NetManager.GET(URLStringBooks, parameters: ["tag":tag,"start":page * pageSize,"count":pageSize],showHUD: false, success: { (responseObject) -> Void in
                    var books = [Book]()
                    if let dict = responseObject as? [String:NSObject],array = dict[self.KeyBooks] as? [[String:NSObject]] {
                        for dict in array {
                            books.append(Book(dict: dict))
                        }
                    }
                    resultClosure(true,books)
                }) { (error) -> Void in
                    resultClosure(false,nil)
            }
    
        }
    
        static func getBookTitles(tag:String, page:Int,resultClosure:([String]) -> Void) {
            NetManager.GET(URLStringBooks, parameters: ["tag":tag,"start":0,"count":10,"fields":"title"],showHUD: false, success: { (responseObject) -> Void in
                    var searchTitles = [String]()
                    if let dict = responseObject as? [String:NSObject],array = dict[self.KeyBooks] as? [[String:NSObject]] {
                        for dict in array {
                            if let title = dict["title"] as? String {
                                searchTitles.append(title)
                            }
                        }
                    }
                    resultClosure(searchTitles)
                }) { (error) -> Void in
                }
        }
    
        static func GET(URLString:String, parameters:[String:NSObject]?, showHUD:Bool = true, success:((NSObject?) -> Void)?, failure:((NSError) -> Void)?) {
    
            let manager = AFHTTPSessionManager()
            manager.requestSerializer.timeoutInterval = 10
    
            let mainWindow = UIApplication.sharedApplication().delegate!.window!
    
            if showHUD {
                MBProgressHUD.showHUDAddedTo(mainWindow, animated: true)
            }
    
            manager.GET(URLString, parameters: parameters, success: { (task, responseObject) -> Void in
                if showHUD {
                    MBProgressHUD.hideAllHUDsForView(mainWindow, animated: true)
                }
                success?(responseObject as? NSObject)
                }) { (task, error) -> Void in
                    if showHUD {
                        MBProgressHUD.hideAllHUDsForView(mainWindow, animated: true)
                        mainWindow?.makeToast(netError)
                    }
                    failure?(error)
            }
    
        }
    
        static func POST(URLString:String, parameters:[String:NSObject]?, showHUD:Bool = true, success:((NSObject?) -> Void)?, failure:((NSError) -> Void)?) {
    
            let manager = AFHTTPSessionManager()
            manager.requestSerializer.timeoutInterval = 10
    
            let mainWindow = UIApplication.sharedApplication().delegate!.window!
    
            if showHUD {
                MBProgressHUD.showHUDAddedTo(mainWindow, animated: true)
            }
    
            manager.POST(URLString, parameters: parameters, success: { (task, responseObject) -> Void in
                if showHUD {
                    MBProgressHUD.hideAllHUDsForView(mainWindow, animated: true)
                }
                success?(responseObject as? NSObject)
                }) { (task, error) -> Void in
                    if showHUD {
                        MBProgressHUD.hideAllHUDsForView(mainWindow, animated: true)
                        mainWindow?.makeToast(netError)
                    }
                    failure?(error)
            }
        }
    }
    

    调用:

       NetManager.getBooks(self.tag, page: 0, resultClosure: { (result, books) -> Void in
                    self.tableView.headerEndRefresh()
                    if result {
                        self.page = 1
                        self.books = books
                        self.tableView.reloadData()
                    } else {
                        self.view.makeToast("请求数据失败")
                    }
                })
  • 相关阅读:
    R-CNN算法中NMS的具体做法
    Spring之Environment
    Spring之Aware
    每晚夜里自我独行,随处荡,多冰冷,以往为了自我挣扎
    Java 反射机制
    Java string String
    Java int Integer
    Java final
    Java toString()方法
    Java Enum
  • 原文地址:https://www.cnblogs.com/melons/p/5791905.html
Copyright © 2020-2023  润新知