Swift:帮助发现导致我的应用程序崩溃的不一致

时间:2016-08-12 02:59:30

标签: swift oop

当我运行我的应用时,我收到了臭名昭着的错误:unexpectedly found nil while unwrapping an Optional value。我相信这是由于我的代码中的不一致,我似乎无法找到......

问题出在4个地方中的任何一个地方。我的子类对象CustomMessage.swift,函数addMessagedidPressSendButtonobserveMessages

这是我的子类对象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(),但即使它运行(注意我在方法中的print语句),但在行之前不会崩溃:

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

这让我相信问题是由于这三个功能内部的某种不一致造成的。我只是无法弄清楚它在哪里或是什么。

感谢您的帮助!

如果您需要任何其他信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

我非常推翻了这个......在之前的测试中,我保存了没有我的新属性的对象,所以当应用程序试图在旧数据上调用新属性时,Xcode会回复此错误消息。对我这么愚蠢。