Swift Firebase数据库post错误:无法调用类型为';邮政';具有类型为的参数列表
我正试图将某些内容发布到我的Firebase数据库,但该帖子需要uid,以便我知道该帖子的作者。我经常遇到这样的错误:`无法使用类型为(username:String!、postId:String、postText:String!、postGame:String!、postDate:(NSNumber)、postType:String、uid:String!)的参数列表调用类型为'Post'的初始值设定项',但我不知道如何解决这个问题。有人能帮帮我吗!谢谢Swift Firebase数据库post错误:无法调用类型为';邮政';具有类型为的参数列表,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我正试图将某些内容发布到我的Firebase数据库,但该帖子需要uid,以便我知道该帖子的作者。我经常遇到这样的错误:`无法使用类型为(username:String!、postId:String、postText:String!、postGame:String!、postDate:(NSNumber)、postType:String、uid:String!)的参数列表调用类型为'Post'的初始值设定项',但我不知道如何解决这个问题。有人能帮帮我吗!谢谢 import UIKit i
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]
}
}
问题在于:
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)