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

  • Best way to cache images on ios app?
  • send image along with other parameters with AFNetworking
  • How to update a UITableVIewCell without reloading full table
  • How to get number of days between two dates objective-c
  • UICollectionVIew : How can i remove the space on top first cell's row
  • MPMediaItemArtwork is null while cover is available in iTunes
  • 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
    

    Please advice how to fix this protocol issue. Thank you.

    2 Solutions Collect From Internet About “Swift – Type '*' does not conform to protocol '*'”

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

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