反射(I)
程序员文章站
2022-07-07 23:40:52
反射获取属性和属性值 1 let item = DoctorGroup() 2 guard let dic = InterfaceTests.obtainValues(subObject: item) else { 3 XCTFail("testFetchMyGroups is \(dataIsEm ......
反射获取属性和属性值
1 let item = DoctorGroup() 2 guard let dic = InterfaceTests.obtainValues(subObject: item) else { 3 XCTFail("testFetchMyGroups is \(dataIsEmpty)") 4 return 5 } 6 7 8 for (_, value) in dic { 9 print("------------>\(value)") 10 } 11 12 13 // 获取Model的属性和值 纯Swift专用 14 class func obtainValues<T>(subObject: T) -> [String: Any]? { 15 var resultDic = [String: Any]() 16 17 let mirror = Mirror(reflecting: subObject) 18 for (name, value) in mirror.children { 19 resultDic[name!] = value 20 } 21 22 var superMirror: Mirror? = mirror.superclassMirror 23 24 while superMirror != nil { 25 for (name, value) in (superMirror?.children)! { 26 resultDic[name!] = value 27 } 28 superMirror = superMirror?.superclassMirror //获取父类的属性和值 29 } 30 return resultDic 31 }
上一篇: yuv转opencv中的IplImage
推荐阅读
-
Redmi微博发布会来了:红米Note 7 Pro支持P2i防水
-
锐龙7 4800H最新测试成绩曝光:CPU战i9 核显战MX250
-
AMD发布2款全新锐龙9系4000处理器:8核16线程 力战酷睿i9
-
深入分析Java I/O 工作机制
-
【java基础】程序员你真的理解反射机制吗?
-
华为freebuds4i和freebudspro哪款值得买 freebuds4i和freebudspro对比评测
-
10nm 11代发布在即 Intel又发了一颗14nm 10代酷睿i5-10500H
-
CentOS 6.5 i386 安装MySQL 5.7.18详细教程
-
PHP反射实际应用示例
-
Intel Comet Lake家族酷睿i3-10100桌面处理器现身:首次4核8线程