我是使用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
评论println
以EXC_BAD_ACCESS
失败。没有给出额外的错误信息,并添加" dynamic"到Polls
类的属性没有区别。
为什么我无法访问Polls
对象的属性?使用valueForKey
方法失败并显示消息"在展开可选" 时,意外地发现了nil值,即使title
属性具有值。< / p>
答案 0 :(得分:3)
我假设您的excludeFiles
数组是Polls
数组。
要检查这一点,请尝试替换:
NSManagedObject
与
//println(poll.title) //fails with EXC_BAD_ACCESS
如果这有帮助,您可以查看此答案