无法符合 swift 中的 objective-c 协议
Cannot conform to objective-c protocol in swift
我正在尝试实现很棒的库 JSQMessagesViewController,但我不断收到错误消息“'ModelMessage' 不符合协议 'JSQMessageData'”,即使我的 class从 NSObject 继承,我实现了所需的属性
协议如下:
@protocol JSQMessageData <NSObject>
@required
- (NSString *)senderId;
- (NSString *)senderDisplayName;
- (NSDate *)date;
- (BOOL)isMediaMessage;
- (NSUInteger)messageHash;
@optional
- (NSString *)text;
- (id<JSQMessageMediaData>)media;
@end
这是我的 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
}
}
继承自
import Foundation
class Model: NSObject
{
func getStringFromJSON(data: NSDictionary, key: String) -> String {
if let info = data[key] as? String{
return info
}
return ""
}
}
试试这个
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 ""
}
}
在实现没有可空性注释的 ObjC 协议时,尝试使用隐式解包选项。您也可以而且应该始终使用 String
而不是 NSString
.
var senderId: String!
var senderDisplayName: String!
var date: NSDate!
var isMediaMessage: Bool
var messageHash: UInt
用 @objc
注释整个 class and/or 属性也有帮助。
如果仍然不起作用,请检查构建日志,因为它会告诉您缺少或未正确实现的协议功能。
我正在尝试实现很棒的库 JSQMessagesViewController,但我不断收到错误消息“'ModelMessage' 不符合协议 'JSQMessageData'”,即使我的 class从 NSObject 继承,我实现了所需的属性
协议如下:
@protocol JSQMessageData <NSObject>
@required
- (NSString *)senderId;
- (NSString *)senderDisplayName;
- (NSDate *)date;
- (BOOL)isMediaMessage;
- (NSUInteger)messageHash;
@optional
- (NSString *)text;
- (id<JSQMessageMediaData>)media;
@end
这是我的 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
}
}
继承自
import Foundation
class Model: NSObject
{
func getStringFromJSON(data: NSDictionary, key: String) -> String {
if let info = data[key] as? String{
return info
}
return ""
}
}
试试这个
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 ""
}
}
在实现没有可空性注释的 ObjC 协议时,尝试使用隐式解包选项。您也可以而且应该始终使用 String
而不是 NSString
.
var senderId: String!
var senderDisplayName: String!
var date: NSDate!
var isMediaMessage: Bool
var messageHash: UInt
用 @objc
注释整个 class and/or 属性也有帮助。
如果仍然不起作用,请检查构建日志,因为它会告诉您缺少或未正确实现的协议功能。