我一直在浏览
Alamofire
源代码,还有一个代码片段,我无法理解它是如何工作的以及为什么工作的。
if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty {
let percentEncodedQuery = (urlComponents.percentEncodedQuery.map { $0 + "&" } ?? "") + query(parameters)
urlComponents.percentEncodedQuery = percentEncodedQuery
urlRequest.url = urlComponents.url
}
是那个
urlComponents.percentEncodedQuery.map { $0 + "&" } ?? "")
我不明白它是怎么工作的,为什么需要这样。
然后我写了我的片段:
import Foundation
let a: String = "hello world"
a.map { $0 + "&" } //error: binary operator '+' cannot be applied to operands of type 'Character' and 'String'
print(a)
但它在
map
方法。
为什么这不起作用,目的是什么
urlcomponents.percentencodedquery.map{$0+“&”}?“?”
?