简体   繁体   中英

Can't conform to obj-c protocol in Swift

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM