Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

ios - 如何从函数内部获取JSON数据并将其快速显示在文本字段上?(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

(我一直在学习如何将json数据解析为swift ...我试图将我的密码区域显示为文本字段...我创建了两个三个swift文件mapview.swift,maphandler.swift和mapmodel.swift.the问题是我想从函数内部获取数据到另一个swift文件,以便我可以在文本字段上显示它...我试图创建一个结构来存储该密码,然后将其传递给mapview.swift,但是没运气)

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

(我要做的就是将那个密码数据发送到mapview.swift,以便我可以在文本字段上显示它)

  ask by Rajat Kotian translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like what you want is your function to return the pin code.

(看起来您想要的是返回PIN码的函数。)

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):

(您可以通过从其他swift文件中调用此函数来使用此函数,如下所示(其中myJsonData是要解码的json数据):)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...