ios开发之--swift下Alamofire的使用

1,首先使用cocoapods导入,如果有不会的同学,可以去看我写的关于cocopods使用的那篇博客

2,直接上代码:

a 先看下文件结构

 

CommonFile.swift

import UIKit

let HGL_ScreenHeight = UIScreen.main.bounds.size.height
let HGL_ScreenWidth = UIScreen.main.bounds.size.width

NetWorkTools.swift

import UIKit
import Alamofire

enum MethodType:NSInteger {
    case GET
    case POST
}

class NetWorkTools: NSObject {
    class func requestData(_ type : MethodType, URLString : String, parameters : [String : Any]? = nil, finishedCallback : @escaping (_ result : Any) -> ()) {
        //1.获取类型
        let method = type == .GET ? HTTPMethod.get : HTTPMethod.post
        
        // 2.发送网络请求
        Alamofire.request(URLString, method: method, parameters: parameters).responseJSON { (response) in
            // 3.获取结果
            guard let result = response.result.value else {
                print(response.result.error!)
                return
                
            }
            // 4.将结果回调出去
            finishedCallback(result)
            
        }
        
    }

}

3,ViewController.swift:

NetWorkTools.requestData(.POST, URLString: "http://www.chexijie.com/appapi/api/gethelps", parameters: [" category_id":"10"]) { (result) in
            print(result)
        }

 打印如下:

 非常感恩,大神们提供的框架!

原文地址:https://www.cnblogs.com/hero11223/p/7196535.html