{"id":336010,"date":"2022-07-22T09:00:04","date_gmt":"2022-07-22T09:00:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=336010"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=336010","title":{"rendered":"<span>Swift. \u0421\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d55\/9cc\/015\/d559cc015df108b410457b7f949dc38b.png\" width=\"230\" height=\"230\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d55\/9cc\/015\/d559cc015df108b410457b7f949dc38b.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430, \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0441\u043b\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u044d\u0442\u043e \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 sdk \u043b\u0438\u0431\u043e \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041d\u043e \u0435\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b iOS \u0438 \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Swift, \u0442\u043e \u0442\u0443\u0442 \u0436\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u044f\u0441\u043d\u043e, \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u044b\u0434\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f <strong>[String: Any].<\/strong> \u041e\u0434\u043d\u0430\u043a\u043e, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0432 <strong>iOS 15.4<\/strong> \u0438 <strong>Swift 5.6<\/strong> \u0434\u0430\u043d\u043d\u044b\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u0441\u0442\u0430\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c.<\/p>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430<\/h2>\n<ul>\n<li>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 body \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0432\u0438\u0434\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f [String: Any].<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"swift\">let requestParameters = [ \"method\": \"createUser\", \"credentials\": [       \"login\": login,       \"password\": password,       \"email\": email,       \"fullName\": [         \"firstName\": firstName,         \"lastName\": lastName       ]   ] ]<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0433\u0447\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 &#171;\u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0445&#187; \u043c\u043e\u0434\u0435\u043b\u0435\u0439.<\/p>\n<pre><code class=\"swift\">struct LoginRequest: Codable { struct Credentials: Codable {       struct FullName: Codable {         var firstName: String         var lastName: String       }            var login: String       var password: String       var email: String       var fullName: FullName   }      var method: String   var credentials: Credentials }<\/code><\/pre>\n<p>\u0421\u0440\u0430\u0437\u0443 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0443, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0430\u043a \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0432 \u0441\u0435\u0442\u0435\u0432\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0443\u0436\u0435 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0430\u044f \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f Basic authentication, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c access token \u0438 refresh token.<\/p>\n<ul>\n<li>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435 query \u0441\u0442\u0440\u043e\u043a\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"swift\">let requestParameters = [ \"email\": email,   \"firstName\": firstName,   \"lastName\": lastName ]<\/code><\/pre>\n<p>\u0431\u044b\u043b \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0432 \u0441\u0430\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435<\/p>\n<pre><code>email=example@example.com&amp;firstName=Nickey&amp;lastName=Santoro<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043a\u0440\u0438\u043f\u0442\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 (\u043a\u043e\u0433\u0434\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 hash \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438).<\/p>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 body \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/codingkeyrepresentable\" rel=\"noopener noreferrer nofollow\">CodingKeyRepresentable<\/a>, \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0435\u043c\u0443\u0441\u044f \u0432 iOS 15.4 \u0438 \u0442\u0435\u0445\u043d\u0438\u043a\u0435 <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0335-existential-any.md\" rel=\"noopener noreferrer nofollow\">type erasure<\/a>, \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0435\u0439\u0441\u044f \u0432 Swift 5.6 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043b\u043e\u0441\u044c \u044d\u043d\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 Any \u0442\u0438\u043f\u0430.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 &#8212; Query, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u043e \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043b\u043e\u0432\u0430\u0440\u044f.<\/p>\n<pre><code class=\"swift\">import Foundation  public extension Request { struct Query&lt;Key: Encodable> {  \/\/ MARK: - Types  public struct Parameter&lt;K, V> { public var key: K public var value: V }  \/\/ MARK: - Properties  private var elements: [Element]  \/\/ MARK: - Lifecycle  init&lt;S: Sequence>(uniqueKeysWithValues elements: S) where S.Element == (Key, Value) { self.elements = elements.map(Parameter.init) }  \/\/ MARK: - Methods  public subscript(key: Key) -> Value? where Key: Equatable { get { elements.first { $0.key == key }?.value } set { if let index = elements.firstIndex(where: { $0.key == key }) { if let newValue { elements[index].value = newValue } else { elements.remove(at: index) } } else { if let newValue { elements.append(Element(key: key, value: newValue)) } } } } } }  \/\/ MARK: - Extensions  extension Request.Query: ExpressibleByDictionaryLiteral { public typealias Value = any Encodable  public init(dictionaryLiteral elements: (Key, Value)...) { self.elements = elements.map(Parameter.init) } }  extension Request.Query: RangeReplaceableCollection { public init() { self.elements = [] } }  extension Request.Query: Sequence { public typealias Iterator = IndexingIterator&lt;Array&lt;Element>>  public func makeIterator() -> Iterator { return elements.makeIterator() } }  extension Request.Query: Collection { public typealias Element = Parameter&lt;Key, Value> public typealias Index = Int  public var startIndex: Index { return elements.startIndex }  public var endIndex: Int { return elements.endIndex }  public subscript(position: Int) -> Element { return elements[position] }  public func index(after i: Int) -> Int { return elements.index(after: i) } }<\/code><\/pre>\n<p>\u0422\u043e\u0433\u0434\u0430, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u043c CodingKeyRepresentable \u043d\u0443\u0436\u0435\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 CodingKey \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b.<\/p>\n<pre><code class=\"swift\">import Foundation  struct QueryCodingKey: CodingKey { let stringValue: String let intValue: Int?  init(stringValue: String) { self.stringValue = stringValue self.intValue = Int(stringValue) }  init(intValue: Int) { self.stringValue = \"\\(intValue)\" self.intValue = intValue }  init(key: CodingKeyRepresentable) { self.stringValue = key.codingKey.stringValue self.intValue = key.codingKey.intValue } } <\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Encodable \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0437\u0430\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 encode(to:).<\/p>\n<pre><code class=\"swift\">extension Request.Query: Encodable { public func encode(to encoder: Encoder) throws { if Key.self is CodingKeyRepresentable.Type { var container = encoder.container(keyedBy: QueryCodingKey.self)  for element in elements { guard let key = element.key as? CodingKeyRepresentable else { continue }  let codingKey = QueryCodingKey(key: key) try container.encode(element.value, forKey: codingKey) } } else { var container = encoder.unkeyedContainer()  for element in elements { try container.encode(element.key) try container.encode(element.value) } } } }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0448\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u044d\u043d\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 query \u0441\u0442\u0440\u043e\u043a\u0443. \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 bool \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u044d\u0442\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043e\u043f\u0438\u0441\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0432\u0438\u0434\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439.<\/p>\n<pre><code class=\"swift\">public struct QueryEncoding { public enum ArrayEncoding { case enclosingBrackets case surroundingBrackets case noBrackets }  public enum BoolEncoding { case numeric case literal }  public var array: ArrayEncoding public var bool: BoolEncoding  public init(array: QueryEncoding.ArrayEncoding = .enclosingBrackets, bool: QueryEncoding.BoolEncoding = .literal) { self.array = array self.bool = bool } } <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c query \u0441\u0442\u0440\u043e\u043a\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0448\u0442\u0430\u0442\u043d\u044b\u043c\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlcomponents\" rel=\"noopener noreferrer nofollow\">URLComponents<\/a> \u0438 <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlqueryitem\" rel=\"noopener noreferrer nofollow\">URLQueryItem<\/a>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e  \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0432 URLQueryItem \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0432\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434. <\/p>\n<pre><code class=\"swift\">extension Request.Query where Key == String { public func encode(to url: URL, encoding: QueryEncoding) -> URL? { var components = URLComponents(url: url, resolvingAgainstBaseURL: false) components?.queryItems = elements.flatMap { encodeQueryItem(element: $0, encoding: encoding) } return components?.url }  private func encodeQueryItem(element: Element, encoding: QueryEncoding) -> [URLQueryItem] { encodeQueryItem(name: element.key, value: element.value, encoding: encoding) }  private func encodeQueryItem(name: String, value: Any, encoding: QueryEncoding) -> [URLQueryItem] { switch value { case let boolean as Bool: let queryItem = encodeBool(name: name, value: boolean, encoding: encoding) return [queryItem] case let number as NSNumber: let queryItem = encodeNumber(name: name, value: number) return [queryItem] case let array as [Any]: let queryItems = encodeArray(name: name, value: array, encoding: encoding) return queryItems case let dictionary as [String: Any]: let queryItems = encodeDictionary(name: name, value: dictionary, encoding: encoding) return queryItems default: let queryItem = URLQueryItem(name: name, value: \"\\(value)\") return [queryItem] } }  private func encodeBool(name: String, value: Bool, encoding: QueryEncoding) -> URLQueryItem { let stringValue: String  switch encoding.bool { case .numeric: stringValue = (value as NSNumber).stringValue case .literal: stringValue = String(value) }  return URLQueryItem(name: name, value: stringValue) }  private func encodeNumber(name: String, value: NSNumber) -> URLQueryItem { let stringValue = value.stringValue return URLQueryItem(name: name, value: stringValue) }  private func encodeArray(name: String, value: [Any], encoding: QueryEncoding) -> [URLQueryItem] { switch encoding.array { case .enclosingBrackets: return value.flatMap { encodeQueryItem(name: name + \"[]\", value: $0, encoding: encoding) } case .surroundingBrackets: let value = value .flatMap { encodeQueryItem(name: name, value: $0, encoding: encoding) } .compactMap { $0.value } .map { \"\\\"\\($0)\\\"\" } .joined(separator: \",\")  let queryItem = URLQueryItem(name: name, value: \"[\\(value)]\") return [queryItem] case .noBrackets: return value.flatMap { encodeQueryItem(name: name, value: $0, encoding: encoding) } } }  private func encodeDictionary(name: String, value: [String: Any], encoding: QueryEncoding) -> [URLQueryItem] { return value .map { encodeQueryItem(name: name + \"[\\($0)]\", value: $1, encoding: encoding) } .flatMap { $0 } } }<\/code><\/pre>\n<p>\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u044c, \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d constraint?<\/p>\n<pre><code class=\"swift\">where Key == String<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u0443\u0441\u043b\u043e\u0432\u043b\u0435\u043d\u043e \u0442\u0438\u043f\u043e\u043c (String) \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b URLQueryItem.<\/p>\n<h2>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 body \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c<\/p>\n<pre><code class=\"swift\">func createAccount(login: String,     password: String,     email: String,     firstName: String,     lastName: String) async throws {   let url = try createUrl(host: .staging, path: \"api\/v1\/account\/create\")    let headers: [HTTPHeader] = [     .contentType(\"application\/json\"),   ]    let parameters: Request.Query = [     \"method\": \"createUser\",     \"credentials\": [       \"login\": login,       \"password\": password,       \"email\": email,       \"fullName\": [         \"firstName\": firstName,         \"lastName\": lastName       ]     ]   ]    try await dataRequest(     url: url,     method: .post,     headers: headers,     parameters: .body(parameters)   ) }<\/code><\/pre>\n<p>\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f query \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043d\u0438\u0436\u0435<\/p>\n<pre><code class=\"swift\">func updateAccount(id: Int, email: String, firstName: String, lastName: String) async throws {   let url = try createUrl(host: .staging, path: \"api\/v1\/account\/\\(id)\")   let accessToken = try accessToken(for: .staging)    let headers: [HTTPHeader] = [     .authorization(\"Bearer \\(accessToken)\")   ]    let parameters: Request.Query = [     \"email\": email,     \"firstName\": firstName,     \"lastName\": lastName   ]    try await dataRequest(     url: url,     method: .put,     headers: headers,     parameters: .query(parameters)   ) }<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0420\u0430\u043d\u044c\u0448\u0435, \u0434\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f CodingKeyRepresentable \u0438 type erasure, \u0434\u0430\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 AnyEncodable \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043f\u043e\u043b\u0435 key \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u0442\u0438\u043f\u0443 String \u0438\u043b\u0438 Int. \u041e\u0434\u043d\u0430\u043a\u043e \u0441 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0435\u043c \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0439 \u0441\u0442\u0430\u043b\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0438 \u043f\u0440\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u0443\u044e request \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0431\u044b\u0442\u044c.<\/p>\n<ol>\n<li>\n<p> <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/codingkeyrepresentable\" rel=\"noopener noreferrer nofollow\">CodingKeyRepresentable<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0335-existential-any.md\" rel=\"noopener noreferrer nofollow\">Type erasure<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlcomponents\" rel=\"noopener noreferrer nofollow\">URLComponents<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlqueryitem\" rel=\"noopener noreferrer nofollow\">URLQueryItem<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/678304\/\"> https:\/\/habr.com\/ru\/post\/678304\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430, \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0441\u043b\u043e\u0439 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u044d\u0442\u043e \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 sdk \u043b\u0438\u0431\u043e \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041d\u043e \u0435\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b iOS \u0438 \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Swift, \u0442\u043e \u0442\u0443\u0442 \u0436\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u044f\u0441\u043d\u043e, \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0432\u044b\u0434\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f <strong>[String: Any].<\/strong> \u041e\u0434\u043d\u0430\u043a\u043e, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0432 <strong>iOS 15.4<\/strong> \u0438 <strong>Swift 5.6<\/strong> \u0434\u0430\u043d\u043d\u044b\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u0441\u0442\u0430\u043b\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c.<\/p>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430<\/h2>\n<ul>\n<li>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 body \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0432\u0438\u0434\u0435 \u0441\u043b\u043e\u0432\u0430\u0440\u044f [String: Any].<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"swift\">let requestParameters = [ \"method\": \"createUser\", \"credentials\": [       \"login\": login,       \"password\": password,       \"email\": email,       \"fullName\": [         \"firstName\": firstName,         \"lastName\": lastName       ]   ] ]<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0433\u0447\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 &#171;\u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0445&#187; \u043c\u043e\u0434\u0435\u043b\u0435\u0439.<\/p>\n<pre><code class=\"swift\">struct LoginRequest: Codable { struct Credentials: Codable {       struct FullName: Codable {         var firstName: String         var lastName: String       }            var login: String       var password: String       var email: String       var fullName: FullName   }      var method: String   var credentials: Credentials }<\/code><\/pre>\n<p>\u0421\u0440\u0430\u0437\u0443 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0443, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u0430\u043a \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0432 \u0441\u0435\u0442\u0435\u0432\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0443\u0436\u0435 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0430\u044f \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u044f Basic authentication, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c access token \u0438 refresh token.<\/p>\n<ul>\n<li>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435 query \u0441\u0442\u0440\u043e\u043a\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"swift\">let requestParameters = [ \"email\": email,   \"firstName\": firstName,   \"lastName\": lastName ]<\/code><\/pre>\n<p>\u0431\u044b\u043b \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u0432 \u0441\u0430\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435<\/p>\n<pre><code>email=example@example.com&amp;firstName=Nickey&amp;lastName=Santoro<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043a\u0440\u0438\u043f\u0442\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 (\u043a\u043e\u0433\u0434\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 hash \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438).<\/p>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 body \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0443 <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/codingkeyrepresentable\" rel=\"noopener noreferrer nofollow\">CodingKeyRepresentable<\/a>, \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0435\u043c\u0443\u0441\u044f \u0432 iOS 15.4 \u0438 \u0442\u0435\u0445\u043d\u0438\u043a\u0435 <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0335-existential-any.md\" rel=\"noopener noreferrer nofollow\">type erasure<\/a>, \u043f\u043e\u044f\u0432\u0438\u0432\u0448\u0435\u0439\u0441\u044f \u0432 Swift 5.6 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043b\u043e\u0441\u044c \u044d\u043d\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 Any \u0442\u0438\u043f\u0430.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0434\u043b\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 &#8212; Query, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u043e \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043b\u043e\u0432\u0430\u0440\u044f.<\/p>\n<pre><code class=\"swift\">import Foundation  public extension Request { struct Query&lt;Key: Encodable> {  \/\/ MARK: - Types  public struct Parameter&lt;K, V> { public var key: K public var value: V }  \/\/ MARK: - Properties  private var elements: [Element]  \/\/ MARK: - Lifecycle  init&lt;S: Sequence>(uniqueKeysWithValues elements: S) where S.Element == (Key, Value) { self.elements = elements.map(Parameter.init) }  \/\/ MARK: - Methods  public subscript(key: Key) -> Value? where Key: Equatable { get { elements.first { $0.key == key }?.value } set { if let index = elements.firstIndex(where: { $0.key == key }) { if let newValue { elements[index].value = newValue } else { elements.remove(at: index) } } else { if let newValue { elements.append(Element(key: key, value: newValue)) } } } } } }  \/\/ MARK: - Extensions  extension Request.Query: ExpressibleByDictionaryLiteral { public typealias Value = any Encodable  public init(dictionaryLiteral elements: (Key, Value)...) { self.elements = elements.map(Parameter.init) } }  extension Request.Query: RangeReplaceableCollection { public init() { self.elements = [] } }  extension Request.Query: Sequence { public typealias Iterator = IndexingIterator&lt;Array&lt;Element>>  public func makeIterator() -> Iterator { return elements.makeIterator() } }  extension Request.Query: Collection { public typealias Element = Parameter&lt;Key, Value> public typealias Index = Int  public var startIndex: Index { return elements.startIndex }  public var endIndex: Int { return elements.endIndex }  public subscript(position: Int) -> Element { return elements[position] }  public func index(after i: Int) -> Int { return elements.index(after: i) } }<\/code><\/pre>\n<p>\u0422\u043e\u0433\u0434\u0430, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u043e\u043c CodingKeyRepresentable \u043d\u0443\u0436\u0435\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 CodingKey \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b.<\/p>\n<pre><code class=\"swift\">import Foundation  struct QueryCodingKey: CodingKey { let stringValue: String let intValue: Int?  init(stringValue: String) { self.stringValue = stringValue self.intValue = Int(stringValue) }  init(intValue: Int) { self.stringValue = \"\\(intValue)\" self.intValue = intValue }  init(key: CodingKeyRepresentable) { self.stringValue = key.codingKey.stringValue self.intValue = key.codingKey.intValue } } <\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Encodable \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0437\u0430\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 encode(to:).<\/p>\n<pre><code class=\"swift\">extension Request.Query: Encodable { public func encode(to encoder: Encoder) throws { if Key.self is CodingKeyRepresentable.Type { var container = encoder.container(keyedBy: QueryCodingKey.self)  for element in elements { guard let key = element.key as? CodingKeyRepresentable else { continue }  let codingKey = QueryCodingKey(key: key) try container.encode(element.value, forKey: codingKey) } } else { var container = encoder.unkeyedContainer()  for element in elements { try container.encode(element.key) try container.encode(element.value) } } } }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0448\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u044d\u043d\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 query \u0441\u0442\u0440\u043e\u043a\u0443. \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 bool \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u044d\u0442\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043e\u043f\u0438\u0441\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0432\u0438\u0434\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439.<\/p>\n<pre><code class=\"swift\">public struct QueryEncoding { public enum ArrayEncoding { case enclosingBrackets case surroundingBrackets case noBrackets }  public enum BoolEncoding { case numeric case literal }  public var array: ArrayEncoding public var bool: BoolEncoding  public init(array: QueryEncoding.ArrayEncoding = .enclosingBrackets, bool: QueryEncoding.BoolEncoding = .literal) { self.array = array self.bool = bool } } <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c query \u0441\u0442\u0440\u043e\u043a\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0448\u0442\u0430\u0442\u043d\u044b\u043c\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlcomponents\" rel=\"noopener noreferrer nofollow\">URLComponents<\/a> \u0438 <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlqueryitem\" rel=\"noopener noreferrer nofollow\">URLQueryItem<\/a>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e  \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0432 URLQueryItem \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0432\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434. <\/p>\n<pre><code class=\"swift\">extension Request.Query where Key == String { public func encode(to url: URL, encoding: QueryEncoding) -> URL? { var components = URLComponents(url: url, resolvingAgainstBaseURL: false) components?.queryItems = elements.flatMap { encodeQueryItem(element: $0, encoding: encoding) } return components?.url }  private func encodeQueryItem(element: Element, encoding: QueryEncoding) -> [URLQueryItem] { encodeQueryItem(name: element.key, value: element.value, encoding: encoding) }  private func encodeQueryItem(name: String, value: Any, encoding: QueryEncoding) -> [URLQueryItem] { switch value { case let boolean as Bool: let queryItem = encodeBool(name: name, value: boolean, encoding: encoding) return [queryItem] case let number as NSNumber: let queryItem = encodeNumber(name: name, value: number) return [queryItem] case let array as [Any]: let queryItems = encodeArray(name: name, value: array, encoding: encoding) return queryItems case let dictionary as [String: Any]: let queryItems = encodeDictionary(name: name, value: dictionary, encoding: encoding) return queryItems default: let queryItem = URLQueryItem(name: name, value: \"\\(value)\") return [queryItem] } }  private func encodeBool(name: String, value: Bool, encoding: QueryEncoding) -> URLQueryItem { let stringValue: String  switch encoding.bool { case .numeric: stringValue = (value as NSNumber).stringValue case .literal: stringValue = String(value) }  return URLQueryItem(name: name, value: stringValue) }  private func encodeNumber(name: String, value: NSNumber) -> URLQueryItem { let stringValue = value.stringValue return URLQueryItem(name: name, value: stringValue) }  private func encodeArray(name: String, value: [Any], encoding: QueryEncoding) -> [URLQueryItem] { switch encoding.array { case .enclosingBrackets: return value.flatMap { encodeQueryItem(name: name + \"[]\", value: $0, encoding: encoding) } case .surroundingBrackets: let value = value .flatMap { encodeQueryItem(name: name, value: $0, encoding: encoding) } .compactMap { $0.value } .map { \"\\\"\\($0)\\\"\" } .joined(separator: \",\")  let queryItem = URLQueryItem(name: name, value: \"[\\(value)]\") return [queryItem] case .noBrackets: return value.flatMap { encodeQueryItem(name: name, value: $0, encoding: encoding) } } }  private func encodeDictionary(name: String, value: [String: Any], encoding: QueryEncoding) -> [URLQueryItem] { return value .map { encodeQueryItem(name: name + \"[\\($0)]\", value: $1, encoding: encoding) } .flatMap { $0 } } }<\/code><\/pre>\n<p>\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u044c, \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d constraint?<\/p>\n<pre><code class=\"swift\">where Key == String<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u0443\u0441\u043b\u043e\u0432\u043b\u0435\u043d\u043e \u0442\u0438\u043f\u043e\u043c (String) \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b URLQueryItem.<\/p>\n<h2>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 body \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c<\/p>\n<pre><code class=\"swift\">func createAccount(login: String,     password: String,     email: String,     firstName: String,     lastName: String) async throws {   let url = try createUrl(host: .staging, path: \"api\/v1\/account\/create\")    let headers: [HTTPHeader] = [     .contentType(\"application\/json\"),   ]    let parameters: Request.Query = [     \"method\": \"createUser\",     \"credentials\": [       \"login\": login,       \"password\": password,       \"email\": email,       \"fullName\": [         \"firstName\": firstName,         \"lastName\": lastName       ]     ]   ]    try await dataRequest(     url: url,     method: .post,     headers: headers,     parameters: .body(parameters)   ) }<\/code><\/pre>\n<p>\u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f query \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043d\u0438\u0436\u0435<\/p>\n<pre><code class=\"swift\">func updateAccount(id: Int, email: String, firstName: String, lastName: String) async throws {   let url = try createUrl(host: .staging, path: \"api\/v1\/account\/\\(id)\")   let accessToken = try accessToken(for: .staging)    let headers: [HTTPHeader] = [     .authorization(\"Bearer \\(accessToken)\")   ]    let parameters: Request.Query<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-336010","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336010"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/336010\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}