NSDictionaryOfVariableBindings swift equivalent?

前端 未结 7 1880
闹比i
闹比i 2020-12-08 01:47

The Apple documentation shows an unsettling blank space under the \'Creating a Dictionary\' section of the UIKit reference here.

Has anyone found a replacement for

相关标签:
7条回答
  • 2020-12-08 02:33

    Once you've stored all your views as properties, you could also use reflection like so:

    extension ViewController {
        func views() -> Dictionary<String, AnyObject> {
            var views = dictionaryOfProperties()
            views.forEach {
                if !($1 is UIView) {
                    views[$0] = nil
                }
            }
            return views
        }
    }
    
    extension NSObject {
    
        func dictionaryOfProperties() -> Dictionary<String, AnyObject> {
            var result = Dictionary<String, AnyObject>()
            let mirror = Mirror(reflecting: self)
            for case let(label?, value) in mirror.children {
                result[label] = value as? AnyObject
            }
            return result
        }
    }
    
    0 讨论(0)
提交回复
热议问题