无法访问NSManagedObject子类的属性(提供EXC_BAD_ACCESS)

时间:2015-08-20 09:37:43

标签: ios swift core-data

我是使用Swift的Core Data中的菜鸟。我有一个NSManagedObject类,如下所示:

import Foundation
import CoreData

class Polls: NSManagedObject {

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

}

在UITableViewController子类中,我有一个获取这些对象的方法,如下所示:

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)
}

请注意,self.polls被定义为"民意调查"对象。

现在,在cellForRowAtIndexPath:我只是这样做:

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

评论printlnEXC_BAD_ACCESS失败。没有给出额外的错误信息,并添加" dynamic"到Polls类的属性没有区别。

为什么我无法访问Polls对象的属性?使用valueForKey方法失败并显示消息&#34;在展开可选&#34; 时,意外地发现了nil值,即使title属性具有值。< / p>

1 个答案:

答案 0 :(得分:3)

我假设您的excludeFiles数组是Polls数组。 要检查这一点,请尝试替换:

NSManagedObject

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

如果这有帮助,您可以查看此答案

CoreData: warning: Unable to load class named