I got this error message when trying to create a Message
class.
尝试创建Message类时出现此错误消息。
'Message' does not conform to protocol 'JSQMessageData'
JSQMessageData
got from https://github.com/jessesquires/JSQMessagesViewController
JSQMessageData来自https://github.com/jessesquires/JSQMessagesViewController
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 个解决方案
#1
6
There are two problems:
有两个问题:
-
hash
is defined as a method that returns anNSUInteger
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 theMessage
class to return anUInt
hash被定义为在协议中返回NSUInteger的方法。另外,NSUInteger在Objective-C中不能为零,因此您无法返回可选项。您需要在Message类中更改其实现以返回UInt
func hash() -> UInt { return UInt(hash_); }
or simply return hash_
and change hash_
from Int
to UInt
itself.
或者简单地返回hash_并将hash从Int更改为UInt本身。
-
BOOL
can't be nil in Objective-C, so to conform to the protocol you need to changeisMediaMessage()
to return a non optionalBool
by removing the!
:在Objective-C中BOOL不能为零,因此要符合您需要更改的协议isMediaMessage()以通过删除!来返回非可选的Bool:
func isMediaMessage() -> Bool { return isMediaMessage_; }
#2
0
The problem could be "hash", that is an unsigned int in the protocol and just an int in the implementation.
问题可能是“哈希”,即协议中的unsigned int,只是实现中的int。
#1
6
There are two problems:
有两个问题:
-
hash
is defined as a method that returns anNSUInteger
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 theMessage
class to return anUInt
hash被定义为在协议中返回NSUInteger的方法。另外,NSUInteger在Objective-C中不能为零,因此您无法返回可选项。您需要在Message类中更改其实现以返回UInt
func hash() -> UInt { return UInt(hash_); }
or simply return hash_
and change hash_
from Int
to UInt
itself.
或者简单地返回hash_并将hash从Int更改为UInt本身。
-
BOOL
can't be nil in Objective-C, so to conform to the protocol you need to changeisMediaMessage()
to return a non optionalBool
by removing the!
:在Objective-C中BOOL不能为零,因此要符合您需要更改的协议isMediaMessage()以通过删除!来返回非可选的Bool:
func isMediaMessage() -> Bool { return isMediaMessage_; }
#2
0
The problem could be "hash", that is an unsigned int in the protocol and just an int in the implementation.
问题可能是“哈希”,即协议中的unsigned int,只是实现中的int。