IOS15之swift的Alamofire 5.4框架的网络封装
此Alamofire 版本较高,是基于IOS15 和最新的 Alamofire 5.4.4 版本,截止我发稿的时候,最新版本,网上其他框架的版本较低,api早就淘汰了,或者其他写法,(对回调函数所使用到的关键字@escaping)要重点掌握
后台我采用Java 的springBoot进行封装测试的。
涉及swift基础语法,cocoaPods的使用,swift的闭包,回调,swift5.5语法新特性,单例模式的使用等,类方法的定义,Alamofire 的get,post,put ,delete请求,带参数,或者不带参数,都要拿捏的非常准备到位
//
// NetworkTools.swift
// Alamofire1
//
// Created by lujun on 2021/10/2.
//
import UIKit
import Alamofire
enum MethodType {
case GET
case POST
}
class NetworkTools{
//类方法
class func requestData(type: MethodType,urlString: String, parameters: [String : Any]? = nil,finishedCallback : @escaping ( _ results : Any) -> ()){
// let type1 = type==MethodType.GET ? ".get" : ".post"
if(type==MethodType.GET){
Alamofire.AF.request(urlString).responseJSON {
(response) in
switch response.result {
case .success(let json):
finishedCallback(json)
break
case .failure(