How to use Swift 4 KeyPath in CAShapeLayer animation
If I write Swift 3 code it looks like this:
let animation = CABasicAnimation(keyPath: #keyPath(CAShapeLayer.path))
But I tried using Swift 4 new syntax for keyPath and I got:
let keyPath = \CAShapeLayer.path
let animation = CABasicAnimation(keyPath: keyPath) // error line
Error : Unable to convert value of type 'ReferenceWritableKeyPath' to expected argument type 'String?'
How can I use the key path in this situation with swift 4?
+3
source to share
1 answer
It CABasicAnimation
still uses the old String
keyPaths for now, so you should still use #keyPath(CAShapeLayer.path)
it even if you are using Swift 4.
Apple is likely to update all of its APIs in the future to use these more secure keypath links. But for now you are stuck with "unsafe" strings.
+3
source to share