-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mccc
committed
Sep 26, 2024
1 parent
6e96553
commit 14fa215
Showing
15 changed files
with
831 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
// } | ||
// } | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Example/SmartCodable/Smart/1.Introduce(使用介绍)/Introduce_12ViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.