gpt4 book ai didi

ios - swift 函数返回多个值

转载 作者:行者123 更新时间:2023-11-30 12:24:20 26 4
gpt4 key购买 nike

下面是我尝试创建的一个函数,它将根据输入的键值获取 JSON 数据中的值。我遇到的问题是我需要能够在另一个计算中使用所有费率。我不确定如何将每个速率保存到全局变量中。此时,该函数给我一个错误 - “期望返回字符串的函数中缺少返回”。任何想法如何解决这个问题

   func getLenderData(selectedLenderKey: String) -> String
{

let url = ""
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "GET"

let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)

let task = session.dataTask(with: request) { (data, response, error) in
if error != nil {
print("Error")
}
else{

do{
//let fetchedData = try JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as! NSArray
//print(fecthdata)

let swiftyJSON = try JSON(data:data!)
let lenderName = swiftyJSON["lenders"][selectedLenderKey]["financial_institution"].stringValue
let oneYear = swiftyJSON["lenders"][selectedLenderKey]["one_year"].stringValue
let twoYear = swiftyJSON["lenders"][selectedLenderKey]["two_year"].stringValue
let threeYear = swiftyJSON["lenders"][selectedLenderKey]["three_year"].stringValue
let fourYear = swiftyJSON["lenders"][selectedLenderKey]["four_year"].stringValue
let fiveYear = swiftyJSON["lenders"][selectedLenderKey]["five_year"].stringValue


print(lenderName)
print(oneYear)
print(twoYear)
print(threeYear)
print(fourYear)
print(fiveYear)


}

catch{
print("Error 2")
}
}

}
task.resume()
}// end function

最佳答案

您可以创建一个模型并返回该模型:

struct Rates {
let lenderName: String?
let oneYear: String?
let twoYear: String?
let threeYear: String?
let fourYear: String?
let fiveYear: String?

required init() {
lenderName = nil
oneYear = nil
twoYear = nil
threeYear = nil
fourYear = nil
fiveYear = nil
}

init(lenderName: String?, oneYear: String?, twoYear: String?, threeYear: String?, fourYear: String?, fiveYear: String?) {
self.lenderName = lenderName
self.oneYear = oneYear
self.twoYear = twoYear
self.threeYear = threeYear
self.fourYear = fourYear
self.fiveYear = fiveYear
}
}

然后返回您创建的模型:

func getLenderData(selectedLenderKey: String, onCompletion: @escaping (Rates?) -> Void, onError: @escaping (NSError) -> Void) {
let url = ""
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "GET"

let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)

let task = session.dataTask(with: request) { (data, response, error) in
if error != nil {
print("Error")
}
else{

do{
//let fetchedData = try JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as! NSArray
//print(fecthdata)

let swiftyJSON = try JSON(data:data!)
let lenderName = swiftyJSON["lenders"][selectedLenderKey]["financial_institution"].stringValue
let oneYear = swiftyJSON["lenders"][selectedLenderKey]["one_year"].stringValue
let twoYear = swiftyJSON["lenders"][selectedLenderKey]["two_year"].stringValue
let threeYear = swiftyJSON["lenders"][selectedLenderKey]["three_year"].stringValue
let fourYear = swiftyJSON["lenders"][selectedLenderKey]["four_year"].stringValue
let fiveYear = swiftyJSON["lenders"][selectedLenderKey]["five_year"].stringValue



print(lenderName)
print(oneYear)
print(twoYear)
print(threeYear)
print(fourYear)
print(fiveYear)

let rate = Rates(lenderName: lenderName, oneYear: oneYear, twoYear: twoYear, threeYear: threeYear, fourYear: fourYear, fiveYear: fiveYear)
onCompletion(rate)


}

catch{
print("Error 2")
onError(NSError(domain: "Some error description...", code: 0, userInfo: nil))
}
}

}
task.resume()
}// end function

现在您可以根据需要使用返回值。要使用它,请使用以下代码:

getLenderData(selectedLenderKey: "someVal", onCompletion: { (rate) in
// you can use the rate here
let oneYear = rate.oneYear
}, onError: { (error) in
// Error
})

关于ios - swift 函数返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44380183/

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