gpt4 book ai didi

ios - 在 swift 5.2 con Alamofire 中将数据(json)传递给另一个 Controller

转载 作者:行者123 更新时间:2023-12-01 18:03:27 25 4
gpt4 key购买 nike

我有一个登录 View ,要求输入卡和密码。我咨询了一个 API,如果输入的数据正确,它会向我发送 JSON像这样。哪个返回有按钮方法?如何将该数据发送到另一个 View ?我占用Alamofire 5.0并拥有我的Model类(class)。

@IBAction func myButtonIngresarAction(_ sender: Any) {

guard let carnet = self.txtCarnet.text else {return}
guard let contrasena = self.txtPassword.text else {return}

let ingresologinmodel = IngresoLoginModel(usuario: carnet, password: contrasena)
self.apiCall(IngresoLoginModel: ingresologinmodel){

(result) in

switch result{
case .success(let json):
print(json)
**//This is where I want to send that json with the data to the other view. ******


case .failure(let err):
print(err.localizedDescription)
}
}

}

enum ApiErros: Error {
case custom(message : String)
}

typealias Handler = (Swift.Result<Any?, ApiErros>) -> Void

func apiCall(IngresoLoginModel: IngresoLoginModel, completionHandler: @escaping Handler)
{

let header: HTTPHeaders = [
.contentType("application/json")
]

AF.request("https://url/xxxx/api/Login", method: .post, parameters: IngresoLoginModel,
encoder: JSONParameterEncoder.default, headers: header).response{ response in
debugPrint(response)

switch response.result{
case .success(let data):
do{

let json = try JSONDecoder().decode([LoginModel].self, from: data!)
print(json)

if response.response?.statusCode == 200{
completionHandler(.success(json))

}else{
completionHandler(.failure(.custom(message: "Por favor verifica tu internet")))
}
}
catch
{
print(error)
completionHandler(.failure(.custom(message: "Problemas")))
}
case .failure(let err):
print(err.localizedDescription)
}
}

}
类(class)型号
struct LoginModel: Codable {

let idEmpleado: Int
let Nombre: String
let CodEmpleado: String
let password: String
let idPerfil: Int
let activo: Int
let Descripcion: String
let idRegion: Int
let correo: String

}

This is the json that the Api sends me the data changes them for these example

{
"idEmpleado": 1,
"nombre": “test”,
"codEmpleado": “000000”,
"password": “123”,
"idPerfil": 4,
"activo": 1,
"Descripcion": “test”,
"idregion": 1,
"correo": “test@test.com"
}

最佳答案

很多方法,比如创建一个变量来将此 json 保存在 OtherViewController 并调用 self?.otherViewController.json = json
https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/

关于ios - 在 swift 5.2 con Alamofire 中将数据(json)传递给另一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63386631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com