Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting string from Swift 4 new key path syntax?

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.

I like the idea of type information being associated with smart key path. But not all APIs and 3rd parties are there yet.

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.

like image 437
Dannie P Avatar asked Oct 02 '17 15:10

Dannie P


1 Answers

A bit late to the party, but I've stumbled upon a way of getting a key path string from NSObject subclasses at least:

NSExpression(forKeyPath: \UIView.bounds).keyPath 
like image 183
Andy Heard Avatar answered Sep 28 '22 18:09

Andy Heard