首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSFetchedResultsController不处理sectionNameKeyPath的瞬态特性

NSFetchedResultsController不处理sectionNameKeyPath的瞬态特性
EN

Stack Overflow用户
提问于 2017-06-28 14:00:06
回答 2查看 695关注 0票数 3

在Swift 3和Xcode8.3中很好地工作

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

它根据时间对信息进行排序,并按日对它们进行分段。

下面是操作步骤:

代码语言:javascript
复制
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)")
}

以下是瞬态特性:

代码语言:javascript
复制
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()
}

将其用作:

代码语言:javascript
复制
func tableView(tableView: UITableView, numberOfRowsInSection section: 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,它就不起作用了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-22 02:23:55

将@objc添加到瞬态属性中,因此:

代码语言:javascript
复制
@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()
}
票数 4
EN

Stack Overflow用户

发布于 2017-08-11 12:55:35

我刚把构建设置中的'Swift 3 @objc推断‘切换到'on’,一切都正常了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44804683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档