Swift - Type' *'不符合协议' *'

时间:2015-03-10 10:34:08

标签: ios objective-c swift

尝试创建Message课程时收到此错误消息。

'Message' does not conform to protocol 'JSQMessageData'

JSQMessageData来自https://github.com/jessesquires/JSQMessagesViewController

enter image description here

我的代码

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

请告知如何解决此协议问题。谢谢。

2 个答案:

答案 0 :(得分:6)

有两个问题:

  1. hash被定义为在协议中返回NSUInteger的方法。此外,NSUInteger在Objective-C中不能为零,因此您无法返回可选项。您需要在Message类中更改其实现以返回UInt

    func hash() -> UInt {
        return UInt(hash_);
    }
    
  2. 或只是return hash_并将hash_Int更改为UInt本身。

    1. BOOL在Objective-C中不能为零,因此要符合协议,您需要通过删除{isMediaMessage()来返回非可选Bool {1}}:

      !

答案 1 :(得分:0)

问题可能是“hash”,即协议中的unsigned int,只是实现中的int。