SwiftMesh 2.2.1

SwiftMesh 2.2.1

jackiehu 维护。



SwiftMesh 2.2.1

  • 作者:
  • HU

Version SPM Xcode 11.0+ iOS 13.0+ Swift 5.0+

SwiftMesh 是基于 Alamofire 和 Codable 的二次封装,使用 Combine 和 Swift Concurrency,支持 SwiftUI 和 UIKit,移除了闭包回调,更加简洁、快速、方便使用。

待完成

  1. 上传
  2. 下载
  3. 表单上传

涉及的设计模式有:适配器、单例等。

使用说明

Swift+UIKit:

import UIKit
import Combine
import SwiftMesh
class ViewController: UIViewController {
    var request = RequestModel()
    private var cancellables: Set<AnyCancellable> = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        request.getAppliances()
        
        request.$cityResult
            .receive(on: RunLoop.main)
            .sink { (model) in
                print("请求数据Model \(String(describing: model))")
         }.store(in: &cancellables)
        
        request.$yesterday
            .receive(on: RunLoop.main)
            .sink { (model) in
                print("请求数据Model \(String(describing: model))")
         }.store(in: &cancellables)
    }
 
}

SwiftUI:

struct SwiftUIView: View {
    @StateObject var request = RequestModel()
    
    var body: some View {
        
        VStack{
            Text("Hello, World!")
            Text(request.cityResult?.message ?? "")
            Text(request.yesterday?.notice ?? "")
        }.onAppear{
            request.getAppliances()
        }
    }
    
}

示例

Mesh:singleton

  • 设置默认参数 — setDefaultParameters

        // MARK: 设置默认参数
        /// 设置默认参数
        /// - Parameter parameters: 默认参数
        public func setDefaultParameters(_ parameters: [String: Any]?) 
  • 设置默认头部信息 — setGlobalHeaders

        // MARK: 设置全局 headers
        /// 设置全局 headers
        /// - Parameter headers:全局 headers
        public func setGlobalHeaders(_ headers: HTTPHeaders?) {
            globalHeaders = headers
        }

Config:适配器

网络请求配置文件用于设置请求超时、请求方法、参数、头部信息、API地址、表单上传等,以及请求完成后返回的响应。

/// Network request configuration
public class MeshConfig {
     //MARK: request related configuration
     /// Timeout configuration
     public var timeout : TimeInterval = 15.0
     /// Add request header
     public var addHeads : HTTPHeaders?
     /// Request method
     public var requestMethod : HTTPMethod = .get
     /// request encoding
     public var requestEncoding: ParameterEncoding = URLEncoding.default //PropertyListEncoding.xml//JSONEncoding.default
     //MARK: request address and parameters
     /// request address
     public var URLString : String?
     /// parameter form upload can also be used
     public var parameters : [String: Any]?
    

请求:解析请求

请根据您的需求自行创建。使用 ObservableObject 以便在 SwiftUI 和 UIKit 之间进行混合开发,并结合 Combine。案例参考Request类

class RequestModel: ObservableObject {
     @MainActor @Published var yesterday: Forecast?

     @MainActor @Published var cityResult: CityResult?
    
     func getAppliances() {
         Task {
             do {
                 // parse all
                 //let data = try await Mesh.shared.request(of: CityResult.self, configClosure: { config in
                 //config.URLString = "http://t.weather.itboy.net/api/weather/city/101030100"
                 // })
                
                
                 // Only parse the required part of the path
                 let data = try await Mesh.shared.request(of: Forecast.self,
                                                          modelKeyPath: "data.yesterday",
                                                          configClosure: { config in
                     config.URLString = "http://t.weather.itboy.net/api/weather/city/101030100"
                 })
                
                 await MainActor. run {
                     self.yesterday = data
                 }
                
             } catch let error {
                 print(error. localizedDescription)
             }
         }
        
     }
}

安装

Cocoapods

  1. 在Podfile中添加pod 'SwiftMesh'

  2. 执行pod installpod update

  3. 导入import SwiftMesh

Swift包管理器

从Xcode 11开始,集成Swift包管理器非常方便。SwiftMesh也支持通过Swift包管理器进行集成。

在Xcode菜单栏中选择文件 > Swift包 > 添加包依赖,并在搜索栏中输入

https://github.com/jackiehu/SwiftMesh,即可完成集成,并默认依赖Alamofire。

手动集成

SwiftMesh还支持手动集成,只需将SwiftMesh文件夹拖入源文件夹中,即可将其集成到所需项目中

更多工具以加速APP开发

ReadMe Card

ReadMe Card

ReadMe Card

ReadMe Card