Firebase数据库发布错误:无法为类型'发布'使用类型的参数列表

时间:2017-10-03 19:33:35

标签: swift firebase firebase-realtime-database

我正在尝试将内容发布到我的Firebase数据库,但帖子需要uid所以我知道帖子是谁。我一直收到这个错误:`无法调用类型' Post'使用类型'的参数列表(用户名:String!,postId:String,postText:String!,postGame:String!,postDate:(NSNumber),postType:String,uid:String!)'但我不知道如何解决这个问题。有人可以帮帮我吗!谢谢!

 import UIKit
    import FirebaseAuth
    import FirebaseStorage
    import FirebaseDatabase

    class AddPostViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {


        @IBOutlet weak var textView: UITextView!
        @IBOutlet weak var GameText: UITextField!

        var currentUser: User2!

        var dataBaseRef: DatabaseReference! {
            return Database.database().reference()
        }

        var storageRef: Storage {

            return Storage.storage()
        }

        func loadUserInfo(){

            let userRef = dataBaseRef.child("users/\(Auth.auth().currentUser!.uid)")
            userRef.observe(.value, with: { (snapshot) in

                self.currentUser = User2(snapshot: snapshot)
                        }) { (error) in
                print(error.localizedDescription)
            }

        }

        @objc func savePost(){

            var text: String!
            var Game: String!

            if let postText = textView.text {
                text = postText
            }

            if let postGame = GameText.text {
                Game = postGame
            }


            let newPost = Post(username: self.currentUser.username, postId: NSUUID().uuidString, postText: text, postGame: Game, postDate: (NSDate().timeIntervalSince1970 as NSNumber), postType: "TEXT", uid: self.currentUser.uid)
                let postRef = self.dataBaseRef.child("posts").childByAutoId()
                postRef.setValue(newPost.toAnyObject(), withCompletionBlock: { (error, ref) in
                    if error == nil {
                        self.navigationController!.popToRootViewController(animated: true)
                    }else {
                        print(error!.localizedDescription)

                    }
                })

            }

        }

这是我的帖子模型:

import Foundation
import UIKit
import FirebaseDatabase
import FirebaseAuth
import Firebase

struct Post {

    var username: String!
    var Game: String!
    var Console: String!
    var ExtraInfo: String!
    var uid: String!
    var postId: String!
    var postType: String!
    var postText: String!
    var postDate: NSNumber!
    var ref: DatabaseReference!
    var key: String?

    init(snapshot: DataSnapshot){

        self.ref = snapshot.ref
        self.key = snapshot.key
        self.username = (snapshot.value! as! NSDictionary)["username"] as! String
        self.Console = (snapshot.value! as! NSDictionary)["Console"] as! String
        self.ExtraInfo = (snapshot.value! as! NSDictionary)["ExtraInfo"] as! String
        self.Game = (snapshot.value! as! NSDictionary)["Game"] as! String
        self.postId = (snapshot.value! as! NSDictionary)["postId"] as! String
        self.postType = (snapshot.value! as! NSDictionary)["postType"] as! String
        self.postDate = (snapshot.value! as! NSDictionary)["postDate"] as! NSNumber
        self.postText = (snapshot.value! as! NSDictionary)["postText"] as! String
        self.uid = (snapshot.value! as! NSDictionary)["uid"] as! String

    }

    init(username: String, postId: String, Game: String, Console: String, ExtraInfo: String, postText: String, postDate: NSNumber, postType: String, uid: String){

        self.username = username
        self.Game = Game
        self.Console = Console
        self.ExtraInfo = ExtraInfo
        self.postText = postText
        self.postType = postType
        self.uid = uid
        self.postDate = postDate
        self.postId = postId

    }

    func toAnyObject() -> [String: Any] {
        return ["username": username, "postId":postId,"Game": Game , "Console": Console,"ExtraInfo": ExtraInfo ,"postType":postType, "postDate":postDate, "postText":postText,"uid": uid]
    }

}

1 个答案:

答案 0 :(得分:1)

问题在于:

let newPost = Post(username: self.currentUser.username, postId: NSUUID().uuidString, 
postText: text, postGame: Game, postDate: (NSDate().timeIntervalSince1970 as NSNumber), 
postType: "TEXT", uid: self.currentUser.uid)

您正在尝试初始化Post对象,但根据您创建的init,它需要包含以下组件:

init(username: String, postId: String, Game: String, Console: String, 
ExtraInfo: String, postText: String, postDate: NSNumber, postType: String, 
uid: String)

相反,对于newPost,它应该具有正确的初始化程序,如:

let newPost = Post(username: self.currentUser.username, postId: NSUUID().uuidString, 
Game: Game, Console: /*I don't know what goes here */, ExtraInfo: String, 
postText: text, postDate: (NSDate().timeIntervalSince1970 as NSNumber), 
postType: "TEXT", uid: self.currentUser.uid)