3030
3131 */
3232
33-
3433import Foundation
3534
3635open class AAObject {
37- public init ( ) {
38-
39- }
36+ public init ( ) { }
4037}
4138
42-
4339@available ( iOS 10 . 0 , macCatalyst 13 . 1 , macOS 10 . 13 , * )
4440public extension AAObject {
4541 var classNameString : String {
@@ -57,68 +53,60 @@ public protocol AASerializableWithComputedProperties {
5753@available ( iOS 10 . 0 , macCatalyst 13 . 1 , macOS 10 . 13 , * )
5854public extension AAObject {
5955
60- fileprivate func loopForMirrorChildren( _ mirrorChildren: Mirror . Children , _ representation: inout [ String : Any ] ) {
56+ fileprivate func loopForMirrorChildren( _ mirrorChildren: Mirror . Children , _ representation: inout [ String : Any ] ) {
6157 for case let ( label? , value) in mirrorChildren {
62- switch value {
63- case let value as AAObject :
58+ if let value = value as? AAObject {
6459 representation [ label] = value. toDic ( )
65-
66- case let value as [ AAObject ] :
67- let valueCount = value. count
68- var aaObjectArr = [ Any] ( )
69- aaObjectArr. reserveCapacity ( valueCount)
70-
71- for aaObject in value {
72- aaObjectArr. append ( aaObject. toDic ( ) )
73- }
74-
75- representation [ label] = aaObjectArr
76-
77- case let value as NSObject :
60+ } else if let value = value as? [ AAObject ] {
61+ // 使用 map 简化数组转换
62+ representation [ label] = value. map { $0. toDic ( ) }
63+ } else if let value = value as? NSObject {
7864 representation [ label] = value
79-
80- default :
81- // Ignore any unserializable properties
82- break
8365 }
8466 }
8567 }
8668
8769 func toDic( ) -> [ String : Any ] {
88- // Create mirror once
70+ // 创建 Mirror 对象
8971 let mirror = Mirror ( reflecting: self )
9072
91- // Estimate capacity based on property count
92- let estimatedCapacity = mirror. children. underestimatedCount +
93- ( mirror. superclassMirror? . children. underestimatedCount ?? 0 ) + 5
94-
73+ // 预估容量
74+ let estimatedCapacity = mirror. children. underestimatedCount +
75+ ( mirror. superclassMirror? . children. underestimatedCount ?? 0 ) + 5
9576 var representation = [ String: Any] ( minimumCapacity: estimatedCapacity)
9677
97- // 遍历当前类的反射子属性
98- loopForMirrorChildren ( mirror. children, & representation)
99-
100- // 遍历父类的反射子属性
101- if let superMirror = mirror. superclassMirror, !superMirror. children. isEmpty {
102- loopForMirrorChildren ( superMirror. children, & representation)
78+ // 遍历当前类和父类的反射子属性
79+ var currentMirror : Mirror ? = mirror
80+ while let current = currentMirror {
81+ loopForMirrorChildren ( current. children, & representation)
82+ currentMirror = current. superclassMirror
10383 }
10484
105- // 如果实现了 SerializableWithComputedProperties 协议,获取计算属性
85+ // 添加计算属性
86+ addComputedProperties ( to: & representation)
87+
88+ return representation
89+ }
90+
91+ private func addComputedProperties( to representation: inout [ String : Any ] ) {
10692 if let selfWithComputed = self as? AASerializableWithComputedProperties {
10793 let computedProps = selfWithComputed. computedProperties ( )
10894 for (key, value) in computedProps {
10995 representation [ key] = value
11096 }
11197 }
112-
113- return representation
11498 }
11599
116100 func toJSON( ) -> String {
117101 do {
118102 let data = try JSONSerialization . data ( withJSONObject: toDic ( ) , options: [ . fragmentsAllowed] )
119- return String ( data: data, encoding: . utf8) ?? " "
120- } catch {
121- print ( " JSON serialization error: \( error) " )
103+ guard let jsonString = String ( data: data, encoding: . utf8) else {
104+ print ( " JSON encoding error: Unable to convert data to String. " )
105+ return " "
106+ }
107+ return jsonString
108+ } catch let error as NSError {
109+ print ( " JSON serialization error: \( error. localizedDescription) " )
122110 return " "
123111 }
124112 }
0 commit comments