2017-10-28 223 views
-1

大家下午好,我正在嘗試上傳一個圖片到Firebase並顯示在一個單元格中..我只是關於除了圖片以外的所有內容(我無法擺脫錯誤) 。Swift 4上傳圖片到Firebase

Here is the error I am getting

不能調用初始化類型 '帖子' 類型的參數列表 '(postImageStringUrl:字符串,內容:字符串!帖子ID:字符串)'

//[Save Image] 
    // Create data in the server 
    let data = UIImageJPEGRepresentation(self.addedImage.image!, 0.5) 
    let metadata = StorageMetadata() 
    metadata.contentType = "image/jpeg" 

    let postId = "\(Auth.auth().currentUser!.uid)\(NSUUID().uuidString)" 
    // Create a reference to the file you want to upload 
    let imagePath = "postImages\(postId)/postPic.jpg" 

    storageRef.child(imagePath).putData(data!, metadata: metadata) { (metadata, error) in 
     if error == nil { 
      let postRef = self.databaseRef.child("posts").childByAutoId() 
      let post = Posts(postImageStringUrl: String (describing: metadata!.downloadURL()), content: descriptionTextView.text, postId: postId) 
      postRef.setValue(post.toAnyObject()) 
     }else{ 
      print(error.debugDescription) 
     } 
    } 
    //[Save Image] 

這裏是我的postViewController

import Foundation 
import Firebase 
import FirebaseDatabase 
import FirebaseStorage

struct Posts {

var postImageStringUrl: String! var department: String! var content: String! var username: String! var postId: String! var ref: DatabaseReference? var key: String! init(postImageStringUrl: String, department: String, content: String, username: String,postId: String, key: String = ""){ self.postImageStringUrl = postImageStringUrl self.department = department self.content = content self.username = username self.postId = postId self.key = key self.ref = Database.database().reference() } init(snapshot: DataSnapshot){ let snapshotValue = snapshot.value as! NSMutableDictionary self.postImageStringUrl = snapshotValue["postImageStringUrl"] as! String self.department = snapshotValue["department"] as! String self.content = snapshotValue["content"] as! String self.username = snapshotValue["username"] as! String self.postId = snapshotValue["postId"] as! String self.key = snapshot.key self.ref = snapshot.ref } func toAnyObject() -> [String: AnyObject] { return ["postImageStringUrl": postImageStringUrl as AnyObject, "department": department as AnyObject,"content": content as AnyObject,"username": username as AnyObject, "postId": postId as AnyObject] }

}

任何幫助將不勝感激...泰

回答

0

你的帖子初始化是

init(
    postImageStringUrl: String, 
    department: String, 
    content: String, 
    username: String, 
    postId: String, 
    key: String = "") 

,它似乎您試圖與

let post = Posts(
      postImageStringUrl: String (describing: metadata!.downloadURL()), 
      content: descriptionTextView.text, 
      postId: postId) 

基本上只是將其初始化在初始化中缺少一些參數;部門,用戶名

+0

你這麼對!謝謝...我試圖不使用部門,內容&&用戶名我沒有意識到它必須在那裏 –

+0

@RubenFranco優秀!很高興我能幫上忙。一定要接受答案,以便它可以幫助他人! – Jay

+0

只是再次感謝.. –