Swift中用于NSFetchedResultsController的匿名类(闭包?)>>

问题描述 投票:0回答:1

某些情况:

我离开iOS编程已有5年多了,男孩好手改变了一切。为了使它更加令人兴奋,我同时尝试了Swift。

我正在尝试使用Master-Detail App模板构建相对简单的iOS应用,并且正要向数据中添加部分。此时,我的(核心数据)数据模型非常简单-一些Location(这些部分),每个都包含一些Container。它们以明显的方式链接(LocationContainer的一对一引用,Container对它的Location的一对一引用)。

[我怀疑我要去一个想要fetchedResultsController.object(at: indexPath)返回与Container相对应的indexPath的地方,但我也想让fetchedResultsController.sections[section]返回一个Location

代码

这几乎是来自应用程序模板的代码(稍作修改即可将我的Location类型用作ResultType的通用NSFetchedResultsController-可能是错误的;也许应该是[C0 ]甚至Container-我们将在一分钟内解决这个问题。

NSManagedObject

我的问题

我是Swift的新手,我正在努力了解Xcode模板为我创建的代码。所以我有几个问题:

  1. 我们怎么称这种模式?匿名类(显然不是)?关闭?知道它的名字将有助于我更好地搜索相关答案,而不是将时间浪费在这些菜鸟问题上。

  2. 它实际上是什么定义?我怀疑它正在为var fetchedResultsController: NSFetchedResultsController<Location> { if _fetchedResultsController != nil { return _fetchedResultsController! } let fetchRequest: NSFetchRequest<Location> = Location.fetchRequest() // Set the batch size to a suitable number. fetchRequest.fetchBatchSize = 20 // Edit the sort key as appropriate. let sortDescriptor = NSSortDescriptor(key: "name", 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! } 定义实现,并返回具有所有默认实现的init实例。如果那还不完全正确,请帮助我更好地理解它(指向要阅读的内容也有帮助)。

  3. 使用此模式时应如何覆盖NSFetchedResultsController的覆盖方法?还是我需要创建real

  4. 子类的东西。
  5. 我应将哪种类型用作通用NSFetchedResultsController,为什么?这有点不合时宜,但是到底,也许您会怜悯我,让我朝着旅程的另一步迈进。

[某些情况:我离开iOS编程已有5年多了,男孩好手的情况有所改变。为了使它更加令人兴奋,我同时尝试了Swift。我试图建立一个相对...

ios swift nsfetchedresultscontroller
1个回答
0
投票
  1. 这是一个计算属性
© www.soinside.com 2019 - 2024. All rights reserved.