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)
尝试用 !
解包 trimmedComment
和 todaysDate
值
commentRef.setValue([
"Comment": trimmedComment!,
"Date": todaysDate!
])
当我尝试将评论保存到 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)
尝试用 !
trimmedComment
和 todaysDate
值
commentRef.setValue([
"Comment": trimmedComment!,
"Date": todaysDate!
])