无法将“用户”类型的值转换为预期的参数类型“海报”?

时间:2019-08-03 22:20:53

标签: xcode firebase notifications swipe

我正在尝试将通知发送到Firebase数据库。尝试运行代码时遇到错误,额外参数'post'incall

我很确定我已正确设置了所有内容。但由于某些原因,我无法解决问题。

我的代码设置如下。

func fetchNotifications() {
    guard let currentID = Auth.auth().currentUser?.uid else { return }

    USER_SWIPES_REF.child(currentID).observe(.childAdded) { (snapshot) in

        guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
        guard let uid = dictionary["uid"] as? String else { return }


        Database.fetchUser(with: uid, completion: { (user) in
             if let fromId = dictionary["fromId"] as? String {
                  Database.fetchUser(with: fromId, completion: { (post) in

//我收到的错误在下面。

                      let notification = Notifications(user: user, poster: post, dictionary: dictionary)
                      self.notifications.append(notification)
                    //self.handleReloadTable()
                })
            }
        })
    }
}

import Foundation

class Notifications {

enum NotificationType: Int, Printable {

    case swipe
    //case Comment


    var description: String {
        switch self {
        case .swipe: return " swiped on your Job Post"
        //case .Comment: return " commented on your post"

        }
    }

    init(index: Int) {
        switch index {
        case 0: self = .swipe
       // case 1: self = .Comment

        default: self = .swipe
        }
    }
}

var creationDate: Date!
var uid: String!
var postId: String?
var user: User!
var poster: Poster?
var type: Int?
var notificationType: NotificationType!
var didCheck = false

init(user: User?, poster: Poster? = nil, dictionary: Dictionary<String, AnyObject>) {

    self.user = user

    if let poster = poster {
        self.poster = poster
    }

    if let creationDate = dictionary["Swipe Date"] as? Double {
        self.creationDate = Date(timeIntervalSince1970: creationDate)
    }

    if let type = dictionary["type"] as? Int {
        self.notificationType = NotificationType(index: type)
    }

    if let uid = dictionary["uid"] as? String {
        self.uid = uid
    }

    if let postId = dictionary["fromId"] as? String {
        self.postId = postId
    }
}

}

我不确定为什么会出现此错误。它与在同一函数中调用两个单独的结构有关吗?

1 个答案:

答案 0 :(得分:0)

您在说

Notifications(user: user, post: post, dictionary: dictionary)

但这与Notifications类的初始化程序不匹配:

init(user: User?, poster: Poster? = nil, dictionary: Dictionary<String, AnyObject>) {

术语post与术语poster不同。

您的代码可能存在更多错误,但这显然是编译错误的直接原因。

编辑,好的,现在您已经编辑了代码。但是现在您遇到的问题是,当我说“您的代码可能存在更多错误”时,我在暗示这个问题。当你说

Database.fetchUser(with: fromId, completion: { (post) in

您的post是用户,而不是发布者。 (获取用户。)因此,您不能将其作为poster参数使用,因为该参数应该是Poster。