试图转换为NSFetchedResultsController"致命错误:在展开可选值时意外发现nil"?

时间:2015-10-22 11:05:12

标签: ios swift nsfetchedresultscontroller

我正在尝试将工作表视图转换为使用NSFetchedResultsController的表视图。

我只使用两个条目进行测试,这是我工作代码的一部分

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
let fetchRequest = NSFetchRequest(entityName: "Appointments")
        //3
        do {
            self.tableView.reloadData()
            let results = try context.executeFetchRequest(fetchRequest)
            invoices = results as Array<AnyObject>  //cast to Appointments didn't work           
        } catch let error as NSError {
            print("Could not fetch \(error), \(error.userInfo)")
        }

这是我转换代码的一部分

    let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

var frc: NSFetchedResultsController = NSFetchedResultsController()

func getFetchedResultsController()->NSFetchedResultsController {
    frc = NSFetchedResultsController(fetchRequest: listFetchRequest(), managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
    return frc
}

func listFetchRequest()->NSFetchRequest{
    let fetchRequest = NSFetchRequest(entityName: "Appointments")
    let sortDescriptor = NSSortDescriptor(key: "appointmentUser", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest
}

var appointments = [AnyObject]()//[NSManagedObject]()

它构建成功,但我直接收到错误消息: 致命错误:在解包可选值时意外发现nil

libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ():
0x1002c0a50 <+0>:   stp    x29, x30, [sp, #-16]!
0x1002c0a54 <+4>:   mov    x29, sp
0x1002c0a58 <+8>:   sub    sp, sp, #16
0x1002c0a5c <+12>:  and    w8, w2, #0x1
0x1002c0a60 <+16>:  tbnz   w8, #0, 0x1002c0a80       ; <+48>
0x1002c0a64 <+20>:  tbnz   x1, #63, 0x1002c0ac4      ; <+116>
0x1002c0a68 <+24>:  add    x1, x0, x1
0x1002c0a6c <+28>:  mov    x2, x3
0x1002c0a70 <+32>:  mov    x3, x4
    0x1002c0a74 <+36>:  mov    x4, x5
    0x1002c0a78 <+40>:  bl     0x10030d910               ; function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
->  0x1002c0a7c <+44>:  brk    #0x1
    0x1002c0a80 <+48>:  str    xzr, [sp, #8]
    0x1002c0a84 <+52>:  cmp    x0, w0, uxtw

如何检查展开的位置?我该如何解决这个问题?

[编辑] 通过一些试验和错误(outcommenting parts),我认为它与我的代码的这部分有关

        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
//        return 1
        let numberOfSections = frc.sections?.count
        return numberOfSections!
    }

另见:NSFetchedResultsController swift sections

1 个答案:

答案 0 :(得分:1)

问题在于这段代码:

// issue 1
var frc: NSFetchedResultsController = NSFetchedResultsController()

// issue 2
func getFetchedResultsController()->NSFetchedResultsController {
    frc = NSFetchedResultsController(fetchRequest: listFetchRequest(), managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
    return frc
}

问题1:NSFetchedResultsController正在实例化(但未配置)并存储在frc中。

问题2:该函数每次调用时都会创建一个新的,已配置的NSFetchedResultsController(并将其存储在frc中)。

我认为你在这里尝试实现的是懒惰的实例化,删除getFetchedResultsController函数并用这个替换变量声明:

lazy var frc: NSFetchedResultsController = {
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: self.listFetchRequest(), managedObjectContext: self.context, sectionNameKeyPath: nil, cacheName: nil)
    fetchedResultsController.delegate = self
    return fetchedResultsController
}()

这将在首次访问frc属性时设置提取的结果控制器,并将委托指定为self

根据您的评论,viewDidLoad方法将如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    do {
        try self.frc.performFetch()
    } catch {
        ...
    }
}