This is what i am trying to accomplish:
I dont know what the best way to accomplish this is, im so lost! Can i somehow return the variable "parameters" out of the function so that i can modify it and send it out with another function?
The below code is not the full code, i removed a big part to make it easier to read. It contains a "get" function, dont know where to go from here.
Thanks for checking it out, just looking for someone to point me in the right direction.
import UIKit
import AVFoundation
import QRCodeReader
import Alamofire
// MARK: - Main
struct Main: Codable {
let objects: [Object]
}
// MARK: - Object
struct Object: Codable {
let inventory: [Inventory]
}
// MARK: - Inventory
struct Inventory: Codable {
let coordinates: String
}
func test() {
// MARK: - LOGIN DETAILS
let headers: HTTPHeaders = [
.authorization(username: "username", password: "password"),
.accept("application/json")]
// MARK: - API REQUEST
AF.request("URL LINK", headers: headers).validate().responseJSON { response in
// MARK: - CHECK FOR ERRORS
if let error = response.error {
print(error)
// MARK: - ERROR MESSAGE
DispatchQueue.main.async
{ let alert = UIAlertController(title: "ERROR", message: "Something went wrong", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)}
return}
// MARK: - PRINT RESPONSE
if response.response != nil
{print (response.response!) }
// MARK: - PRINT DATA
if response.data != nil
{ print(response.data!)
let decoder = JSONDecoder()
do
{
let api = try decoder.decode(Main.self, from: response.data!)
let parameters = [
"meta": [
"language": "en"
],
"request": [
"inventory": [
[
"id": 6534206,
"warehouseid": 1,
"instock": 3,
"threshold": 0,
"reserved": 0,
"coordinates": "\(api.objects[0].inventory[0].coordinates)",
"note": "",
"prodno": 25669,
"soldout": 0
]
]
]
]
}
catch {
print("Error in JSON parsing")
}
}
}
}
You can use the closure to return the value out of the function. This practice is functional programming, almost using for async function.
func test(completion: ([String: AnyObject]) -> ()) {
...
let param = [“meta”: [“language”: “en”],...]
completion(param)
}
And then, to use it:
test(completion: { param in
// handle the “param”
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.