0

I have a viewcontroller in Swift that works well, and I would like to use it from an Objective-C class. My problem is that I don't know how to use it.

I try to add the delegate in the header of my main Objective-C class.

@interface MainViewController : UIViewController <PlanningSearchDelegate>

But I think as it is a swift class, it doesn't work. The error is:
Cannot find protocol declaration for PlanningSearchDelegate

Then, I tried to add the @objc word in the swift class.

import UIKit

@objc protocol PlanningSearchDelegate
{
    func planningSearchDidSelectResult(_ result:DALSearchResult)
}

class PlanningSearchViewController: UIViewController 
{
}

extension PlanningSearchViewController: UITableViewDelegate
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        let result = _results[indexPath.row]
        delegate?.planningSearchDidSelectResult(result)
    }
}

But I have an error message:

Method cannot be a member of an @objc protocol because the type of the parameter cannot be represented in Objective-C.

I put the delegate = self in the prepareForSegue of the main Objective-C class without success :

else if ([segue.identifier isEqualToString:@"showSearchViewSegue"])
{
    PlanningSearchViewController *searchViewController = segue.destinationViewController;
    searchViewController.delegate = self;
}

The error message is:
Property 'delegate' not found on object of type "PlanningSearchViewController *'

Here is the DALSearchResult Class :

import Foundation

class DALSearchResult
{
    var contactId: Int
    var firstname: String
    var lastname: String
    var email: String
    var phone: String
    var city: String
    var registrationId: Int?
    var slotId: Int?
    var vehicleId: Int?
    var startDate: Date?
    var vehicleName: String?

    init(contactId:Int, firstname:String, lastname:String, email:String, phone:String, city:String)
    {
        self.contactId = contactId
        self.firstname = firstname
        self.lastname = lastname
        self.email = email
        self.phone = phone
        self.city = city
    }

    convenience init(registrationId:Int, slotId:Int, vehicleId:Int, contactId:Int, firstname:String, lastname:String, email:String, phone:String, city:String, startDate:Date, vehicleName: String)
    {
        self.init(contactId: contactId, firstname: firstname, lastname: lastname, email: email, phone: phone, city: city)

        self.registrationId = registrationId
        self.vehicleId = vehicleId
        self.slotId = slotId
        self.startDate = startDate
        self.vehicleName = vehicleName
    }
}

Anyone has an idea ?

Thanks in advance.

9
  • What is DALSearchResult ? Commented Apr 12, 2017 at 9:38
  • It is a class I call in the swift that contain informations on the concerning search of people (firstname, lastname, phone, email, etc...) Commented Apr 12, 2017 at 9:40
  • 2
    try class DALSearchResult: NSObject Commented Apr 12, 2017 at 9:43
  • 1
    1. does your MainViewController implements protocol method planningSearchDidSelectResult? >> 2. Does this if ([segue.identifier isEqualToString:@"showSearchViewSegue"]) gets called? 3. How delegate var declared? Commented Apr 12, 2017 at 10:10
  • 1
    Ok that was the problem :D Thanks Commented Apr 12, 2017 at 10:45

3 Answers 3

2

Make DALSearchResult class Objective-C compatible. If your DALSearchResult does not extend from NSObject, you can extend it form NSObject.

Sign up to request clarification or add additional context in comments.

Comments

0

Can you share how the declaration of DALSearchResult looks like? Maybe it's an enum which cannot be translated to Obj-C. In this case you might need to pass the result in a different way.

4 Comments

I declare it like this : var results:[DALSearchResult] = [], I put it in the message just now.
As others suggested, just try @objc class DALSearchResult: NSObject where you define your class.
I put it, it is better, it accepts the class DALSearchResult declaration now, but now it puts me another message : [MainViewController planningSearchDidSelectResult:]: unrecognized selector sent to instance. The mainviewController is my main objective-C class
Are you actually implementing the method in MainViewController?
0

To access swift from objective-C you have to import the swift.h always in your class that you want to use it like so

#import "Yourprojectname-Swift.h"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.