簡體   English   中英

Firebase setValue在IOS 10,Swift 3中不起作用

[英]Firebase setValue not working in IOS 10, Swift 3

嘗試將評論保存到Firebase時,我的應用程序崩潰了。 這段代碼在Xcode 8更新之前運行良好:

    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”來設置那些鍵值對時。 任何想法為什么會這樣?

提前致謝。

確保getRef! 是正確的節點名稱,然后將todaysDate更改為

let todaysDate = "\(day!) \(monthSymbol!),\(year!)"

Firebase不接受optional類型作為值

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

嘗試使用來包裝trimmedCommenttodaysDate!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM