将数据保存到Firebase数据库

时间:2019-10-03 00:40:24

标签: ios swift firebase firebase-realtime-database

我正在尝试通过childByAutoID将数据保存到Firebase。由于某些原因,我被打中错误:

  

无法将类型“ Poster.Type”的值转换为预期的参数类型“ Poster”

我不知道这意味着什么。我的代码看起来像

var post: Poster?
// var post:Poster? is above my viewDidLoad function

@objc func handleLike() {
    sendSwipeNotification(post: Poster, swipedRight: true)
} 

  func sendSwipeNotification(post: Poster, swipedRight: Bool) {
    guard let currentUser = Auth.auth().currentUser?.uid else {return}
    guard let cardID = topCardView?.cardViewModel.uid else { return }

    let creationDate = Int(NSDate().timeIntervalSince1970)

    if swipedRight {

        if currentUser != post.fromId {
            let values = ["checked": 0,
                          "creationDate": creationDate,
                          "uid": currentUser,
                          "type": SWIPE_INT_VALUE,
                          "cardID": cardID] as [String : Any]

          let swipeRef = USER_SWIPES_REF.child(post.fromId!).childByAutoId()
              swipeRef.updateChildValues(values, withCompletionBlock: { (err, ref) in
              USER_SWIPES_REF.child(currentUser).child(cardID).setValue(swipeRef.key)



         })

            NOTIFICATIONS_REF.childByAutoId().updateChildValues(values)
        }

    } else {

    }

}

我的海报是一种结构,其中包含UID字符串等变量。感谢所有帮助

1 个答案:

答案 0 :(得分:0)

sendSwipeNotification(post: Poster, swipedRight: true)不起作用。

在调用函数(在这种情况下)时,应该期望它传递一个已填充的对象,而不是对象类或结构本身(在这种情况下为Poster结构)

换句话说,您将实际的Struct模型发送到sendSwipeNotification,而应该从Struct模型创建一个struct对象,将其填充并传递该对象。

例如

@objc func handleLike() {
    let createdByUserId = //get the id of who created the post (?)

    let myPostToPass = PostStruct(fromId: createdByUserId, postText: "hello!")

    //pass the created myPostToPass poster object
    sendSwipeNotification(post: myPostToPass, swipedRight: true) 
}

然后

func sendSwipeNotification(post: PostStruct, swipedRight: Bool) {
    guard let currentUser = Auth.auth().currentUser?.uid else {return}

    let fromId = post.fromId
    let msg = post.postText

    guard let cardID = topCardView?.cardViewModel.uid else { return }