1
Firebaseデータベースに何かを投稿しようとしていますが、ポストにはuidが必要です。 `ユーザ名:String!、postId:String、postText:String!、postGame:String!、postDate:(NSNumber)、postType: 'の型の引数リストを持つPost型の型を呼び出すことはできません。 String、uid:String!) 'しかし、私はこれをどのように修正できるのか分かりません。誰か助けてください!ありがとう!ここでFirebaseデータベースのポスト・エラー:タイプが「ポスト」の初期化子を呼び出すことができません
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]
}
}