0

Crashlytics says about several crashes happened randomly.

Please consider the following code:

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
}

Here is NewsfeedItem class:

class NewsfeedItem: NSManagedObject {

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

    var dataItem: DataItem?

}

PS: PLEASE NOTE that NewsfeedItem.text property IS NOT Optional, so it can't be nil!

Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
  • Most probably you are accessing nil when accessing text. Trying using optional if syntax, if (let text = newsfeedItem.text) {//Your code} – Abubakr Dar Jan 27 '15 at 12:20
  • self.newsfeedItems..?? – Kumar KL Jan 27 '15 at 12:21
  • How it's possible? `text` is not `Optional` – Andrey Gordeev Jan 27 '15 at 12:26
  • Even for optional Core Data properties, Xcode (unfortunately) generates a non-optional Swift property (so it *can* be nil if the Core Data property has not been set!). See [Check if property is set in Core Data?](http://stackoverflow.com/questions/25661120/check-if-property-is-set-in-core-data) for a possible workaround. – Martin R Jan 27 '15 at 12:31
  • @MartinR, this is much closer to the answer, thanks! Will try that – Andrey Gordeev Jan 27 '15 at 12:33

2 Answers2

1

Are you sure that your NewsfeedItem text is not nil ? If you are not check it first

if let text = newsfeedItem.text as? NSString{
    cell.descriptionLabel.text = text
}
Zell B.
  • 10,266
  • 3
  • 40
  • 49
0

I don't have an idea on swift. But the exception clearly says that its about the accessing a non String value. That mean you'r accessing a nil object or different on not an string object .

Probably self.newsfeedItems is an object of NewsfeedItem, so you'r trying to access a (iVar) string of it.

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • `self.newsfeedItems` is an array of `Newsfeed` items. AFAIK in Swift, if I declare a property like this: `var text: String` it can't take a nil value – Andrey Gordeev Jan 27 '15 at 12:30
  • `newsfeedItem` make this explicitly an object of `NewsfeedItem`, and then try.. Something like `let newsfeedItem as NewsfeedItem` – Kumar KL Jan 27 '15 at 12:35