NSFetchedResultsController not working with transient properties

Works fine in Swift 3 with Xcode8.3

I have a project that contains basic data for persisting messages.
It sorts messages according to the time and separates them according to the 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 the property of the 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()
}

      

Using it like:

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 the sectionTitle

property will never be called.

This setting was / works correctly with Swift3 in Xcode8.3.
Even this works with Swift3.2 in Xcode9 beta.
But if I switch to Swift4 in Xcode9 beta, it doesn't work.

+3


source to share


2 answers


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

      

+3


source


I just switched the "Swift 3 @objc inference" in build settings to 'on' and everything works fine again.



+1


source







All Articles