SwiftMesh is a secondary encapsulation based on Alamofire and Codable, uses Combine and Swift Concurrency, supports SwiftUI and UIKit, removes closure callbacks, is more concise, faster, and more convenient to use.
To be completed:
The design patterns involved are: adapter, singleton, etc.
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)
}
}
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()
}
}
}
Set default parameters —setDefaultParameters
// MARK: 设置默认参数
/// 设置默认参数
/// - Parameter parameters: 默认参数
public func setDefaultParameters(_ parameters: [String: Any]?)
Set default header —setGlobalHeaders
// MARK: 设置全局 headers
/// 设置全局 headers
/// - Parameter headers:全局 headers
public func setGlobalHeaders(_ headers: HTTPHeaders?) {
globalHeaders = headers
}
The configuration file of the network request is used to set the request timeout, request method, parameters, header, API address, upload form, etc., and the response returned after the request is completed.
/// 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]?
Please create it yourself based on your usage. Use ObservableObject to facilitate the mixed development of SwiftUI and UIKit, combined with Combine. Use case reference Request class:
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)
}
}
}
}
Add pod 'SwiftMesh'
to Podfile
Execute pod install or pod update
Import import SwiftMesh
Starting from Xcode 11, the Swift Package Manager is integrated, which is very convenient to use. SwiftMesh also supports integration via the Swift Package Manager.
Select File > Swift Packages > Add Pacakage Dependency
in Xcode's menu bar, and enter in the search bar
https://github.com/jackiehu/SwiftMesh
, you can complete the integration and rely on Alamofire by default.
SwiftMesh also supports manual integration, just drag the SwiftMesh folder in the Sources folder into the project that needs to be integrated
link |
Stars: 18 |
Last commit: 5 days ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics