gpt4 book ai didi

ios - swift 3 : How to convert struct to Parameters

转载 作者:IT王子 更新时间:2023-10-29 05:33:12 26 4
gpt4 key购买 nike

我有一个结构如下

struct UserInfo
{
var userId : Int
var firstName : String
var lastName : String
}

如何序列化 UserInfo 的实例以键入 Parameters

var user = UserInfo(userId: 1, firstName: "John", lastName: "Skew")

// Convert user to Parameters for Alamofire
Alamofire.request("https://httpbin.org/post", parameters: parameters)

最佳答案

只需实现一个dictionaryRepresentation 计算变量或函数:

struct UserInfo {
var userId : Int

var firstName : String
var lastName : String

var dictionaryRepresentation: [String: Any] {
return [
"userId" : userId,
"firstName" : firstName,
"lastName" : lastName
]
}
}

用法:

var user = UserInfo(userId: 1, firstName: "John", lastName: "Skew")
let userDict = user.dictionaryRepresentation

关于ios - swift 3 : How to convert struct to Parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40374356/

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