为什么实现委托将值从子swift类传递到父目标C类时出错?

我正在尝试将值从快速类传递给目标C类,但出现错误。错误是

  

“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[   MainViewController childviewcontrollerResponseWithAsset:]:无法识别的选择器已发送到实例0x7f969a133c00“

childviewcontroller swift类:

@objc protocol childviewcontrollerDelegate
{
func childviewcontrollerResponse(asset:AVAsset)
}

class childviewcontroller:UIViewController
{
@objc var delegate: childviewcontrollerDelegate?
@objc var asset:AVAsset!

@objc func apply() {
self.delegate?.childviewcontrollerResponse(asset: self.Video())

//dismiss view
self.navigationController?.popViewController(animated: false)
}

}

MainViewController目标C类:

#import "Project-Swift.h"
@interface MainViewController()<childviewcontrollerDelegate>
{

-(IBaction)Buttonpressed:(UIButton *)sender{

 uistoryboard *storyboard = [uistoryboard storyboardWithName:@"Main" bundle:nil];
 childviewcontroller *vc = (childviewcontroller*)[storyboard instantiateViewControllerWithIdentifier:@"childviewcontroller"];
 AVAsset *asset = self.originalVideoAsset;
 vc.asset = asset;
 vc.delegate = self;
 [self.navigationController pushViewController:vc animated:YES];

 }

 // Define Delegate Method
 -(void)childviewcontrollerResponse:(AVAsset*)asset
 {
 self.originalVideoAsset = asset;
 }

}

我将如何解决此问题或我错了什么?

woshiwuchao1 回答:为什么实现委托将值从子swift类传递到父目标C类时出错?

Swift方法childViewControllerResponse变成了Objective-C方法childViewControllerResponseWithAsset。这就是Swift到ObjC转换的工作方式。因此,您应该将Objective-C方法重命名为:

 -(void)childViewControllerResponseWithAsset:(AVAsset*)asset
 {
    self.originalVideoAsset = asset;
 }

或者,您可以将@objc属性应用于Swift方法,并指定所需的名称:

@objc(childViewControllerResponse)
func childViewControllerResponse(asset:AVAsset)
本文链接:https://www.f2er.com/3117292.html

大家都在问