當我運行我的應用程序時,我得到了臭名昭着的錯誤:unexpectedly found nil while unwrapping an Optional value
。我相信這是由於我的代碼不一致,我似乎無法找到...Swift:幫助發現導致我的應用程序崩潰的不一致性
問題出現在4個地方中的任何一個。我的子類對象CustomMessage.swift
,功能addMessage
,didPressSendButton
或observeMessages
這裏是我的子類的對象CustomMessage
:
class CustomMessage: JSQMessage {
var score : Int
init(senderId: String, displayName: String, text: String, date: NSDate, score: Int) {
self.score = score
// super.init(senderId:senderId, displayName:displayName, text:text) // Must call a designated initializer of the superclass 'JSQMessage'
//super.init(senderId: senderId, displayName: displayName, text: text)
super.init(senderId: senderId, senderDisplayName: displayName, date: date, text: text)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
這裏是我已經提到的其他三個功能。請注意,所有這些一起工作。
func addMessage(id: String, text: String, displayName: String, date: NSDate, score: Int) {
// Make sure the character count is between 10 and 140, then add message to message list to display
if (text.characters.count <= 10 || text.characters.count >= 140) {
}
else {
// let message = JSQMessage(senderId: id, displayName: displayName, text: text)
let message = CustomMessage(senderId: id, displayName: displayName, text: text, date: date, score: score)
messages.append(message)
}
}
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!,
senderDisplayName: String!, date: NSDate!) {
// Make sure the character count is between 10 and 140, then save data to Firebase
if (text.characters.count <= 10 || text.characters.count >= 140) {
print("Your text must be between 10 and 140 characters")
}
else {
let itemRef = messageRef.childByAutoId() // 1
let messageItem = [ // 2
"text": text,
"senderId": senderId,
"location": getLocation(),
"date": String(date),
"score": getScore()
]
itemRef.setValue(messageItem) // 3
// 4
JSQSystemSoundPlayer.jsq_playMessageSentSound()
// 5
finishSendingMessage()
Answers.logCustomEventWithName("Message sent", customAttributes: nil)
}
}
private func observeMessages() {
print("gets here")
// 1
let messagesQuery = messageRef.queryLimitedToLast(25)
// 2
messagesQuery.observeEventType(.ChildAdded) { (snapshot: FIRDataSnapshot!) in
// 3
print("gets inside")
let id = snapshot.value!["senderId"] as! String
let text = snapshot.value!["text"] as! String
let locationId = snapshot.value!["location"] as! String
let textScore = snapshot.value!["score"] as! NSInteger
let date = NSDate()
// 4
self.addMessage(id, text: text, displayName: locationId, date: date, score: textScore)
print("finished adding messages")
// 5
self.finishReceivingMessage()
Answers.logCustomEventWithName("Visited RoastChat", customAttributes: nil)
}
}
我也試圖做一些調試和代碼不會崩潰,直到observeMessages()被調用,但即使如此,它運行(注意方法中我打印語句),但不會崩潰,直至線:
self.addMessage(id, text: text, displayName: locationId, date: date, score: textScore)
這使我相信,這個問題是由於不一致的地方這三個函數裏面。我無法弄清楚它在哪裏或是什麼。
感謝您的幫助!
讓我知道你是否需要任何額外的信息。
重寫你的代碼沒有!s,問題會變得更加明顯。如果讓,守衛讓和使用可選的鏈接來代替。 –
這並不能解決我的問題:( –
我不確定你在乎的是@AaronBrager正確指出的,你有很多不安全的解包裝在你的self.addMessage方法之上 – Shripada