spectre_d2
spectre_d2

Reputation: 203

Why am I getting error to implement delegate to pass value from a child swift class to a parent objective C class?

I am trying to pass value from a swift class to an objective C class, but get an error. The error is

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

ChildViewController swift class:

@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 class:

#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;
 }

}

How I will solve this problem or what did I wrong?

Upvotes: 0

Views: 71

Answers (1)

Sweeper
Sweeper

Reputation: 271905

The Swift method childViewControllerResponse turns into the Objective-C method childViewControllerResponseWithAsset. That is just how Swift-to-ObjC conversion works. Therefore, you should rename your Objective-C method to:

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

Alternatively, you can apply a @objc attribute to the Swift method, and specify the name you want:

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

Upvotes: 1

Related Questions