gpt4 book ai didi

objective-c - 为什么我在实现委托(delegate)以将值从子 swift 类传递到父 Objective-C 类时出错?

转载 作者:行者123 更新时间:2023-11-28 13:23:01 26 4
gpt4 key购买 nike

我试图将值从 swift 类传递到 Objective-C 类,但出现错误。错误是

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ MainViewController childViewControllerResponseWithAsset:]: unrecognized selector sent to instance 0x7f969a133c00"

ChildViewController 快速类:

@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 Objective-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;
}

}

我将如何解决这个问题或者我做错了什么?

最佳答案

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

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

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

@objc(childViewControllerResponse)
func childViewControllerResponse(asset:AVAsset)

关于objective-c - 为什么我在实现委托(delegate)以将值从子 swift 类传递到父 Objective-C 类时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58819144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com