从Swift 4获取字符串新的密钥路径语法? [英] Getting string from Swift 4 new key path syntax?
问题描述
如何从字符串值 rel =noreferrer> Swift 4智能密钥路径 语法(例如, \Foo.bar
)?在这一点上,我对任何方式都很好奇,如果它很复杂并不重要。
How can you get a string value from Swift 4 smart keypaths syntax (e.g., \Foo.bar
)? At this point I'm curious about any way at all, does not matter if it's complicated.
我喜欢类型信息与智能密钥路径相关联的想法。但并非所有API和第三方都在那里。
I like the idea of type information being associated with smart key path. But not all APIs and 3rd parties are there yet.
通过 #keyPath(编译时验证)获取属性名字符串的旧方法)
。使用Swift 4来使用 #keyPath()
你必须将属性声明为 @objc
,这是我的意思d喜欢避免。
There's old way of getting string for property name with compile-time validation by #keyPath()
. With Swift 4 to use #keyPath()
you have to declare a property as @objc
, which is something I'd prefer to avoid.
推荐答案
对于Objective-C类的Objective-C属性,可以使用 _kvcKeyPathString
property 得到它。
For Objective-C properties on Objective-C classes, you can use the _kvcKeyPathString
property to get it.
但是,Swift键路径可能没有String等价物。它是Swift键路径的既定目标,它们不要求字段名称包含在可执行文件中。关键路径可能表示为要获取的字段偏移序列,或者可以表示要调用对象的闭包。
However, Swift key paths may not have String equivalents. It is a stated objective of Swift key paths that they do not require field names to be included in the executable. It's possible that a key path could be represented as a sequence of offsets of fields to get, or closures to call on an object.
当然,这与您的直接冲突自己的目标是避免声明属性 @objc
。我相信没有内置工具可以做你想做的事。
Of course, this directly conflicts with your own objective of avoiding to declare properties @objc
. I believe that there is no built-in facility to do what you want to do.
这篇关于从Swift 4获取字符串新的密钥路径语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!