How to conform to an objective c protocol in an ex

2019-07-22 13:32发布

I am trying to recreate a Ray Wenderlich tutorial in Swift, but using both Swift and Objective C. The main part (i.e. view controllers and models) is done in Swift and using a bridging header for existing .h and .m classes.

I am having trouble trying to get my swift view controller to conform to a obj-c delegate.

In my .h file I have:

@class RWTRateView;

@protocol RWTRateViewDelegate
- (void)rateView:(RWTRateView *)rateView ratingDidChange:(float)rating;
@end
@interface RWTRateView : UIView
...
@property (assign) id <RWTRateViewDelegate> delegate;

@end

In my swift file I have:

class DetailViewController: UIViewController, UITextFieldDelegate, RWTRateViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet var rateView : RWTRateView
...
    func configureView() {
            // Update the user interface for the detail item.
            if let detail:RWTScaryBugDoc = self.detailItem as? RWTScaryBugDoc {
                if let rv = self.rateView {
                    rv.notSelectedImage = UIImage(named: "shockedface2_empty.png")
                    rv.halfSelectedImage = UIImage(named: "shockedface2_half.png")
                    rv.fullSelectedImage = UIImage(named: "shockedface2_full.png")
                    rv.editable = true
                    rv.maxRating = 5
                    rv.delegate = self as RWTRateViewDelegate
                    rv.rating = detail.data.rating!
                    self.titleField.text = detail.data.title
                    self.imageView.image = detail.fullImage
                }
            }
        }
    ...

    func rateView(rateView:RWTRateView!, ratingDidChange rating:Float!) ->Void {

        if let detail = self.detailItem as? RWTScaryBugDoc {
            detail.data.rating = rating
        }
    }

...
}

For some reason, I'm getting an error message saying that Type 'DetailViewController" does not conform to protocol 'RWTRateViewDelegate' and I'm not quite sure why.

The full code is at https://github.com/dwmchan/ScaryBugsSwift.

Would appreciate any feedback on this because I've spent the last 3 days looking for answers online but couldn't find anything on this.

1条回答
放我归山
2楼-- · 2019-07-22 13:43

You have:

func rateView(rateView:RWTRateView!, ratingDidChange rating:Float!) ->Void {

My Xcode 6 (beta 2) autocompletes this line as

func rateView(rateView: RWTRateView!, ratingDidChange rating: CFloat) {

Note that second parameter's type is CFloat, not Float!.

查看更多
登录 后发表回答