gpt4 book ai didi

swift - 使用 Result 调用函数

转载 作者:行者123 更新时间:2023-11-28 14:46:30 27 4
gpt4 key购买 nike

我有这个代码:

enum Result<T> {
case succes(T)
case error(String)
}
typealias completionHandler = (Result<Data >) -> ()
func getJsonFromServer(parameters: String, completion: @escaping completionHandler) {
let fullUrlString = ApiConstans.fullPath + parameters
guard let url = URL(string: fullUrlString) else {
return completion(.error("Error 100: Problem with url"))
}

URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil else {
return completion(.error("Error 101: Problem with data"))
}

guard let data = data else {
return completion(.error("Error 102: Problem with data"))
}

debugPrint("R> \(fullUrlString)")
return completion(.succes(data))
}.resume()
}

func checkUsersLogin(login: String?, password: String?, completion: @escaping completionHandler) {
self.getJsonFromServer(parameters: "?action=LOGOWANIE&login=\(login!)&password=\(password!)", completion: completion)
}

如何调用?

我试试这个:

@IBAction func btnLoginPressed(_ sender: Any) {
if self.textFieldLogin.text?.isEmpty ?? true || self.textFieldPassword.text?.isEmpty ?? true {
self.errorLoginMessage(txt: "Error", title: "Error")
} else {
cms.checkUsersLogin(login: self.textFieldLogin.text, password: self.textFieldPassword.text, completion: { (data) in
if data.error == nil, let data = data {
do {
let decoder = JSONDecoder()
loggedUser = try decoder.decode(LoginUser.self, from: data)

if ((loggedUser?.id ) == nil) {
let jsonValues = try? JSONSerialization.jsonObject(with: data, options: [])
if let downloadJson = jsonValues as? [String: Any], let message = downloadJson["komunikat"] as? String, let title = downloadJson["error"] as? String {
DispatchQueue.main.async {
self.errorLoginMessage(txt: message, title: title)
}
} else {
DispatchQueue.main.async {
self.errorLoginMessage(txt: "Podany login lub hasło jest błędny!!", title: "Błąd")
}
}
} else {
DispatchQueue.main.async {
dump(loggedUser)
self.performSegue(withIdentifier: "toLoginUser", sender: self)
}
}
}
catch {
print("Error in decoder")
}

} else {
print("Error 104: \(error)")
}


})
}
}

但我有以下错误:枚举元素 'error' 不能作为实例成员引用:如果 data.error == nil,让 data = data {

和使用未解析的标识符“error”:print("Error 104: (error)")

我可以请你修复这段代码吗?

最佳答案

像这样更改您的代码:

@IBAction func btnLoginPressed(_ sender: Any) {
if self.textFieldLogin.text?.isEmpty ?? true || self.textFieldPassword.text?.isEmpty ?? true {
self.errorLoginMessage(txt: "Error", title: "Error")
} else {
cms.checkUsersLogin(login: self.textFieldLogin.text, password: self.textFieldPassword.text, completion: { (data) in

switch(data) {
case .success(let data):
do {
let decoder = JSONDecoder()
loggedUser = try decoder.decode(LoginUser.self, from: data)

if ((loggedUser?.id ) == nil) {
let jsonValues = try? JSONSerialization.jsonObject(with: data, options: [])
if let downloadJson = jsonValues as? [String: Any], let message = downloadJson["komunikat"] as? String, let title = downloadJson["error"] as? String {
DispatchQueue.main.async {
self.errorLoginMessage(txt: message, title: title)
}
} else {
DispatchQueue.main.async {
self.errorLoginMessage(txt: "Podany login lub hasło jest błędny!!", title: "Błąd")
}
}
} else {
DispatchQueue.main.async {
dump(loggedUser)
self.performSegue(withIdentifier: "toLoginUser", sender: self)
}
}
}
catch {
print("Error in decoder")
}
case .error(let error):
print("Error 104: \(error)")
}

})
}
}

关于swift - 使用 Result<T> 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50209722/

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