Ios NSFetchedResultsController无法使用sectionNameKeyPath的瞬态属性
使用Xcode8.3在Swift 3中工作正常 我有一个正在进行的项目,其中包含用于保存消息的核心数据。Ios NSFetchedResultsController无法使用sectionNameKeyPath的瞬态属性,ios,core-data,nsfetchedresultscontroller,swift4,Ios,Core Data,Nsfetchedresultscontroller,Swift4,使用Xcode8.3在Swift 3中工作正常 我有一个正在进行的项目,其中包含用于保存消息的核心数据。 它根据时间对消息进行排序,并根据日期对消息进行分区 以下是方法: let request = NSFetchRequest(entityName: "Message") let sortDiscriptor = NSSortDescriptor(key: "time", ascending: true) request.sortDescriptors
它根据时间对消息进行排序,并根据日期对消息进行分区 以下是方法:
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)")
}
以下是瞬态特性:
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测试版中,Swift3.2也是如此。
但是如果我在Xcode9测试版中切换到Swift4,它就不起作用了。我刚刚将构建设置中的“Swift 3@objc推理”切换到“开”,所有操作都可以正常运行。将@objc添加到瞬态属性中,因此:
@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()
}
向Apple报告一个bug。不要使用瞬态作为sectionNameKeyPath,因为所有对象都会出现故障,只是为了找出它们所在的分区。而是使用持久化属性并实现节的tableView标题。