NSDictionaryOfVariableBindings swift equivalent?

前端 未结 7 1904
闹比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:16

    So I hacked something together which seems to work:

    func dictionaryOfVariableBindings(container: Any, views:UIView...) -> Dictionary {
        var d = Dictionary()
        let mirror = Mirror(reflecting: container)
        let _ = mirror.children.compactMap {
            guard let name = $0.label, let view = $0.value as? UIView else { return }
            guard views.contains(view) else { return }
            d[name] = view
        }
        return d
    }
    

    Usage:

            let views = dictionaryOfVariableBindings(container: self, views: imageView)
    
    

提交回复
热议问题