【发布时间】:2020-08-04 19:39:07
【问题描述】:
我创建的 URL 有效,在 Chrome 上测试时会返回所需的 JSON,但在我的项目中失败。
func createWikipediaURL(place: String) -> URL? {
let _place = place.replacingOccurrences(of: " ", with: "%20")
let urlStr =
"https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts|pageimages&exintro&explaintext&generator=search&gsrsearch=intitle:\(_place)&gsrlimit=1&redirects=1"
if let url = URL(string:urlStr) {
return url
} else {
return nil
}
}
使用参数“Malibu Beach”,该函数将创建正确的 URL,https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts|pageimages&exintro&explaintext&generator=search&gsrsearch=intitle:Malibu%20Beach&gsrlimit=1&redirects=1,但也将导致不返回任何 URL,因为该字符串无法转换为 URL。关于如何将字符串变成 URL 的任何建议?
【问题讨论】: