1

I'm creating an iOS app with Swift. I discovered an animation I'd like to implement in my table view, but the code is in Objective-C.

Repository: https://github.com/recruit-mp/RMPZoomTransitionAnimator

I have successfully bridged Obj-C code to Swift but can't seem to conform to a required protocol.

The protocol:

@protocol RMPZoomTransitionAnimating <NSObject> @required - (UIImageView *)transitionSourceImageView; - (UIColor *)transitionSourceBackgroundColor; - (CGRect)transitionDestinationImageViewFrame; @end 

My Swift implementation:

First class that implements the protocol:

class ChallengeViewController: UIViewController, RMPZoomTransitionAnimating func transitionSourceImageView() -> UIImageView { return imageView } func transitionSourceBackgroundColor() -> UIColor { return UIColor.whiteColor() } func transitionDestinationImageViewFrame() -> CGRect { return imageView.frame } 

Second class:

class ChallengeTableViewController: UITableViewController, RMPZoomTransitionAnimating func transitionSourceImageView() -> UIImageView { return imageForTransition! } func transitionSourceBackgroundColor() -> UIColor { return UIColor.whiteColor() } func transitionDestinationImageViewFrame() -> CGRect { return imageFrame! } 

This check that occurs before animating always fails:

Protocol *animating = @protocol(RMPZoomTransitionAnimating); BOOL doesNotConfirmProtocol = ![self.sourceTransition conformsToProtocol:animating] || ![self.destinationTransition conformsToProtocol:animating]; 

I've read this topic How to create class methods that conform to a protocol shared between Swift and Objective-C? but didn't found any help

Any clues would be really appreciated

5
  • 1
    There is a sourceTransition and a destinationTransition. They are two different objects. They must both conform to RMPZoomTransitionAnimating. But you have only shown one object that conforms to RMPZoomTransitionAnimating. Where's the other one? Commented Oct 24, 2015 at 23:02
  • Possible duplicate of How to create class methods that conform to a protocol shared between Swift and Objective-C? Commented Oct 25, 2015 at 2:12
  • @Jeef I'll have a look at it now Commented Oct 25, 2015 at 7:52
  • Okay, so what's your evidence that "This check that occurs before animating always fails"? You do conform to the protocol; if you didn't, the compiler would stop you and the code wouldn't even run. So what's the actual problem at this point? Commented Oct 25, 2015 at 14:33
  • Also - its possible you are going to have to use UnsafeMutalbePointers Commented Oct 25, 2015 at 16:11

1 Answer 1

0

Swift classes by themselves are not (by default) Objective-C Compatible.

You get compatibility by either inheriting from NSObject or adding @objc in front of your class. I suspect this "may" be your issues - but I unfortunately can't test it at the moment.

You may also have to add a few initializers like in your case one from NSCoder or something - I don't unfortunately recall off the top of my head - and I don't have access to Xcode right now.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Try:

@objc class ChallengeViewController: UIViewController, RMPZoomTransitionAnimating func transitionSourceImageView() -> UIImageView { return imageView } func transitionSourceBackgroundColor() -> UIColor { return UIColor.whiteColor() } func transitionDestinationImageViewFrame() -> CGRect { return imageView.frame } 

This will tell the compiler your class is objective-c compatible

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.