Reputation: 793
What I had first:
I have this method in JSQMessagesViewController.m
:
- (id<JSQMessageData>)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSAssert(NO, @"ERROR: required method not implemented: %s", __PRETTY_FUNCTION__);
return nil;
}
And then MessagesViewController.swift
with a subclass:
class MessagesViewController: JSQMessagesViewController {
with this method inside:
func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath: NSIndexPath!) -> JSQMessageData! {
return messages[messageDataForItemAtIndexPath.item]
}
A controller in the storyboard of class MessagesViewController
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.m
is being called.
What I've tried to solve it, and the real problems:
Rename the swift method to:
func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath: NSIndexPath!) -> JSQMessageData! {
Method 'collectionView(_:messageDataForItemAtIndexPath:)' with Objective-C selector 'collectionView:messageDataForItemAtIndexPath:' conflicts with method 'collectionView(_:messageDataForItemAt:)' from superclass 'JSQMessagesViewController' with the same Objective-C selector
Adding then an override prefix to prevent the conflict above:
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath: NSIndexPath!) -> JSQMessageData! {
Method does not override any method from its superclass
What I want to know:
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).
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
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