我正在尝试在Swift中重新创建Ray Wenderlich教程,但同时使用Swift和Objective C.主要部分(即视图控制器和模型)在Swift中完成,并使用桥接头来存在.h和.m类
我无法让我的快速视图控制器符合obj-c委托。
在我的.h档案中,我有:
@class RWTRateView;
@protocol RWTRateViewDelegate
- (void)rateView:(RWTRateView *)rateView ratingDidChange:(float)rating;
@end
@interface RWTRateView : UIView
...
@property (assign) id <RWTRateViewDelegate> delegate;
@end
在我的swift文件中,我有:
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
}
}
...
}
出于某种原因,我收到一条错误消息,指出Type&#39; DetailViewController&#34;不符合协议&#39; RWTRateViewDelegate&#39;而且我不太清楚为什么。
完整代码位于https://github.com/dwmchan/ScaryBugsSwift。
感谢对此提出的任何反馈,因为我过去3天都在网上寻找答案,但却找不到任何相关内容。
答案 0 :(得分:1)
你有:
func rateView(rateView:RWTRateView!, ratingDidChange rating:Float!) ->Void {
我的Xcode 6(beta 2)将此行自动填充为
func rateView(rateView: RWTRateView!, ratingDidChange rating: CFloat) {
请注意,第二个参数的类型为CFloat
,而不是Float!
。