-1

When I run my app I am getting the infamous error: unexpectedly found nil while unwrapping an Optional value. I believe this is due to inconsistencies in my code that I cannot seem to find...

The problem is in either one of 4 places. My subclassed object CustomMessage.swift, the function addMessage, didPressSendButton, or observeMessages

Here is my subclassed object 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")
    }
}

And here are the other three functions I had mentioned. Note that all of these work together.

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)

        }
    }

Also I have tried to do some debugging and the code doesn't crash until observeMessages() is called but even then it runs (Note my print statements within the method) but doesn't crash until the line:

self.addMessage(id, text: text, displayName: locationId, date: date, score: textScore)

Which leads me to believe that the problem is due to an inconsistency somewhere inside these three functions. I just can't figure out where or what it is.

Thanks for any help!

Let me know if you need any additional information.

Ahad Sheriff
  • 1,829
  • 8
  • 24
  • 46

1 Answers1

0

I overthought this so much... During previous testing, I had saved objects without my new properties, so when the app tried to call the new properties on my old data, Xcode responded with this error message. So dumb on my part.

Ahad Sheriff
  • 1,829
  • 8
  • 24
  • 46