5

I am a noob in Core Data with Swift. I have a single NSManagedObject class which looks like this:

import Foundation
import CoreData

class Polls: NSManagedObject {

@NSManaged var title: String
@NSManaged var pollDescription: String

}

In a UITableViewController subclass, I have a method for fetching these objects, like this:

func refreshPolls()
{
    //do the query to populate the array
    let req = NSFetchRequest(entityName: "Polls")
    self.polls = self.managedObjectContext?.executeFetchRequest(req, error: nil) as! [Polls]
    println(polls.count)
}

Note that self.polls is defined as an array of "Polls" objects.

Now, in the cellForRowAtIndexPath: I am simply doing this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell
    if indexPath.row==0
    {
        cell = tableView.dequeueReusableCellWithIdentifier("EditPollCell") as! UITableViewCell
    }
    else
    {

        let poll:Polls = self.polls[indexPath.row-1] as Polls
        //println(poll.title) //fails with EXC_BAD_ACCESS

        cell  = tableView.dequeueReusableCellWithIdentifier("PollCell") as! UITableViewCell
        //cell.textLabel?.text = (poll.valueForKey("title") as! String)


    }

    return cell

That commented println fails with EXC_BAD_ACCESS. No additional error info is given, and adding "dynamic" to the Polls class's attributes makes no difference.

Why can't I access the Polls object's properties? Using the valueForKey method fails with the message "Unexpectedly found a nil value when unwrapping an optional", even though the title property has a value.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
tutiplain
  • 1,427
  • 4
  • 19
  • 37

1 Answers1

3

I assume, that your Polls array is NSManagedObject array. To check this, try to replace:

//println(poll.title) //fails with EXC_BAD_ACCESS

with

println(poll.valueForKey("title"))

If this helps, you can check this answer

CoreData: warning: Unable to load class named

Community
  • 1
  • 1
Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66
  • I renamed my Entity as a singular (Poll instead of Polls), as suggested in the first answer of the question you linked, and the problem disappeared. I have no idea why. Can you perhaps offer some insight into this? I do not, and did not have, the @objc() annotation in my class. Why does changing the name fix the problem? – tutiplain Aug 22 '15 at 16:05
  • @tutiplain Did you also change class name from `Current Product Module.Polls` to simple `Poll`? – Vitalii Gozhenko Aug 22 '15 at 20:30
  • I am now having the same problem on another view controller. The class name is PollMaster.Poll. PollMaster is the app name. This has not changed. The second view controller I am having problems with now says EXC_BAD_INSTRUCTION, accessing the same property. – tutiplain Aug 22 '15 at 20:44
  • Hum, can you share code how you get `Poll` objects in second view controller? – Vitalii Gozhenko Aug 23 '15 at 14:56
  • Hi, after carefully debugging my code I found that the issue with the second view controller had to do with me sending a nil value, and not related to the fact that a Core Data object was being sent. I was finally able to send to another view controller like this: ` var PollDet:PollDetailsViewController=self.storyboard?.instantiateViewControllerWithIdentifier("PollDetailsViewController") as! PollDetailsViewController PollDet.setPoll(self.polls[indexPath.row-1]) self.navigationController?.pushViewController(PollDet, animated: true)` – tutiplain Aug 24 '15 at 02:37