CountryKit 是一个 Swift 库,适用于 iOS、macOS、Linux、tvOS 和 watchOS,其中包含根据《统计用途的国家或地区标准代码》(也称为 UN M49)的世界所有大洲、区域、次区域和国家/地区的数据。
国家/地区数据包含:
Swift Package Manager 是用于管理 Swift 代码分发的工具。它与 Swift 构建系统集成,可以自动执行下载、编译和链接依赖项的过程。
需要 Xcode 11+ 才能使用 Swift Package Manager 构建 CountryKit。
要使用 Swift Package Manager 将 CountryKit 集成到您的 Xcode 项目中,请将其添加到您的 Package.swift 文件的 dependencies 值中。
dependencies: [
.package(url: "https://github.com/frederik-jacques/countrykit.git", .upToNextMajor(from: "0.1.0"))
]
如果您不想使用 Swift Package Manager,您可以手动将 CountryKit 集成到您的项目中。
每个大洲都有 5 个国家/地区提供程序。
AfricanCountriesProvider:非洲的所有数据AmericasCountriesProvider:美洲的所有数据AsianCountriesProvider:亚洲的所有数据EuropeanCountriesProvider:欧洲的所有数据OceanianCountriesProvider:大洋洲的所有数据如果您想要世界上的所有国家/地区并进行特定的过滤,可以使用 WorldProvider。
每个国家/地区提供程序都遵循 CountryProvidable 协议,该协议具有以下属性和方法。
/// All countries for this continent.
var countries: [Country] { get }
/// Get a country for a given country code.
/// - Parameter countryCode: The numeric country code
/// - Returns: The country for the given country code (optional)
func get(countryCode: Int) -> Country?
/// Get a list of countries for a given continent.
/// - Parameter continent: The continent
/// - Parameter sortBehavior: The sort behavior
/// - Returns: A list of countries
func get(continent: Continent, sortBehavior: CountrySortBehavior) -> [Country]
/// Get a list of countries for a region.
/// - Parameter region: The region
/// - Parameter sortBehavior: The sort behavior
/// - Returns: A list of countries
func get(region: Region, sortBehavior: CountrySortBehavior) -> [Country]
/// Get a list of countries for a subregion.
/// - Parameter subregion: The subregion
/// - Parameter sortBehavior: The sort behavior
/// - Returns: A list of countries
func get(subregion: Subregion, sortBehavior: CountrySortBehavior) -> [Country]
/// Get a list of countries for a given region and subregion.
/// - Parameter region: The region
/// - Parameter subregion: The subregion
/// - Parameter sortBehavior: The sort behavior
/// - Returns: A list of countries
func get(region: Region, subregion: Subregion, sortBehavior: CountrySortBehavior) -> [Country]
| 大洲 | 区域 | 次区域 |
|---|---|---|
| 非洲 | ||
| 北非 | ||
| 撒哈拉以南非洲 | ||
| 西非 | ||
| 东非 | ||
| 中非 | ||
| 南部非洲 | ||
| 美洲 | ||
| 北美洲 | ||
| 北部美洲 | ||
| 拉丁美洲和加勒比地区 | ||
| 南美洲 | ||
| 中美洲 | ||
| 加勒比地区 | ||
| 亚洲 | ||
| 东亚 | ||
| 南亚 | ||
| 东南亚 | ||
| 中亚 | ||
| 西亚 | ||
| 欧洲 | ||
| 南欧 | ||
| 东欧 | ||
| 北欧 | ||
| 海峡群岛 | ||
| 西欧 | ||
| 大洋洲 | ||
| 澳大利亚和新西兰 | ||
| 美拉尼西亚 | ||
| 密克罗尼西亚 | ||
| 波利尼西亚 |
import CountryKit
let provider = EuropeanContinentProvider()
provider.get(sortBehavior: .ascending)
import CountryKit
let provider = AmericasContinentProvider() // Or use WorldProvider
provider.get(subregion: .southAmerica, sortBehavior: .descending)
import CountryKit
let provider = WorldProvider()
let belgium = provider.get(countryCode: 56)
如果您想要英文名称,您可以直接使用 Country 结构体上的 name 属性。但是,如果您想在您的应用程序中本地化国家/地区名称,您可以使用 translation(for:) 方法并传入 Locale。
import CountryKit
let locale = Locale(identifier: "fr")
let provider = WorldProvider()
let belgium = provider.get(countryCode: 56)
let countryNameInFrench = belgium.translation(for: locale) // Belgique
CountryKit 在 MIT 许可下发布。有关详细信息,请参阅 LICENSE。