在swift中不能符合objective-c协议

时间:2022-12-26 16:10:40

I'm trying to implement the awesome library JSQMessagesViewController but I keep getting the error "'ModelMessage' does not conform to protocol 'JSQMessageData'" even though my class inherit from NSObject and I implement the required properties

我正在尝试实现令人敬畏的库JSQMessagesViewController,但我不断收到错误“'ModelMessage'不符合协议'JSQMessageData'”,即使我的类继承自NSObject并实现了所需的属性

Here is the protocol:

这是协议:

@protocol JSQMessageData <NSObject>

@required
 - (NSString *)senderId;
 - (NSString *)senderDisplayName;
 - (NSDate *)date;
 - (BOOL)isMediaMessage;
 - (NSUInteger)messageHash;

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

@end

and here is my class

这是我的课

import Foundation

class ModelMessage: Model, JSQMessageData {

/* Required */
var senderId: NSString
var senderDisplayName: NSString
var date: NSDate
var isMediaMessage: Bool
var messageHash: UInt
/**/

var text_: String

var id:String!
var conversationId: String!
var imageUrl: String?
var recipientId: String!
var recipientRead: Bool!

var avatar: NSData?


init(data: NSDictionary){

    super.init()

    /* Required */
    self.senderId = getStringFromJSON(data, key:"senderId")
    self.senderDisplayName = "Me"
    let dateString = getStringFromJSON(data, key: "timeStamp")
    let dateDouble = (dateString as NSString).doubleValue / 1000.0
    self.date = NSDate(timeIntervalSince1970: dateDouble)
    self.isMediaMessage = false
    /**/

    self.text_ = getStringFromJSON(data, key:"text")

    self.id = getStringFromJSON(data, key:"id")
    self.conversationId = getStringFromJSON(data, key:"conversationId")
    self.recipientId = getStringFromJSON(data, key:"recipientId")
    self.recipientRead = data["recipientRead"] as! Bool

}

}

which inherit from

继承自

import Foundation

class Model: NSObject
{

func getStringFromJSON(data: NSDictionary, key: String) -> String {

    if let info = data[key] as? String{
        return info
    }
    return ""

}

}

2 个解决方案

#1


Try using implicitly unwrapped optionals when implementing ObjC protocols without nullability annotations. Also you can and should always use String instead of NSString.

在实现没有可空性注释的ObjC协议时,尝试使用隐式展开的选项。您也可以并且应该始终使用String而不是NSString。

var senderId: String!
var senderDisplayName: String!
var date: NSDate!
var isMediaMessage: Bool
var messageHash: UInt

Annotating the whole class and/or the properties with @objc could also help.

使用@objc注释整个类和/或属性也可以提供帮助。

If that still doesn't work then check the build log as it will tell you what protocol function is missing or not implemented correctly.

如果仍然无效,请检查构建日志,因为它会告诉您缺少哪些协议功能或未正确实现。

#2


try this

import Foundation

class ModelMessage: NSObject , JSQMessageData {

/* Required */
var senderId: NSString
var senderDisplayName: NSString
var date: NSDate
var isMediaMessage: Bool
var messageHash: UInt
/**/

var text_: String

var id:String!
var conversationId: String!
var imageUrl: String?
var recipientId: String!
var recipientRead: Bool!

var avatar: NSData?


init(data: NSDictionary){

    super.init()

    /* Required */
    self.senderId = getStringFromJSON(data, key:"senderId")
    self.senderDisplayName = "Me"
    let dateString = getStringFromJSON(data, key: "timeStamp")
    let dateDouble = (dateString as NSString).doubleValue / 1000.0
    self.date = NSDate(timeIntervalSince1970: dateDouble)
    self.isMediaMessage = false
    /**/

    self.text_ = getStringFromJSON(data, key:"text")

    self.id = getStringFromJSON(data, key:"id")
    self.conversationId = getStringFromJSON(data, key:"conversationId")
    self.recipientId = getStringFromJSON(data, key:"recipientId")
    self.recipientRead = data["recipientRead"] as! Bool

}

func getStringFromJSON(data: NSDictionary, key: String) -> String {

    if let info = data[key] as? String{
        return info
    }
    return ""

}


}

#1


Try using implicitly unwrapped optionals when implementing ObjC protocols without nullability annotations. Also you can and should always use String instead of NSString.

在实现没有可空性注释的ObjC协议时,尝试使用隐式展开的选项。您也可以并且应该始终使用String而不是NSString。

var senderId: String!
var senderDisplayName: String!
var date: NSDate!
var isMediaMessage: Bool
var messageHash: UInt

Annotating the whole class and/or the properties with @objc could also help.

使用@objc注释整个类和/或属性也可以提供帮助。

If that still doesn't work then check the build log as it will tell you what protocol function is missing or not implemented correctly.

如果仍然无效,请检查构建日志,因为它会告诉您缺少哪些协议功能或未正确实现。

#2


try this

import Foundation

class ModelMessage: NSObject , JSQMessageData {

/* Required */
var senderId: NSString
var senderDisplayName: NSString
var date: NSDate
var isMediaMessage: Bool
var messageHash: UInt
/**/

var text_: String

var id:String!
var conversationId: String!
var imageUrl: String?
var recipientId: String!
var recipientRead: Bool!

var avatar: NSData?


init(data: NSDictionary){

    super.init()

    /* Required */
    self.senderId = getStringFromJSON(data, key:"senderId")
    self.senderDisplayName = "Me"
    let dateString = getStringFromJSON(data, key: "timeStamp")
    let dateDouble = (dateString as NSString).doubleValue / 1000.0
    self.date = NSDate(timeIntervalSince1970: dateDouble)
    self.isMediaMessage = false
    /**/

    self.text_ = getStringFromJSON(data, key:"text")

    self.id = getStringFromJSON(data, key:"id")
    self.conversationId = getStringFromJSON(data, key:"conversationId")
    self.recipientId = getStringFromJSON(data, key:"recipientId")
    self.recipientRead = data["recipientRead"] as! Bool

}

func getStringFromJSON(data: NSDictionary, key: String) -> String {

    if let info = data[key] as? String{
        return info
    }
    return ""

}


}