gpt4 book ai didi

ios - NSFetchedResultsController 不使用 sectionNameKeyPath 的 transient 属性

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:25:26 26 4
gpt4 key购买 nike

使用 Xcode8.3 在 Swift 3 中工作良好

我正在进行一个项目,其中包含用于保存消息的核心数据。
它根据时间对消息进行排序,并根据日期对消息进行分段。

方法如下:

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)")
}

这里是 transient 属性:

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()
}

将其用作:

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,它就无法正常工作。

最佳答案

将@objc 添加到 transient 属性,因此:

@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()
}

关于ios - NSFetchedResultsController 不使用 sectionNameKeyPath 的 transient 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44804683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com