我正在尝试从此项目中的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
}
}
}
答案 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
}
}
要检查是否有imageURL
,postKey
或caption
,只需使用isEmpty
功能。