我得到了这个奇怪的“使用未解析的标识符”警告,即使我在上面的行上声明了var。
这里发生了什么? 这是我的代码:
var fetchedResultsController: NSFetchedResultsController<Project> {
if _fetchedResultsController != nil {
return _fetchedResultsController
}
let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest()
fetchRequest.fetchBatchSize = 20
let sortDescriptor = NSSortDescriptor(key: "name", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
do {
try _fetchedResultsController.performFetch()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//println("Unresolved error \(error), \(error.userInfo)")
abort()
}
return _fetchedResultsController!
}
这是来自Apple模板项目的代码,它没有任何警告或错误:
var fetchedResultsController: NSFetchedResultsController<Event> {
if _fetchedResultsController != nil {
return _fetchedResultsController!
}
let fetchRequest: NSFetchRequest<Event> = Event.fetchRequest()
// Set the batch size to a suitable number.
fetchRequest.fetchBatchSize = 20
// Edit the sort key as appropriate.
let sortDescriptor = NSSortDescriptor(key: "timestamp", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController
do {
try _fetchedResultsController!.performFetch()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
return _fetchedResultsController!
}
我开始认为它可能与我创建的CoreData Entity(Class)有关(Project)。虽然它是一个具有一个属性(名称)的简单实体 - 模板项目也有一个具有一个属性的实体(事件)(timeStamp)。
CoreData的“工具版本”设置为“自动”(XCode 9.0),“代码生成”设置为Swift。
我注意到的另一件事是XCode有时无法识别我的实体(项目),我必须重新启动以解决该问题。
XCode 9是否证明有关CoreData的错误?
答案 0 :(得分:1)
看起来您正在尝试创建NSFetchedResultsController
的单例实例或尝试仅初始化NSFetchedResultsController
一次。
<强>的问题:强>
直接在其getter中访问该属性将导致无限循环,app将崩溃。
<强>解决方案:强>
您所需要的只是lazy variable
lazy var fetchedResultsController : NSFetchedResultsController<Project> = {
let localFetchedResultsController : NSFetchedResultsController< Project > = //initialize your fetched results controller
return localFetchedResultsController
}()
这将确保fetchedResultsController
仅初始化一次并再次返回相同的实例n。除非您通过调用
self.fetchedResultsController = //some_new_fetchedResultsController_instance
希望ut帮助
编辑1:
为什么不尝试
lazy var fetchedResultsController : NSFetchedResultsController<Project> = {
let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest()
fetchRequest.fetchBatchSize = 20
let sortDescriptor = NSSortDescriptor(key: "name", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")
aFetchedResultsController.delegate = self
do {
try aFetchedResultsController.performFetch()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//println("Unresolved error \(error), \(error.userInfo)")
abort()
}
return aFetchedResultsController!
}()