Skip to content

Commit b17d751

Browse files
author
Mccc
committed
发布V4.2.2
1 parent 3c8bac8 commit b17d751

10 files changed

Lines changed: 106 additions & 76 deletions

File tree

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PODS:
88
- FBSnapshotTestCase/SwiftSupport (2.1.4):
99
- FBSnapshotTestCase/Core
1010
- HandyJSON (5.0.0-beta.1)
11-
- SmartCodable (4.2.1)
11+
- SmartCodable (4.2.2)
1212
- SnapKit (5.6.0)
1313

1414
DEPENDENCIES:
@@ -39,7 +39,7 @@ SPEC CHECKSUMS:
3939
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
4040
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
4141
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
42-
SmartCodable: ad311e84a90b09a85ba8f963ec16d30bc11fed19
42+
SmartCodable: 4f52d801dbd15856fabb69110d24f31f49145734
4343
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25
4444

4545
PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f

Example/Pods/Local Podspecs/SmartCodable.podspec.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SmartCodable.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,12 @@
823823
TargetAttributes = {
824824
607FACCF1AFB9204008FA782 = {
825825
CreatedOnToolsVersion = 6.3.1;
826+
DevelopmentTeam = RA2G5HURUT;
826827
LastSwiftMigration = 0900;
827828
};
828829
607FACE41AFB9204008FA782 = {
829830
CreatedOnToolsVersion = 6.3.1;
831+
DevelopmentTeam = RA2G5HURUT;
830832
LastSwiftMigration = 0900;
831833
TestTargetID = 607FACCF1AFB9204008FA782;
832834
};
@@ -1239,6 +1241,7 @@
12391241
baseConfigurationReference = BC656B63C9031B22DEB5C8EB /* Pods-SmartCodable_Example.debug.xcconfig */;
12401242
buildSettings = {
12411243
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
1244+
DEVELOPMENT_TEAM = RA2G5HURUT;
12421245
INFOPLIST_FILE = SmartCodable/Info.plist;
12431246
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
12441247
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@@ -1255,6 +1258,7 @@
12551258
baseConfigurationReference = AD8215B5E8E50F277C1C4356 /* Pods-SmartCodable_Example.release.xcconfig */;
12561259
buildSettings = {
12571260
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
1261+
DEVELOPMENT_TEAM = RA2G5HURUT;
12581262
INFOPLIST_FILE = SmartCodable/Info.plist;
12591263
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
12601264
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@@ -1270,6 +1274,7 @@
12701274
isa = XCBuildConfiguration;
12711275
baseConfigurationReference = DB7246035010143E03C7616B /* Pods-SmartCodable_Tests.debug.xcconfig */;
12721276
buildSettings = {
1277+
DEVELOPMENT_TEAM = RA2G5HURUT;
12731278
FRAMEWORK_SEARCH_PATHS = (
12741279
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
12751280
"$(inherited)",
@@ -1293,6 +1298,7 @@
12931298
isa = XCBuildConfiguration;
12941299
baseConfigurationReference = AFEADC35BC2373F1DC69B73B /* Pods-SmartCodable_Tests.release.xcconfig */;
12951300
buildSettings = {
1301+
DEVELOPMENT_TEAM = RA2G5HURUT;
12961302
FRAMEWORK_SEARCH_PATHS = (
12971303
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
12981304
"$(inherited)",

Example/SmartCodable/Smart/1.Introduce(使用介绍)/Introduce_12ViewController.swift

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ import SmartCodable
1313
class Introduce_12ViewController: BaseViewController {
1414
var cancellables = Set<AnyCancellable>()
1515

16+
var model: PublishedModel?
17+
1618
override func viewDidLoad() {
1719
super.viewDidLoad()
1820

1921
let dict: [String: Any] = [
20-
"newName": 1,
22+
"name": 1,
2123
"age": "333333"
2224
]
2325

2426
if let model = PublishedModel.deserialize(from: dict) {
27+
28+
self.model = model
2529
print("反序列化后的 name 值: \(model.name)")
2630

2731
// 正确访问 name 属性的 Publisher
@@ -31,10 +35,14 @@ class Introduce_12ViewController: BaseViewController {
3135
}
3236
.store(in: &cancellables)
3337

34-
// 修改 model 的 name 属性
35-
model.name = "Updated iOS Developer"
38+
3639
}
3740
}
41+
42+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
43+
// 修改 model 的 name 属性
44+
model?.name = "Updated iOS Developer"
45+
}
3846
}
3947

4048
// 定义 PublishedModel,并实现反序列化
@@ -43,28 +51,22 @@ class PublishedModel: ObservableObject, SmartCodable {
4351

4452
@SmartPublished
4553
var name: String = "iOS Developer"
46-
47-
static func mappingForKey() -> [SmartKeyTransformer]? {
48-
[CodingKeys.name <--- "newName"]
49-
}
50-
51-
// static func mappingForValue() -> [SmartValueTransformer]? {
52-
// [
53-
// CodingKeys.name <--- PublishedValueTransformer(),
54-
// ]
54+
//
55+
// static func mappingForKey() -> [SmartKeyTransformer]? {
56+
// [CodingKeys.name <--- "newName"]
5557
// }
5658
}
5759

58-
struct PublishedValueTransformer: ValueTransformable {
59-
func transformFromJSON(_ value: Any) -> String? {
60-
return "good"
61-
}
62-
63-
func transformToJSON(_ value: String) -> String? {
64-
return "gooooooood"
65-
}
66-
67-
typealias Object = String
68-
69-
typealias JSON = String
70-
}
60+
//struct PublishedValueTransformer: ValueTransformable {
61+
// func transformFromJSON(_ value: Any) -> String? {
62+
// return "good"
63+
// }
64+
//
65+
// func transformToJSON(_ value: String) -> String? {
66+
// return "gooooooood"
67+
// }
68+
//
69+
// typealias Object = String
70+
//
71+
// typealias JSON = String
72+
//}

Example/SmartCodable/Test2ViewController.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,17 @@ class Test2ViewController: BaseViewController {
1616
override func viewDidLoad() {
1717
super.viewDidLoad()
1818

19-
let model = Model()
20-
21-
let json = model.toDictionary()
22-
23-
print(json)
19+
let dict = [
20+
"name": 1.22222
21+
]
22+
let model = Model.deserialize(from: dict)
23+
print(model)
2424

2525
}
2626

2727
struct Model: SmartCodable {
28-
var name: String = "Mccc"
29-
30-
@IgnoredKey(supportEncode: false)
31-
var ignore1: String = "忽略的key1"
28+
var name: String = ""
3229

33-
@IgnoredKey(supportEncode: true)
34-
var ignore2: String = "忽略的key2"
3530

3631
}
3732
}

Example/SmartCodable/TestViewController.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ import BTPrint
3131
*/
3232

3333

34-
3534
class TestViewController: BaseViewController {
3635

3736
override func viewDidLoad() {
3837
super.viewDidLoad()
3938

40-
41-
4239
}
4340
}

SmartCodable.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
Pod::Spec.new do |s|
1414
s.name = 'SmartCodable'
15-
s.version = '4.2.1'
15+
s.version = '4.2.2'
1616
s.summary = '数据解析库'
1717

1818
s.homepage = 'https://github.com/intsig171'

SmartCodable/Classes/SmartType/SmartPublished.swift

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,80 @@ public protocol SmartPublishedProtocol {
2626
static func createInstance(with value: Any) -> Self?
2727
}
2828

29-
/// PublishedContainer
30-
/// @Published使属性成为一个发布者,自动发布变更。
31-
/// ObservableObject可以被SwiftUI或其他观察者订阅以监听其变化。
32-
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
33-
public class PublishedContainer<Value>: ObservableObject {
34-
@Published public var wrappedValue: Value
35-
36-
public init(wrappedValue: Value) {
37-
self.wrappedValue = wrappedValue
38-
}
39-
}
4029

41-
42-
/// 属性包装器SmartPublished,允许在声明属性时附加额外的行为。
43-
/// container使用PublishedContainer管理值和发布功能。
44-
/// wrappedValue提供对实际值的访问。
4530
/// projectedValue提供一个发布者,可供订阅。
4631
@propertyWrapper
4732
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
4833
public struct SmartPublished<Value: Codable>: Codable {
49-
private var container: PublishedContainer<Value>
5034

35+
public init(from decoder: Decoder) throws {
36+
let container = try decoder.singleValueContainer()
37+
let value = try container.decode(Value.self)
38+
self.wrappedValue = value
39+
publisher = Publisher(wrappedValue)
40+
}
41+
42+
public func encode(to encoder: Encoder) throws {
43+
var container = encoder.singleValueContainer()
44+
try container.encode(self.wrappedValue)
45+
}
5146
public var wrappedValue: Value {
52-
get { container.wrappedValue }
53-
set { container.wrappedValue = newValue }
47+
// willSet 观察器在 wrappedValue 被修改前调用,会将新的值通过 publisher 发送出去,从而通知所有的订阅者。这实现了数据更新的响应式特性。
48+
willSet {
49+
publisher.subject.send(newValue)
50+
}
5451
}
5552

56-
public var projectedValue: Published<Value>.Publisher {
57-
container.$wrappedValue
53+
public var projectedValue: Publisher {
54+
publisher
5855
}
5956

60-
public init(wrappedValue: Value) {
61-
self.container = PublishedContainer(wrappedValue: wrappedValue)
57+
private var publisher: Publisher
58+
59+
public struct Publisher: Combine.Publisher {
60+
public typealias Output = Value
61+
public typealias Failure = Never
62+
63+
// CurrentValueSubject 是 Combine 中的一种 Subject,它会保存当前值并向新订阅者发送当前值。相比于 PassthroughSubject,它在初始化时就要求有一个初始值,因此更适合这种包装属性的场景。
64+
var subject: CurrentValueSubject<Value, Never>
65+
66+
// 这个方法实现了 Publisher 协议,将 subscriber 传递给 subject,从而将订阅者连接到这个发布者上。
67+
public func receive<S>(subscriber: S) where S: Subscriber, Self.Failure == S.Failure, Self.Output == S.Input {
68+
subject.subscribe(subscriber)
69+
}
70+
71+
// Publisher 的构造函数接受一个初始值,并将其传递给 CurrentValueSubject 的初始化方法。
72+
init(_ output: Output) {
73+
subject = .init(output)
74+
}
6275
}
6376

64-
public init(from decoder: Decoder) throws {
65-
let container = try decoder.singleValueContainer()
66-
let value = try container.decode(Value.self)
67-
self.container = PublishedContainer(wrappedValue: value)
77+
public init(wrappedValue: Value) {
78+
self.wrappedValue = wrappedValue
79+
publisher = Publisher(wrappedValue)
6880
}
6981

70-
public func encode(to encoder: Encoder) throws {
71-
var container = encoder.singleValueContainer()
72-
try container.encode(self.wrappedValue)
82+
83+
/// 这个下标实现了对属性包装器的自定义访问逻辑,用于在包装器内自定义 wrappedValue 的访问和修改行为。
84+
/// 参数解析:
85+
/// observed:观察者,即外部的 ObservableObject 实例。
86+
/// wrappedKeyPath:指向被包装值的引用键路径。
87+
/// storageKeyPath:指向属性包装器自身的引用键路径。
88+
public static subscript<OuterSelf: ObservableObject>(
89+
_enclosingInstance observed: OuterSelf,
90+
wrapped wrappedKeyPath: ReferenceWritableKeyPath<OuterSelf, Value>,
91+
storage storageKeyPath: ReferenceWritableKeyPath<OuterSelf, Self>
92+
) -> Value {
93+
get {
94+
observed[keyPath: storageKeyPath].wrappedValue
95+
}
96+
set {
97+
// 在设置新值之前,如果 observed 的 objectWillChange 属性是 ObservableObjectPublisher 类型,则它会发送通知,确保在属性值更新之前,订阅者能收到通知。
98+
if let subject = observed.objectWillChange as? ObservableObjectPublisher {
99+
subject.send() // 修改 wrappedValue 之前
100+
observed[keyPath: storageKeyPath].wrappedValue = newValue
101+
}
102+
}
73103
}
74104
}
75105

0 commit comments

Comments
 (0)