ios - 访问 String 属性时的 EXC_BAD_ACCESS

标签 ios swift exc-bad-access

Crashlytics 说有几次崩溃是随机发生的。

请考虑以下代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("NewsfeedItemCell", forIndexPath: indexPath) as NewsfeedItemTableViewCell

    let newsfeedItem = self.newsfeedItems[indexPath.row]

    // This line gives crash: EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x0000000000000000
    let text = newsfeedItem.text as NSString
    cell.descriptionLabel.text = text

    return cell
}

这是 NewsfeedItem 类:

class NewsfeedItem: NSManagedObject {

    @NSManaged var date: NSDate
    @NSManaged var sku: String
    @NSManaged var text: String

    var dataItem: DataItem?

}

PS:请注意 NewsfeedItem.text 属性不是可选的,因此它不能是 nil!

最佳答案

您确定您的 NewsfeedItem 文本不是 nil 吗?如果你不是先检查一下

if let text = newsfeedItem.text as? NSString{
    cell.descriptionLabel.text = text
}

关于ios - 访问 String 属性时的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28170294/

相关文章:

ios - 带有 UIWindowLevelStatusBar + 1 的窗口在 iOS 8 上隐藏状态栏

ios - 将应用提交到使用iCloud的App Store,无效的权利

ios - becomeFirstResponder 返回 true 并且键盘被关闭

objective-c - ios5 - 单击按钮时出现 "EXC_BAD_ACCESS"

c++ - 链表 - EXC_BAD_ACCESS。我认为该错误是因为我错误地删除了一个节点,但我将不胜感激任何建议

iphone - 应用程序窗口在应用程序启动结束时应该有一个 Root View Controller

ios - SecItemCopyMatching 返回 -25300

ios - 快速使用 requestWhenInUseAuthorization

swift - 二进制搜索 : error in passing array

iOS:lldb EXC_BAD_ACCESS 自定义单元格