IOS15之swift的Alamofire 5.4框架的网络封装
生活随笔
收集整理的這篇文章主要介紹了
IOS15之swift的Alamofire 5.4框架的网络封装
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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請求,帶參數,或者不帶參數,都要拿捏的非常準備到位
測試代碼如下:
// // ViewController.swift // Alamofire1 // // Created by lujun on 2021/10/2. //import UIKit import Alamofireclass ViewController: UIViewController {/*AF.request("https://httpbin.org/get").responseJSON{ (response) inguard let result = response.value else{print(response.error ?? "")return}print(result)}*/override func viewDidLoad() {super.viewDidLoad()NetworkTools.requestDataWithParam(type: .GET, urlString: "http://localhost:8081/demo3",parameters: ["sn":"lujun"]) { result inprint(result)}}func getRequest(){let url = "https://httpbin.org/get"Alamofire.AF.request(url).responseJSON { (response) inswitch response.result {case .success(let json):print(json)// Success in request and do a async or sync(NOT UI) task here.breakcase .failure(let error):print("error:\(error)")break}}}func postRequest(){let url = "https://httpbin.org/post"Alamofire.AF.request(url, method: .post, parameters: ["name":"jack","password":"123456"]).responseJSON { (response) inswitch response.result {case .success(let json):print(json)breakcase .failure(let error):print("error:\(error)")break}}}}Java 后臺
package com.example.demo.controller; import com.example.demo.dao.User; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController public class DemoController {@GetMapping(value = "/demo")public String test2(String sn){System.out.println(sn);return "test";}@GetMapping(value = "/demo3")public Object test23(String sn){Map m1 = new HashMap<String,Object>();System.out.println(sn);User user = new User("lujun", 18, "123@qq.com");m1.put("u1",user);return m1;} }項目文件名在我的資源文件下。名稱是Alamofire1.zip
總結
以上是生活随笔為你收集整理的IOS15之swift的Alamofire 5.4框架的网络封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 264,avs重要的变量:
- 下一篇: 软件测试不是一个功能