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

Community
  • 1
  • 1
Mindaugas
  • 69
  • 1
  • 6
  • 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? – matt Oct 24 '15 at 23:02
  • Possible duplicate of [How to create class methods that conform to a protocol shared between Swift and Objective-C?](http://stackoverflow.com/questions/29399871/how-to-create-class-methods-that-conform-to-a-protocol-shared-between-swift-and) – Jeef Oct 25 '15 at 02:12
  • @Jeef I'll have a look at it now – Mindaugas Oct 25 '15 at 07: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? – matt Oct 25 '15 at 14:33
  • Also - its possible you are going to have to use UnsafeMutalbePointers – Jeef Oct 25 '15 at 16:11

1 Answers1

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

Jeef
  • 26,861
  • 21
  • 78
  • 156