Nurdin
Nurdin

Reputation: 23893

Swift - Type '*' does not conform to protocol '*'

I got this error message when trying to create a Message class.

'Message' does not conform to protocol 'JSQMessageData'

JSQMessageData got from https://github.com/jessesquires/JSQMessagesViewController

enter image description here

My code

import Foundation

class Message : NSObject, JSQMessageData {

    var senderId_ : String!
    var senderDisplayName_ : String!
    var date_ : NSDate
    var isMediaMessage_ : Bool
    var hash_ : Int = 0
    var text_ : String

    init(senderId: String, senderDisplayName: String?, isMediaMessage: Bool, hash: Int, text: String) {
        self.senderId_ = senderId
        self.senderDisplayName_ = senderDisplayName
        self.date_ = NSDate()
        self.isMediaMessage_ = isMediaMessage
        self.hash_ = hash
        self.text_ = text
    }

    func senderId() -> String! {
        return senderId_;
    }

    func senderDisplayName() -> String! {
        return senderDisplayName_;
    }

    func date() -> NSDate! {
        return date_;
    }

    func isMediaMessage() -> Bool! {
        return isMediaMessage_;
    }

    func hash() -> Int? {
        return hash_;
    }

    func text() -> String! {
        return text_;
    }
}

JSQMessageData.h

#import <Foundation/Foundation.h>
#import "JSQMessageMediaData.h"

@protocol JSQMessageData <NSObject>

@required

- (NSString *)senderId;
- (NSString *)senderDisplayName;
- (NSDate *)date;
- (BOOL)isMediaMessage;
- (NSUInteger)hash;

@optional

- (NSString *)text;
- (id<JSQMessageMediaData>)media;

@end

How to fix this protocol issue?

Upvotes: 1

Views: 4491

Answers (2)

Cezar
Cezar

Reputation: 56352

There are two problems:

  1. hash is defined as a method that returns an NSUInteger in the protocol. Also, NSUInteger can't be nil in Objective-C, so you can't return an optional. You need to change its implementation in the Message class to return an UInt

    func hash() -> UInt {
        return UInt(hash_);
    }
    

or simply return hash_ and change hash_ from Int to UInt itself.

  1. BOOL can't be nil in Objective-C, so to conform to the protocol you need to change isMediaMessage() to return a non optional Bool by removing the !:

    func isMediaMessage() -> Bool {
        return isMediaMessage_;
    }
    

Upvotes: 6

mastro35
mastro35

Reputation: 143

The problem could be "hash", that is an unsigned int in the protocol and just an int in the implementation.

Upvotes: 0

Related Questions