我正在尝试使用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]()
它构建成功,但是我直接收到错误消息:
致命错误:解开Optional值时意外发现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
我如何检查展开的位置?我怎样才能解决这个问题?
[编辑]
通过一些反复试验(即将淘汰的部分),我认为这与代码的这一部分有关
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
最佳答案
问题出在这段代码上:
// 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 {
...
}
}
关于ios - 尝试转换为NSFetchedResultsController“严重错误:在展开可选值时意外发现nil”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33279511/