URLComponentsBuilder
一个静态库,提供了URLComponents的构建器模式以简化设置查询参数
将'Dictionary'转换为'URLQueryItem(s)'。简化了设置查询参数,避免了在URL查询中表示数组和字典的繁琐任务。
安装
Cocoa Pods
需要Cocoa Pods 1.5.0或更高版本。
target '<you project target>' do
pod 'URLComponentsBuilder'
end
或者直接将此类文件复制到您的项目中
URLComponentsBuilder.swift
使用示例
let query: [String: Any] = [
"name": "Tony",
"username": "Stark",
"password": "%&34",
"isSuperhero": true,
"weightKg": 75.8,
"phones": ["mobile": "123456789",
"office": "123987456"],
"homes": [["name": "Point Dume", "address": "10880 Malibu Point"],
["name": "Stark Tower", "address": "200 Park Avenue"]],
"belongings": ["cars": ["1932 Ford Flathead Roadster",
"1967 Shelby Cobra",
"Saleen S7"],
"motorcycles": ["zero eng type6"]]]
var urlComponents = URLComponentsBuilder()
.setScheme("http")
.setHost("superherobuilder.com")
.setPath("/buildSuit/")
.addQuery(items: query)
.build()
print(urlComponents.url!.absoluteString)
输出:[链接](http://superherobuilder.com/buildSuit/?belongings%5Bcars%5D%5B0%5D=1932 "福特Flathead敞篷车"); [链接](http://superherobuilder.com/buildSuit/?belongings[cars][0]=1932&belongings[cars][1]=1967 Shelby Cobra&belongings[cars][2]=Saleen S7&belongings[motorcycles][0]=zero eng type6&homes[0][address]=10880 Malibu Point&homes[0][name]=Point Dume&homes[1][address]=200 Park Avenue&homes[1][name]=Stark Tower&isSuperhero=1&name=Tony&password=%&34&phones[mobile]=123456789&phones[office]=123987456&username=Stark&weightKg=75.8 "雪莱刺豺")
许可
本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE文件