kk_JSONKey也支持ConvertibleConfig配置,用法类似于kk_modelKey,参考第三篇文章
值过滤 private let dateFmt: DateFormatter = { let fmt = DateFormatter() fmt.dateFormat = "yyyy-MM-dd HH:mm:ss" return fmt }() struct Student: Convertible { var birthday: Date? // 可以通过实现`kk_JSONValue`方法自定义最终生成JSON的value func kk_JSONValue(from modelValue: Any?, property: Property) -> Any? { if property.name != "birthday" { return modelValue } // 将`birthday`格式化成字符串后放入JSON return birthday.flatMap(dateFmt.string) } } let time = "2019-08-13 12:52:51" let date = dateFmt.date(from: time) let student = Student(birthday: date) let jsonString = student.kk.JSONString() /* {"birthday":"2019-08-13 12:52:51"} */kk_JSONValue也支持ConvertibleConfig配置,用法类似于kk_modelKey,参考第三篇文章
监听 struct Car: Convertible { var name: String = "Bently" var age: Int = 10 // 当Model即将开始转换为JSON时调用 func kk_willConvertToJSON() { print("Car - kk_willConvertToJSON") } // 当Model转换JSON完毕时调用 func kk_didConvertToJSON(json: JSONObject?) { print("Car - kk_didConvertToJSON", json as Any) } }