axelbrz
axelbrz

Reputation: 793

How to run a Swift method of a subclass of an Objective-C class, which has a method with the same name

What I had first:

The obvious "problem":

When I run the app I'm getting this error: ERROR: required method not implemented: -[JSQMessagesViewController collectionView:messageDataForItemAtIndexPath:]'

So, obviously the method in JSQMessagesViewController.mis being called.

What I've tried to solve it, and the real problems:

  1. Rename the swift method to:

    func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath: NSIndexPath!) -> JSQMessageData! {
    
    • But I've got: Method 'collectionView(_:messageDataForItemAtIndexPath:)' with Objective-C selector 'collectionView:messageDataForItemAtIndexPath:' conflicts with method 'collectionView(_:messageDataForItemAt:)' from superclass 'JSQMessagesViewController' with the same Objective-C selector
  2. Adding then an override prefix to prevent the conflict above:

    override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath: NSIndexPath!) -> JSQMessageData! {
    
    • But then I'm getting: Method does not override any method from its superclass

What I want to know:

  1. I want to understand that contradiction between the point 1. (conflict between a method and its superclass same-name-method) and 2. (no superclass method with the same name).

  2. To run the code assuming the controller being used is of the class MessagesViewController instead of JSQMessagesViewController (as that's the class wrote in the Identity inspector), and to call [object collectionView:..., messageDataForItemAtIndexPath:...] using the swift subclass method.

Upvotes: 0

Views: 110

Answers (1)

Maksym Musiienko
Maksym Musiienko

Reputation: 1248

Try this definition (Swift 3):

override func collectionView(_ collectionView: JSQMessagesCollectionView, messageDataForItemAt indexPath: IndexPath) -> JSQMessageData {
    // return your item
}

I used this in my project almost 6 months ago.

P.S. JSQMessagesViewController is deprecated now and no longer maintained. You can use MessageKit instead.

P.P.S. You can learn more about how objective-c methods are imported to Swift here.

Upvotes: 3

Related Questions