简体   繁体   English

Swift - 类型“*”不符合协议“*”

[英]Swift - Type '*' does not conform to protocol '*'

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 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

How to fix this protocol issue?如何解决此协议问题?

There are two problems:有两个问题:

  1. hash is defined as a method that returns an NSUInteger in the protocol. hash被定义为在协议中返回NSUInteger的方法。 Also, NSUInteger can't be nil in Objective-C, so you can't return an optional.此外, NSUInteger在 Objective-C 中不能为 nil,所以你不能返回一个可选项。 You need to change its implementation in the Message class to return an UInt您需要在Message类中更改其实现以返回UInt

     func hash() -> UInt { return UInt(hash_); }

or simply return hash_ and change hash_ from Int to UInt itself.或者简单地return hash_并将hash_Int更改为UInt本身。

  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 ! BOOL不能在Objective-C为零,所以符合你需要改变协议isMediaMessage()返回一个非可选Bool通过删除! :

     func isMediaMessage() -> Bool { return isMediaMessage_; }

问题可能是“散列”,即协议中的 unsigned int 和实现中的 int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型不符合协议Swift - Type does not conform to protocol Swift 类型不符合协议序列类型 - Swift - type does not conform to protocol Sequence Type - Swift 类型“NSPersistentStore”在swift中不符合协议“BooleanType” - Type 'NSPersistentStore' does not conform to protocol 'BooleanType' in swift Swift:类型&#39;ViewController&#39;不符合协议&#39;UIPageViewControllerDataSource&#39; - Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource' Swift 2.0类型&#39;()&#39;不符合协议 - Swift 2.0 Type '()' does not conform to protocol Swift:`类型&#39;[String]&#39;不符合协议&#39;StringLiteralConvertible&#39; - Swift: `Type '[String]' does not conform to protocol 'StringLiteralConvertible'` 类型“myViewController”不符合Swift中的协议UIPIckerDataSource - Type “myViewController” does not conform to protocol UIPIckerDataSource in Swift Swift - MultipeerConnectivity类型不符合协议 - Swift - MultipeerConnectivity Type does not conform to protocol Swift-类型&#39;MenuViewController&#39;不符合协议&#39;GKGameCenterControllerDelegate&#39; - Swift - Type 'MenuViewController' does not conform to protocol 'GKGameCenterControllerDelegate' Swift - 结构类型不符合协议 - Swift - struct type does not conform to protocol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM