Reputation: 45
I'm trying to extract animations from a DAE file, how do I use this method in swift?
Thanks,
Objective-C code is:
//CAAnimation *animation = [sceneSource entryWithIdentifier:animationIDs[index] withClass:[CAAnimation class]];
func extractAnimationsFromSceneSource(sceneSource: SCNSceneSource) {
let animationsIDs: NSArray = [sceneSource.isKindOfClass(CAAnimation)]
let animationCount: Int = animationsIDs.count
var longAnimations = NSMutableArray(capacity: animationCount)
let maxDuration: CFTimeInterval = 0
for index in 0..animationCount {
// gets error CAAnimation is not convertible to 'AnyClass'
let animation = sceneSource.entryWithIdentifier(animationsIDs[index], withClass:CAAnimation())
}
}
Upvotes: 0
Views: 1880
Reputation: 126137
There are a couple of issues you'll need to deal with here:
[SomeClass class]
is SomeClass.self
.entryWithIdentifier
returns an id
in ObjC, you'll typically want to cast it to an appropriate Swift type (because you can't call methods or access properties on an AnyObject
).entryWithIdentifier
can return nil
in ObjC if no entry is found, which means it returns an optional in Swift. Bridging makes that an implicitly unwrapped optional, which puts you one step away from a crash if you're planning to add these animations to an array or otherwise use them — best to check the optional yourself.Also, your array of animationIDs
looks suspect — you're assigning an array with one element, a Boolean that's the result of asking the scene source what class it is. (And this Boolean is always false
, because an SCNSceneSource
is not a kind of CAAnimation
.)
So, your method might look something like this:
func extractAnimationsFromSceneSource(sceneSource: SCNSceneSource) {
let animationsIDs = source.identifiersOfEntriesWithClass(CAAnimation.self) as String[]
var animations: CAAnimation[] = []
for animationID in animationsIDs {
if let animation = source.entryWithIdentifier(animationID, withClass: CAAnimation.self) as? CAAnimation {
animations += animation
}
}
}
(Includes a bit of attempting to infer your intent, and slimming code down where type inference allows such.)
Upvotes: 2