Skip to content

Commit a11a894

Browse files
committed
Refacor interface
1 parent 62cfc99 commit a11a894

File tree

20 files changed

+595
-597
lines changed

20 files changed

+595
-597
lines changed

KakaJSON/Convert/Convertible.swift

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,33 +142,33 @@ public struct ConvertibleKJ_M<T: Convertible> {
142142
}
143143

144144
/// JSONObject -> Model
145-
public func convert(from jsonData: Data?) {
145+
public func convert(from jsonData: Data) {
146146
basePtr.pointee.kj_convert(from: jsonData)
147147
}
148148

149149
/// JSONObject -> Model
150-
public func convert(from jsonData: NSData?) {
151-
basePtr.pointee.kj_convert(from: jsonData as Data?)
150+
public func convert(from jsonData: NSData) {
151+
basePtr.pointee.kj_convert(from: jsonData as Data)
152152
}
153153

154154
/// JSONObject -> Model
155-
public func convert(from jsonString: String?) {
155+
public func convert(from jsonString: String) {
156156
basePtr.pointee.kj_convert(from: jsonString)
157157
}
158158

159159
/// JSONObject -> Model
160-
public func convert(from jsonString: NSString?) {
161-
basePtr.pointee.kj_convert(from: jsonString as String?)
160+
public func convert(from jsonString: NSString) {
161+
basePtr.pointee.kj_convert(from: jsonString as String)
162162
}
163163

164164
/// JSONObject -> Model
165-
public func convert(from json: [String: Any]?) {
165+
public func convert(from json: [String: Any]) {
166166
basePtr.pointee.kj_convert(from: json)
167167
}
168168

169169
/// JSONObject -> Model
170-
public func convert(from json: NSDictionary?) {
171-
basePtr.pointee.kj_convert(from: json as? [String: Any])
170+
public func convert(from json: NSDictionary) {
171+
basePtr.pointee.kj_convert(from: json as! [String: Any])
172172
}
173173
}
174174

@@ -200,31 +200,30 @@ private extension Convertible {
200200

201201
// MARK: - JSON -> Model
202202
extension Convertible {
203-
mutating func kj_convert(from jsonData: Data?) {
203+
mutating func kj_convert(from jsonData: Data) {
204204
if let json = JSONSerialization.kj_JSON(jsonData, [String: Any].self) {
205205
kj_convert(from: json)
206206
return
207207
}
208208
Logger.error("Failed to get JSON from JSONData.")
209209
}
210210

211-
mutating func kj_convert(from jsonString: String?) {
211+
mutating func kj_convert(from jsonString: String) {
212212
if let json = JSONSerialization.kj_JSON(jsonString, [String: Any].self) {
213213
kj_convert(from: json)
214214
return
215215
}
216216
Logger.error("Failed to get JSON from JSONString.")
217217
}
218218

219-
mutating func kj_convert(from json: [String: Any]?) {
220-
guard let dict = json,
221-
let mt = Metadata.type(self) as? ModelType,
219+
mutating func kj_convert(from json: [String: Any]) {
220+
guard let mt = Metadata.type(self) as? ModelType,
222221
let properties = mt.properties else { return }
223222

224223
// get data address
225224
let model = _ptr()
226225

227-
kj_willConvertToModel(from: dict)
226+
kj_willConvertToModel(from: json)
228227

229228
// enumerate properties
230229
for property in properties {
@@ -234,7 +233,7 @@ extension Convertible {
234233

235234
// value filter
236235
guard let newValue = kj_modelValue(
237-
from: dict.kj_value(for: key),
236+
from: json.kj_value(for: key),
238237
property)~! else { continue }
239238

240239
let propertyType = property.dataType
@@ -253,15 +252,15 @@ extension Convertible {
253252
}
254253

255254
// try to convert newValue to propertyType
256-
guard let value = newValue~?.kj_value(propertyType) else {
255+
guard let value = Values.kj_value(newValue, propertyType) else {
257256
property.set(newValue, for: model)
258257
continue
259258
}
260259

261260
property.set(value, for: model)
262261
}
263262

264-
kj_didConvertToModel(from: dict)
263+
kj_didConvertToModel(from: json)
265264
}
266265

267266
private mutating
@@ -321,7 +320,7 @@ extension Convertible {
321320
from: property.get(from: ptr)~!,
322321
property)~! else { continue }
323322

324-
guard let v = value~?.kj_JSON() else { continue }
323+
guard let v = Values.kj_JSON(value) else { continue }
325324

326325
// key filter
327326
json[mt.JSONKey(from: property.name,
@@ -334,7 +333,7 @@ extension Convertible {
334333
}
335334

336335
func kj_JSONString(prettyPrinted: Bool = false) -> String? {
337-
if let str = JSONSerialization.kj_string(kj_JSONObject(),
336+
if let str = JSONSerialization.kj_string(kj_JSONObject() as Any,
338337
prettyPrinted: prettyPrinted) {
339338
return str
340339
}

KakaJSON/Convert/ConvertibleEnum.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
//
88

99
public protocol ConvertibleEnum {
10-
static func kj_convert(from value: Any?) -> Self?
10+
static func kj_convert(from value: Any) -> Self?
1111
var kj_value: Any { get }
1212
static var kj_valueType: Any.Type { get }
1313
}
1414

1515
public extension RawRepresentable where Self: ConvertibleEnum {
16-
static func kj_convert(from value: Any?) -> Self? {
16+
static func kj_convert(from value: Any) -> Self? {
1717
return (value as? RawValue).flatMap { Self.init(rawValue: $0) }
1818
}
1919
var kj_value: Any { return rawValue }

0 commit comments

Comments
 (0)