Swift 重构:URL 参数增删改查

遇到 url query items 为 nil 的问题,解决方法:对链接进行编码即可:

:
let aString = "链接"
let aUrl = NSURL(string: aString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
DDLog(aUrl?.queryParameters)

URL 参数增删改查

        let absoluteString = "iop://Goods?openNames=30027&activeName=960001"
        switch absoluteString {
        case let value where absoluteString.hasPrefix("iop://Goods"):
            DDLog(value)
            if var URL = URL(string: value) {
                DDLog(URL.queryParameters)
                
                URL = URL.appendingQueryParameters(["token": "1111111"])
//                URL.appendQueryParameters(["token": "1111111"])
                DDLog("append", URL)
                
                URL = URL.removingQueryParameter(for: ["token", "activeName"])
//                URL.removeQueryParameter(for: ["token", "activeName"])
                DDLog("delete", URL)
            }
            
        default:
            break
        }

//testFunc()[line 146]: iop://Goods?openNames=30027&activeName=960001
//testFunc()[line 148]: Optional(["openNames": "30027", "activeName": "960001"])
//testFunc()[line 152]: append, iop://Goods?openNames=30027&activeName=960001&token=1111111
//testFunc()[line 156]: delete, iop://Goods?openNames=30027
import UIKit

public extension URLComponents {
    
    ///查询参数
    var queryParameters: [String: String]? {
        guard let queryItems = queryItems else { return nil }

        var dic = [String: String]()
        for item in queryItems {
            dic[item.name] = item.value
        }
        return dic
    }
    
    ///查询对应参数值
    func queryValue(for key: String) -> String? {
        return queryItems?
            .first(where: { $0.name == key })?
            .value
    }
    
    ///追加查询参数
    mutating func appendingQueryParameters(_ parameters: [String: String]) -> URL {
        queryItems = (queryItems ?? []) + parameters.map { URLQueryItem(name: $0, value: $1) }
        return url!
    }
    
    ///删除查询参数
    mutating func removingQueryParameters(for keys: [String]) -> URL {
        var dic = [String: String]()

        queryItems?
            .filter({ !keys.contains($0.name) })
            .forEach({ dic[$0.name] = $0.value })
        
        queryItems = [] + dic.map { URLQueryItem(name: $0, value: $1) }
        return url!
    }

}


public extension URL {
    
    ///查询参数
    var queryParameters: [String: String]? {
        guard let components = URLComponents(url: self, resolvingAgainstBaseURL: false)
        else { return nil }
        return components.queryParameters
    }
    
    ///查询对应参数值
    func queryValue(for key: String) -> String? {
        return URLComponents(string: absoluteString)?.queryValue(for: key)
    }
    
    ///追加查询参数
    func appendingQueryParameters(_ parameters: [String: String]) -> URL {
        var components = URLComponents(url: self, resolvingAgainstBaseURL: true)!
        return components.appendingQueryParameters(parameters)
    }
    
    ///追加查询参数
    mutating func appendQueryParameters(_ parameters: [String: String]) {
        self = appendingQueryParameters(parameters)
    }
    
    func removingQueryParameters(for keys: [String]) -> URL {
        var components = URLComponents(url: self, resolvingAgainstBaseURL: true)!
        return components.removingQueryParameters(for: keys)
    }
    
    ///删除查询参数
    mutating func removeQueryParameters(for keys: [String]) {
        self = removingQueryParameters(for: keys)
    }
    

}

//@objc public extension NSURLComponents {
//
//    ///查询参数
//    var queryParameters: [String: String]? {
//        return (self as URLComponents).queryParameters
//    }
//    ///追加查询参数
//    func appendingQueryParameters(_ parameters: [String: String]) -> URL {
//        queryItems = (queryItems ?? []) + parameters.map { URLQueryItem(name: $0, value: $1) }
//        return url!
//    }
//
//    ///查询对应参数值
//    func queryValue(for key: String) -> String? {
//        return (self as URLComponents).queryValue(for: key)
//    }
//}


@objc public extension NSURL {
    
    ///查询参数
    var queryParameters: [String: String]? {
        return (self as URL).queryParameters
    }
    
    ///查询对应参数值
    func queryValue(for key: String) -> String? {
        return (self as URL).queryValue(for: key)
    }
    
    ///追加查询参数
    func appendingQueryParameters(_ parameters: [String: String]) -> URL {
        return (self as URL).appendingQueryParameters(parameters)
    }
    
    ///删除查询参数
    func removingQueryParameters(for keys: [String]) -> URL {
        return (self as URL).removingQueryParameters(for: keys)
    }
    
}

你可能感兴趣的:(Swift 重构:URL 参数增删改查)