__ _ __ _ _ _
/ _\_ _(_)/ _| |_ /\/\ __ _ __ _(_) ___ /\ /\___| |_ __ ___ _ __ ___
\ \\ \ /\ / / | |_| __|/ \ / _` |/ _` | |/ __|/ /_/ / _ \ | '_ \ / _ \ '__/ __|
_\ \\ V V /| | _| |_/ /\/\ \ (_| | (_| | | (__/ __ / __/ | |_) | __/ | \__ \
\__/ \_/\_/ |_|_| \__\/ \/\__,_|\__, |_|\___\/ /_/ \___|_| .__/ \___|_| |___/
|___/ |_|
该软件包统一了您日常编码中最有用的助手工具。
public struct HelperManager {}
此助手工具提供使用 Codable 框架将 JSON 编码和解码为 Swift 对象的方法,反之亦然。
描述:将 Swift 对象编码为 JSON 字符串的函数。
- 参数 result: 用于传递要编码对象的泛型。
- 抛出: NSError,包含消息和错误代码 Int,用于错误管理。
- 返回: String: 编码后的 JSON 字符串格式对象。
public static func jsonEncode<T: Codable>(_ result: T) throws -> String
do {
let encoded = try HelperManager.JSON.jsonEncode(mockStructure)
} catch let error as NSError {
//put here your error management.
}
描述: 将 JSON 字符串解码为 Swift 对象的函数。
- 参数:
- json: 要解码的 JSON 字符串。
- type: 作为 JSON 转换的 baseinterface 的类型标识。
- 抛出: NSError,包含消息和错误代码 Int,用于错误管理。
- 返回: T?: 在构造函数中提供的类类型,用于解码此 JSON。
public static func jsonDecode<T: Codable>(_ json: String, type: T.Type) throws -> T
对象
mockStruct = MockObject(field1: "value1", field2: 1, field3: ["arrayvalue1", "arrayvalue2"], field4: MockObject2(mField1: "mValue1", mField2: 2, mField3: [0, 1, 2, 3, 4, 5, 6]))
使用
do {
let decoded = try HelperManager.JSON.jsonDecode(jsonString, type: MockObject.self)
} catch let error as NSError {
//put here your error management.
}
此助手工具包含一个函数,用于捕获视图循环堆栈中的顶部视图控制器。
描述:将 Swift 对象编码为 JSON 字符串的函数。
- 参数 base: 用于查找顶部视图控制器的堆栈基础。
- 返回: UIViewController: 在基础堆栈中找到的第一个视图控制器。
public static func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?
let topVC = HelperManager.UIApplicationHelpers.getTopViewController()
David Martin Saiz – @deividmarshall – davms81@gmail.com
在 MIT 许可下分发。 有关更多信息,请参阅 LICENSE
。
https://github.com/CodeNationDev/