I am a noob in Core Data with Swift. I have one NSManagedObject class that looks like this:
import Foundation import CoreData class Polls: NSManagedObject { @NSManaged var title: String @NSManaged var pollDescription: String }
In a subclass of UITableViewController, I have a method for retrieving these objects, for example:
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.
Now, in cellForRowAtIndexPath: I just do 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
Commented on println with EXC_BAD_ACCESS . No additional error information was provided, and adding βdynamicβ to the attributes of the Polls class does not matter.
Why can't I access the properties of the Polls object? The use of the valueForKey method fails with the message "Unexpectedly found nil value when unpacking optional", even if the title property has a value.
source share