I'm trying to rewrite an iOS project in Objective-C to Swift in Xcode 6.1 but I can't "translate" this Objective-C line :
CGFloat imageRotation = [[self.imageView valueForKeyPath:@"layer.presentationLayer.transform.rotation.z"] floatValue];
How can I get my UIImageView rotation value in Swift?
It is only slightly more complex in Swift because
valueForKeyPathreturns an optional which has to be unwrapped an then explicitly cast toNSNumber. This can (for example) be done with a combination of optional chaining and optional cast:The final "nil-coalescing operator"
??sets the value to0.0if the key path is not set (or is not anNSNumber), this mimics the behaviour of Objective-C where sending thefloatValuemessage tonilwould also return0.0.