线程1:快速3中的EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0 * 0)

时间:2017-02-13 18:08:10

标签: swift3

我正在尝试从此项目中的firebase解析UI中的数据。但是我的应用程序将运行时出现问题。它给出了错误"线程1:EXC_BAD_INSTRUCTION(代码= EXC_1386_INVOP,子代码= 0 * 0)"当下一个视图控制器试图显示时。 Plzz帮助我解决这个问题。代码如下:

import Foundation

class Post {
private var _caption: String!
 private var _imageUrl: String!
 private var _likes: Int!
 private var _postKey: String!

var caption: String {
    return _caption
}

var imageUrl: String {
    return _imageUrl
}

var likes: Int {
    return _likes     //Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0*0)
}

var postKey: String {
    return _postKey
}

init(caption: String, imageUrl: String, likes: Int) {
    self._caption = caption
    self._imageUrl = caption
    self._likes = likes
}

init(postKey: String, postData: Dictionary<String, AnyObject>) {

    self._postKey = postKey

    if let caption = postData["caption"] as? String {
        self._caption = caption
    }
    if let imageUrl = postData["imageUrl"] as? String {
        self._imageUrl = imageUrl
    }
    if let likes = postData["likes"] as? Int {
        self._likes = likes
    }

}

}

1 个答案:

答案 0 :(得分:1)

除了我的评论之外,如果其中一个属性为nil并且使用了非可选的getter,则会发生崩溃。

Post类与问题中的相同,但永远不会崩溃

class Post {

    let caption: String
    let imageUrl: String
    let likes: Int
    let postKey : String

    init(caption: String, imageUrl: String, likes: Int) {
        self.caption = caption
        self.imageUrl = caption
        self.likes = likes
        self.postKey = ""
    }

    init(postKey: String, postData: Dictionary<String, Any>) {
        self.postKey = postKey
        self.caption = postData["caption"] as? String ?? ""
        self.imageUrl = postData["imageUrl"] as? String ?? ""
        self.likes = postData["likes"] as? Int ?? 0
    }
}

要检查是否有imageURLpostKeycaption,只需使用isEmpty功能。