gpt4 book ai didi

ios - 如何在 Swift 中对 URL 进行编码

转载 作者:行者123 更新时间:2023-11-29 05:22:11 24 4
gpt4 key购买 nike

这是我的URL .

问题是 address字段未附加到 urlpath .

有谁知道这是为什么吗?

var address:string
address = "American Tourister, Abids Road, Bogulkunta, Hyderabad, Andhra Pradesh, India"
let urlpath = NSString(format: "http://maps.googleapis.com/maps/api/geocode/json?address="+"\(address)")

最佳答案

swift 4.2

var urlString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

swift 3.0

var address = "American Tourister, Abids Road, Bogulkunta, Hyderabad, Andhra Pradesh, India"
let escapedAddress = address.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
let urlpath = String(format: "http://maps.googleapis.com/maps/api/geocode/json?address=\(escapedAddress)")

使用stringByAddingPercentEncodingWithAllowedCharacters :

var escapedAddress = address.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
<小时/>

使用stringByAddingPercentEscapesUsingEncoding: 在 iOS 9 和 OS X v10.11 中已弃用

var address = "American Tourister, Abids Road, Bogulkunta, Hyderabad, Andhra Pradesh, India"
var escapedAddress = address.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let urlpath = NSString(format: "http://maps.googleapis.com/maps/api/geocode/json?address=\(escapedAddress)")

关于ios - 如何在 Swift 中对 URL 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578561/

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