2016-10-18 141 views
0

當我嘗試將評論保存到Firebase時,我的應用崩潰了。此代碼是Xcode的8更新之前可以正常使用:Firebase setValue不適用於iOS 10,Swift 3

func saveNewComment(){ 

    //get date 
    let date = Date() 
    let calendar = Calendar.current 
    let components = (calendar as NSCalendar).components([.day,.month,.year], from: date) 

    let year = components.year 
    let month = components.month 
    let day = components.day 

    //format month 
    let dateFormatter: DateFormatter = DateFormatter() 
    let months = dateFormatter.shortMonthSymbols 
    let monthSymbol = months?[month!-1] 
    let todaysDate = "\(day) \(monthSymbol),\(year)" 

    //trim comment 
    let comment = textView.text 
    let trimmedComment = comment?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 

    //save new comment 
    let commentRef = ref.child("Stores").child(getRef!).child("CommentArray").childByAutoId() 

    commentRef.setValue([ 

     "Comment": trimmedComment, 
     "Date": todaysDate 

    ]) 

    self.delegate?.commentSubmitted(true)//delegate 
    self.dismiss(animated: true, completion: nil)//dismiss view 

} 

顯然錯誤是,當我嘗試使用「的setValue」設置這些鍵值對。任何想法爲什麼會發生?

在此先感謝。

+1

你做了什麼更新?你有什麼錯誤? –

+0

什麼是getRef? – Dravidian

+0

所以我更新了這個問題,我的錯誤,我的意思是Xcode 8更新。另外,getRef只是您記下的對節點的引用。 – Noah

回答

0

Firebase不接受optional類型的值

let trimmedComment = comment?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) 

嘗試解開trimmedCommenttodaysDate值與!

commentRef.setValue([ 
     "Comment": trimmedComment!, 
     "Date": todaysDate! 
]) 
1

確保getRef!是一個合適的節點名稱,然後todaysDate改變

let todaysDate = "\(day!) \(monthSymbol!),\(year!)" 
相關問題