iosjsonswiftfunction

How get JSON data from inside a function and display it on textfield in swift?


I have been learning how to parse JSON data into swift. I am trying to show area of my pincode from into a textfield. I have created two three swift files mapview.swift, maphandler.swift and mapmodel.swift. The problem is I want to get the data from inside the function to another swift file so that I can display it on a textfield. I have tried creating a struct to store that pin code and then pass it on to the mapview.swift but no luck.

This is the code which is able to get pincode as data.

func parseJson(Json:Data) {
    
    let decoder = JSONDecoder()
    do{
        let decodedData = try decoder.decode(MapModel.self, from: Json)
        let format = decodedData.results[0].formatted_address
        var pinCode:String!
        for item in decodedData.results[0].address_components{
            if item.types[0] == "postal_code"{
                pinCode =  ( item.long_name)
                
            }
        }
       
       print(pinCode)

        
    }
    catch{
        
        print(error)
        return 
        
    }
    
    
}

All I want to do is get that pincode data to mapview.swift so that I can display it on a textfield.


Solution

  • It looks like what you want is your function to return the pin code. You can do that as follows:

    func parsePinCode(fromJson json: Data) -> String? {
    
        let decoder = JSONDecoder()
        do {
            let decodedData = try decoder.decode(MapModel.self, from: json)
            let format = decodedData.results[0].formatted_address
            var pinCode:String!
            for item in decodedData.results[0].address_components{
                if item.types[0] == "postal_code"{
                    pinCode = (item.long_name)
    
                    // Return the pinCode if we found one
                    return pinCode
                }
            }
        } catch {
            print(error)
        }
    
        return nil
    }
    

    You can use this function by calling it from other swift files as follows (where myJsonData is the json data you want to decode):

    let pinCode = parsePinCode(fromJson: myJsonData)
    

    EDIT:

    If you want to return multiple values, you could create a struct with all the values you want to return. For example:

    struct MapData {
        var pinCode: String?
        var address: String?
    }
    
    func parsePinCode(fromJson json: Data) -> MapData? {
    
        let decoder = JSONDecoder()
        do {
            let decodedData = try decoder.decode(MapModel.self, from: json)
    
            var mapData = MapData()
    
            mapData.address = decodedData.results[0].formatted_address
    
            for item in decodedData.results[0].address_components {
                if item.types[0] == "postal_code" {
                    mapData.pinCode = (item.long_name)
                    break
                }
            }
    
            return mapData
        } catch {
            print(error)
        }
    
        return nil
    }