gpt4 book ai didi

swift - Firebase 数据库发布错误 : Cannot invoke initializer for type 'Post' with an argument list of type

转载 作者:行者123 更新时间:2023-11-28 06:11:10 25 4
gpt4 key购买 nike

我正在尝试将某些内容发布到我的 Firebase 数据库,但该帖子需要 uid,因此我可以从谁那里知道该帖子。我不断收到此错误:`无法使用类型为'(用户名: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)

}
})

}

}

这是我的 Post 模型:

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)

关于swift - Firebase 数据库发布错误 : Cannot invoke initializer for type 'Post' with an argument list of type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46552069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com