在Swift 3和Xcode8.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)")
}以下是瞬态特性:
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,它就不起作用了。
发布于 2017-08-22 02:23:55
将@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()
}发布于 2017-08-11 12:55:35
我刚把构建设置中的'Swift 3 @objc推断‘切换到'on’,一切都正常了。
https://stackoverflow.com/questions/44804683
复制相似问题