Skip to content

Commit

Permalink
支持Combine
Browse files Browse the repository at this point in the history
  • Loading branch information
Mccc committed Sep 26, 2024
1 parent 6e96553 commit 14fa215
Show file tree
Hide file tree
Showing 15 changed files with 831 additions and 515 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PODS:
- FBSnapshotTestCase/SwiftSupport (2.1.4):
- FBSnapshotTestCase/Core
- HandyJSON (5.0.0-beta.1)
- SmartCodable (4.1.10)
- SmartCodable (4.1.11-beta)
- SnapKit (5.6.0)

DEPENDENCIES:
Expand Down Expand Up @@ -39,7 +39,7 @@ SPEC CHECKSUMS:
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
SmartCodable: f1e865bb983fdcf51b8bc45c6dda8a08379c315c
SmartCodable: 07ad5f71014a288baaf3c221e1061b547af08607
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25

PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f
Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/SmartCodable.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

890 changes: 447 additions & 443 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Example/SmartCodable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
990512A52C0869CA00A2C5D8 /* Decoding_globalKeyStrategyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 990512A42C0869CA00A2C5D8 /* Decoding_globalKeyStrategyViewController.swift */; };
990512A72C086FBB00A2C5D8 /* Decoding_globalValueStrategyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 990512A62C086FBB00A2C5D8 /* Decoding_globalValueStrategyViewController.swift */; };
990512A92C0873F200A2C5D8 /* Decoding_valueMapViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 990512A82C0873F200A2C5D8 /* Decoding_valueMapViewController.swift */; };
990CBAF42CA4F5300099E4C7 /* 1231231.swift in Sources */ = {isa = PBXBuildFile; fileRef = 990CBAF32CA4F5300099E4C7 /* 1231231.swift */; };
990ECFA92B95A45B003D37F1 /* Support_JSONStringViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 990ECFA82B95A45B003D37F1 /* Support_JSONStringViewController.swift */; };
991259ED2BFB5B2000ED76B5 /* BaseData_Int8ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 991259EC2BFB5B2000ED76B5 /* BaseData_Int8ViewController.swift */; };
991259EF2BFB6C1000ED76B5 /* Container_DictViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 991259EE2BFB6C1000ED76B5 /* Container_DictViewController.swift */; };
Expand Down Expand Up @@ -133,6 +134,7 @@
99D9458A2B91C75600CC6470 /* SpecialData_dateViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99D945892B91C75600CC6470 /* SpecialData_dateViewController.swift */; };
99D9458C2B91C80300CC6470 /* SpecialData_dataViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99D9458B2B91C80300CC6470 /* SpecialData_dataViewController.swift */; };
99D9458E2B91C80C00CC6470 /* SpecialData_FloatViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99D9458D2B91C80C00CC6470 /* SpecialData_FloatViewController.swift */; };
99E438A92CA50D2E001C08B9 /* Introduce_12ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99E438A82CA50D2E001C08B9 /* Introduce_12ViewController.swift */; };
99F9E8A62BB5139900D68037 /* MixDecodingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99F9E8A52BB5139900D68037 /* MixDecodingViewController.swift */; };
AF374D4C62AA92DE0D9ED2B7 /* Pods_SmartCodable_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5EB62463C4F6AEFEAA2A5E1E /* Pods_SmartCodable_Tests.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -172,6 +174,7 @@
990512A42C0869CA00A2C5D8 /* Decoding_globalKeyStrategyViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Decoding_globalKeyStrategyViewController.swift; sourceTree = "<group>"; };
990512A62C086FBB00A2C5D8 /* Decoding_globalValueStrategyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Decoding_globalValueStrategyViewController.swift; sourceTree = "<group>"; };
990512A82C0873F200A2C5D8 /* Decoding_valueMapViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Decoding_valueMapViewController.swift; sourceTree = "<group>"; };
990CBAF32CA4F5300099E4C7 /* 1231231.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = 1231231.swift; sourceTree = "<group>"; };
990ECFA82B95A45B003D37F1 /* Support_JSONStringViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Support_JSONStringViewController.swift; sourceTree = "<group>"; };
991259EC2BFB5B2000ED76B5 /* BaseData_Int8ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseData_Int8ViewController.swift; sourceTree = "<group>"; };
991259EE2BFB6C1000ED76B5 /* Container_DictViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Container_DictViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -274,6 +277,7 @@
99D945892B91C75600CC6470 /* SpecialData_dateViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialData_dateViewController.swift; sourceTree = "<group>"; };
99D9458B2B91C80300CC6470 /* SpecialData_dataViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialData_dataViewController.swift; sourceTree = "<group>"; };
99D9458D2B91C80C00CC6470 /* SpecialData_FloatViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpecialData_FloatViewController.swift; sourceTree = "<group>"; };
99E438A82CA50D2E001C08B9 /* Introduce_12ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Introduce_12ViewController.swift; sourceTree = "<group>"; };
99F9E8A52BB5139900D68037 /* MixDecodingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MixDecodingViewController.swift; sourceTree = "<group>"; };
AD8215B5E8E50F277C1C4356 /* Pods-SmartCodable_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SmartCodable_Example.release.xcconfig"; path = "Target Support Files/Pods-SmartCodable_Example/Pods-SmartCodable_Example.release.xcconfig"; sourceTree = "<group>"; };
AFEADC35BC2373F1DC69B73B /* Pods-SmartCodable_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SmartCodable_Tests.release.xcconfig"; path = "Target Support Files/Pods-SmartCodable_Tests/Pods-SmartCodable_Tests.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -339,6 +343,7 @@
99675FDC2B3EC930006C83EC /* TestViewController.swift */,
99CE4D512BC687700006D435 /* Test2ViewController.swift */,
99153D762BD1124C004C01AD /* Test3ViewController.swift */,
990CBAF32CA4F5300099E4C7 /* 1231231.swift */,
99C1E70C2B99A55E0090E8FD /* 说明信息 */,
99675FDF2B3EC930006C83EC /* Smart */,
9902B4CE2BB2F32D00310E69 /* 平替HandyJSON */,
Expand Down Expand Up @@ -506,6 +511,7 @@
99CE4D632BC6AB8B0006D435 /* Introduce_9ViewController.swift */,
9955576D2BE0DCC6005F9ABD /* Introduce_10ViewController.swift */,
995E03C12C182C68009E1E5B /* Introduce_11ViewController.swift */,
99E438A82CA50D2E001C08B9 /* Introduce_12ViewController.swift */,
99CE4D652BC6AFC90006D435 /* IntroduceViewController.swift */,
);
path = "1.Introduce(使用介绍)";
Expand Down Expand Up @@ -1011,6 +1017,7 @@
996760402B3EC930006C83EC /* Introduce_3ViewController.swift in Sources */,
9956B9B92BD92D1900C711F5 /* DecodingArrayLogViewController.swift in Sources */,
998567292C12A3AE0021B7FD /* Encode_CustomKeyViewController.swift in Sources */,
99E438A92CA50D2E001C08B9 /* Introduce_12ViewController.swift in Sources */,
9967604A2B3EC930006C83EC /* BaseViewController.swift in Sources */,
9967601B2B3EC930006C83EC /* ViewController.swift in Sources */,
9985671A2C11C6010021B7FD /* Encode_SpecialData_dateViewController.swift in Sources */,
Expand Down Expand Up @@ -1082,6 +1089,7 @@
996760242B3EC930006C83EC /* BaseData_BoolViewController.swift in Sources */,
9985670E2C11B9810021B7FD /* TestEncodeCaseViewController.swift in Sources */,
996760372B3EC930006C83EC /* CaseOneViewController.swift in Sources */,
990CBAF42CA4F5300099E4C7 /* 1231231.swift in Sources */,
9902B4DC2BB304B400310E69 /* ReplaceHandyJSON_6ViewController.swift in Sources */,
9985671C2C11C6510021B7FD /* Encode_SpecialData_dataViewController.swift in Sources */,
);
Expand Down
50 changes: 50 additions & 0 deletions Example/SmartCodable/1231231.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// 1231231.swift
// SmartCodable_Example
//
// Created by qixin on 2024/9/26.
// Copyright © 2024 CocoaPods. All rights reserved.
//

import Foundation
import SwiftUI
import Combine
import SmartCodable

class MyModel: ObservableObject, SmartCodable {
required init() {

}
@Published
var name: String = "iOS Developer"

enum CodingKeys: String, CodingKey {
case name
}

// 自定义的编码方法
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
}

// 自定义的解码方法
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
}

}

//struct ContentView: View {
// @ObservedObject var model = MyModel()
//
// var body: some View {
// VStack {
// Text("Hello, \(model.name)")
// Button("Change Name") {
// model.name = "Swift Developer"
// }
// }
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extension IntroduceViewController {
["name": "9. 支持UIColor的解析", "vc": "Introduce_9ViewController"],
["name": "10. 指定解析路径", "vc": "Introduce_10ViewController"],
["name": "11. 更新现有模型", "vc": "Introduce_11ViewController"],
["name": "12. 支持Combine", "vc": "Introduce_12ViewController"],


]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Introduce_12ViewController.swift
// SmartCodable_Example
//
// Created by qixin on 2024/9/26.
// Copyright © 2024 CocoaPods. All rights reserved.
//

import Foundation
import Combine
import SmartCodable

class Introduce_12ViewController: BaseViewController {
var cancellables = Set<AnyCancellable>()

override func viewDidLoad() {
super.viewDidLoad()

let dict: [String: Any] = [
"newName": 1,
"age": "333333"
]

if let model = PublishedModel.deserialize(from: dict) {
print("反序列化后的 name 值: \(model.name)")

// 正确访问 name 属性的 Publisher
model.$name
.sink { newName in
print("name 属性发生变化,新值为: \(newName)")
}
.store(in: &cancellables)

// 修改 model 的 name 属性
model.name = "Updated iOS Developer"
}
}
}

// 定义 PublishedModel,并实现反序列化
class PublishedModel: ObservableObject, SmartCodable {
required init() {}

@SmartPublished @SmartAny
var name: Any = "iOS Developer"

static func mappingForKey() -> [SmartKeyTransformer]? {
[CodingKeys.name <--- "newName"]
}

// static func mappingForValue() -> [SmartValueTransformer]? {
// [
// CodingKeys.name <--- PublishedValueTransformer(),
// ]
// }
}

struct PublishedValueTransformer: ValueTransformable {
func transformFromJSON(_ value: Any) -> String? {
return "good"
}

func transformToJSON(_ value: String) -> String? {
return "gooooooood"
}

typealias Object = String

typealias JSON = String
}
38 changes: 3 additions & 35 deletions Example/SmartCodable/Test2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,13 @@

import SmartCodable
import BTPrint
import Combine

class Test2ViewController: BaseViewController {

var cancellables = Set<AnyCancellable>()

override func viewDidLoad() {
super.viewDidLoad()



SmartConfig.debugMode = .none

guard let image = UIImage(named: "question") else { return }
let byte = UIImagePNGRepresentation(image)!


var dict111: [String: Any] = [
"id": "112312",
"name": "hello",
"byte": byte
]

// print(dict111)

let model = DataModel11.deserialize(from: dict111)
print(model)
print(model?.thumbnail_picture)
}
}
struct DataModel11: SmartCodable {
/// 表id
var id: Int = -1
/// 名称
var name: String = ""
/// 缩略图流
var byte: Data? = nil

var thumbnail_picture: UIImage? {
guard let byte = byte else {
return nil
}
return UIImage(data: byte)
}
}
66 changes: 57 additions & 9 deletions Example/SmartCodable/Test3ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,73 @@ import CleanJSON
*/


import SmartCodable

class Test3ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

let string = "{\"id\":1, \"sex\":{\"id\":1}}"


if let model = Model.deserialize(from: string) {
smartPrint(value: model)

let dict1: [String: Any] = [
"age": 10,
"name": "Mccc",
"location": [
"province": "Jiang zhou",
"city": "Su zhou",
]

]

if let jsonData = try? JSONSerialization.data(withJSONObject: dict1, options: []) {
// Successfully converted Dictionary to Data
print("JSON Data:", jsonData)

do {
let obj = try JSONDecoder().decode(Model.self, from: jsonData)
print("obj = ", obj)

} catch {
print("error = ", error)
}

// If you want to convert it back to a String for debugging purposes
if let jsonString = String(data: jsonData, encoding: .utf8) {
print("JSON String:", jsonString)
}
}

if let model = Model.deserialize(from: dict1) {
smartPrint(value: model)
print("\n")
let dict = model.toDictionary() ?? [:]
print(dict)
}
}

}




extension Test3ViewController {
struct Model: SmartCodable {
var id: Int = 0
var sex: SubModel = SubModel()
var name: String = ""
@IgnoredKey
var ignore: String = ""
@IgnoredKey
var ignore2 = ""
var age: Int = 0
var location: Location?
}

struct SubModel: SmartCodable {
var id : Int = 0
struct Location: SmartCodable {
var province: String = ""

// 忽略解析
@IgnoredKey
var city: String = "area123"
}
}


Loading

0 comments on commit 14fa215

Please sign in to comment.