NSFetchedResultsController不适用于sectionNameKeyPath的瞬态属性 [英] NSFetchedResultsController not working with transient property for sectionNameKeyPath

查看:65
本文介绍了NSFetchedResultsController不适用于sectionNameKeyPath的瞬态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift 3中使用Xcode8.3可以正常工作


我正在进行一个项目,该项目具有用于保存消息的核心数据。

它根据时间对消息进行排序,并根据天对消息进行分段。 = NSFetchRequest(实体名称:消息)
let sortDiscriptor = NSSortDescriptor(键:时间,升序:正确)
request.sortDescriptors = [sortDiscriptor]

fetchedResultsController = NSFetchedResultsController(fetchRequest:request,managedObjectContext:mainThreadMOC,sectionNameKeyPath: sectionTitle,cacheName:nil)
fetchedResultsController.delegate =自我
做{
尝试fetchedResultsController.performFetch()
} catch {
fatalError(未能初始化FetchedResultsController:\(错误))
}

这是临时属性:

  var sectionTitle:字符串? {
//这是** transient **属性
//将其设置为瞬态,请在数据模型
返回时间中选中具有相同名称的框!.getTimeStrWithDayPrecision()
}

将其用作:

  func tableView(tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {
let sectionInfo = fetchedResultsController.sections![section]
let n = sectionInfo.numberOfObjects
return n
}

它始终提供0个部分,而 sectionTitle 属性永远不会被调用。


此设置在Xcode8.3中与Swift3一起正常工作。

即使在Xcode9-beta中与Swift3.2一起工作。
但是,如果我在Xcode9-beta中切换到Swift4,它不起作用。

解决方案

在临时属性中添加@objc,因此:

  @objc var section标题:字符串? {
//这是** transient **属性
//将其设置为瞬态,请在数据模型
返回时间中选中具有相同名称的框!.getTimeStrWithDayPrecision()
}


Working fine in Swift 3 with Xcode8.3

I have a project ongoing which has core data for saving messages.
It sorts messages according to time and sections them according to day.

Here's how:

let request = NSFetchRequest(entityName: "Message")
let sortDiscriptor = NSSortDescriptor(key: "time", ascending: true)
request.sortDescriptors = [sortDiscriptor]

fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: mainThreadMOC, sectionNameKeyPath: "sectionTitle", cacheName: nil)
fetchedResultsController.delegate = self
do {
    try fetchedResultsController.performFetch()
} catch {
    fatalError("Failed to initialize FetchedResultsController: \(error)")
}

Here is transient property:

var sectionTitle: String? {
    //this is **transient** property
    //to set it as transient, check mark the box with same name in data model
    return time!.getTimeStrWithDayPrecision()
}

Using it as:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  let sectionInfo = fetchedResultsController.sections![section]
  let n =  sectionInfo.numberOfObjects
  return n
}

It always gives 0 sections and sectionTitle property never getting called.

This setup was/is working correctly with Swift3 in Xcode8.3.
Even this is working with Swift3.2 in Xcode9-beta.
But if I switch to Swift4 in Xcode9-beta, it's not working.

解决方案

Add @objc to the transient property, so:

@objc var sectionTitle: String? {
    //this is **transient** property
    //to set it as transient, check mark the box with same name in data model
    return time!.getTimeStrWithDayPrecision()
}

这篇关于NSFetchedResultsController不适用于sectionNameKeyPath的瞬态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆