Config
Config是一个用Swift编写的框架,使得您可以用作配置的JSON文件,您可以在应用程序中使用点表示法中的JSON键。还支持SwiftUI!
目录
需求
- iOS 15.0
- Xcode 13.1
- Swift 5.5
集成
Swift包管理器
- 在Xcode中,打开
文件 > 添加包
。 - 搜索 https://github.com/mustafakarakus/Config.git
- Config 应该会被列出。点击
添加包
。
CocoaPods
您可以使用CocoaPods通过将其添加到Podfile来安装 Config
use_frameworks!
target 'MyApp' do
pod 'Config'
end
使用方式
在Xcode项目中创建一个所需的配置文件,格式为 JSON,或者使用一个返回 JSON 的web URL。
{
"development": {
"debug": true
},
"application": {
"type":5,
"version":1.2,
"appKey":"ABCD-EFGH-IJKLMNOPR",
"security":{
"OAuth2":{
"credentials":{
"username":"mustafakarakus",
"password":"password"
},
"groups":[1,9,0,5],
"chain":false
},
"domainExceptions":[
"https://www.google.com",
"https://www.twitter.com"
]
}
}
}
您有三种读取您的 JSON 值的选择,选择最适合您的方式
- 实例
- 属性包裹器
- 模式匹配
实例
- 使用您目录中存在的JSON文件初始化 Config。或者在初始化时使用JSON URL。
本地文件
在所需的项目目录中添加 my-config.json
文件
let config = Config(with: "my-config")
远程文件
使用您远程 موجودي JSON文件 的URL。
let config = Config(with: "https://raw.githubusercontent.com/mustafakarakus/Config/master/ConfigExamples/config.json")
- 用 点表示法 输入你的JSON键,这与JSON文件中所述的相同。最后调用 .value() 函数。 value() 函数是类型推断的。这意味着你需要在变量/常量中指定你的数据类型。
let myIntegerValue:Int = config.myKey.myIntegerValue.value()
if let development:[String:Any] = config.development.value(){
print(development)
}
示例
import Config
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let config = Config(with: "my-config")
if let development:[String:Any] = config.development.value() {
print(development)
}
if let debug:Bool = config.development.debug.value(){
print(debug)
}
if let application:[String:Any] = config.application.value(){
print(application)
}
if let version:Double = config.application.version.value(){
print(version)
}
if let applicationType:Int = config.application.type.value(){
print(applicationType)
}
if let appKey:String = config.application.appKey.value(){
print(appKey)
}
if let username:String = config.application.security.OAuth2.credentials.username.value(){
print(username)
}
if let groups:[Int] = config.application.security.OAuth2.groups.value(){
print(groups)
}
if let domainExceptions:[String] = config.application.security.domainExceptions.value(){
print(domainExceptions)
}
}
}
JSONValue 属性包装器
- 用本地JSON文件名标记变量;
或者
在这种情况下,'application.security.OAuth2.groups' 数据类型是 'my-config.json' 文件中的一个整型数组(参见上面的示例JSON文件)。
@JSONValue("my-config", "application.security.OAuth2.groups")
var groups: [Int] = []
@JSONValue(url: "https://raw.githubusercontent.com/mustafakarakus/Config/master/ConfigExamples/config.json", "application.appKey")
var appKey: String = ""
示例
import Config
class PropertyWrapperViewController: UIViewController {
@JSONValue("my-config", "application.security.OAuth2.groups")
var groups: [Int] = []
@JSONValue("my-config1", "development")
var development: [String:Any] = [:]
@JSONValue("my-config", "development.debug")
var isDebug: Bool = false
@JSONValue("my-config", "application.security.OAuth2.credentials.username")
var username: String = ""
@JSONValue(url: "https://raw.githubusercontent.com/mustafakarakus/Config/master/ConfigExamples/config.json", "application.appKey")
var appKey: String = ""
override func viewDidLoad() {
super.viewDidLoad()
print("groups: \(groups)")
print("development: \(development)")
print("debug mode: \(isDebug)")
print("OAuth2 username: \(username)")
print("App Key: \(appKey)")
}
}
模式匹配
- 创建一个 Config 实例,然后使用 点表示法 输入JSON文件中的键。
- 使用Swift的模式匹配功能进行以下操作
.string(val)
.int(val)
.double(val)
.bool(val)
.object(val)
.array(val)
private var config: Config? {
let sampleUrl = "https://raw.githubusercontent.com/mustafakarakus/Config/master/ConfigExamples/config.json"
guard let url = URL(string: sampleUrl) else { return nil }
return Config(with: url)
}
let string = config?.application.appKey
if case let .string(val) = string {
print("app Key \(val)")
}
示例
import Config
class PatternMatchingViewController: UIViewController {
private var config: Config? {
let sampleUrl = "https://raw.githubusercontent.com/mustafakarakus/Config/master/ConfigExamples/config.json"
guard let url = URL(string: sampleUrl) else { return nil }
return Config(with: url)
}
override func viewDidLoad() {
super.viewDidLoad()
let string = config?.application.appKey
if case let .string(val) = string {
print("app Key \(val)")
}
let double = config?.application.version
if case let .double(val) = double {
print("version: \(val)")
}
let intArray = config?.application.security.OAuth2.groups
if case let .array(val) = intArray {
for item in val {
print("groups: \(String(describing: item.intValue))")
}
}
let dictionary = config?.application
if case let .object(val) = dictionary {
print("application: \(val)")
}
}
}
SwiftUI
Config 现已支持 SwiftUI。在 SwiftUI 变量的 JSONValue 属性包装器中,将绑定来自 JSON 源的数据,本地或远程。
import SwiftUI
import Config
struct ContentViewLocal: View {
@JSONValue("my-config", "application.security.OAuth2.credentials.username")
var name: String = ""
var body: some View {
Text(name)
.padding()
}
}
import SwiftUI
import Config
struct ContentViewRemote: View {
@JSONValue(url: "https://raw.githubusercontent.com/mustafakarakus/Config/master/ConfigExamples/config.json", "application.appKey")
var appKey: String = ""
var body: some View {
Text(appKey)
.padding()
}
}
接下来是什么?
- Carthage
- Swift包管理器
- 远程JSON
- Swift 5.x
- 属性包装器
- 使用实例代替单例
- SwiftUI
贡献
任何愿意为项目做出贡献的人都非常欢迎。
- Fork 此仓库
- 做出您的更改
- 提交拉取请求
授予权限
MIT 授权协议
版权所有 (c) 2021 mustafakarakus
任何人可以免费获得并使用此软件及其相关文档文件(以下称为“软件”),无限制地处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许将软件提供给其他人,以便他们这样做,只需遵守以下条件
上述版权声明和本许可声明应包含在软件的任何副本或实质部分中。
软件按“原样”提供,不受任何形式的保证,明示或暗示,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权持有人均不承担任何权利主张、损害赔偿或其他责任,无论其源于合同、侵权或其他方式,源于、起源于或与软件或其使用或处理相关。