如何在JSON Swift中传递URL正文中的两个值?解决

问题描述 投票:0回答:1

我正在尝试在网址正文中传递两个值

它运行完美:在这里,我给出了requestedKey值字符串

let urlStr = "http://itaag-env-1-south-1.elasticbeanstalk.com/getprofile/?requestedKey=" + "9609cc826b0d472faf9967370c095c21&requestedUserType=personal"

这里我已存储requestedKey value in personalId

let personalId: String = UserDefaults.standard.string(forKey: "USERID") ?? ""

[如果我尝试如下所示显示错误:个性值仍然显示为空

let urlStr  = "http://itaag-env-1-south-1.elasticbeanstalk.com/getprofile/?requestedUserType"  + "personal & requestedKey =" + personalId

[请帮助我解决此错误。

json swift api url
1个回答
0
投票

这应该如何创建具有不同组件的URL:

let string = "http://itaag-env-1-south-1.elasticbeanstalk.com/getprofile"
var urlComponents = URLComponents(string: string)
let requestedItem = URLQueryItem(name: "requestedKey", value: "yourReqKey")
let requestedUserType = URLQueryItem(name: "requestedUserType", value: "personal")
urlComponents?.queryItems = [requestedItem, requestedUserType]
let url = urlComponents?.url
print(url?.absoluteString)
© www.soinside.com 2019 - 2024. All rights reserved.