1
votes

I want to add a method or a custom attribute to the generate subclass Entity.

//---------------- Books+CoreDataClass.swift ----------
import Foundation
import CoreData

@objc(Books)
public class Books: NSManagedObject {

}
//--------------------------end file Books+CoreDataClass.swift —

//----------- Books+CoreDataProperties.swift
//
import Foundation
import CoreData

extension Books {

@nonobjc public class func fetchRequest() -> NSFetchRequest<Event> {
    return NSFetchRequest<Books>(entityName: "Books")
}

@NSManaged public var bookNumber: Int16
@NSManaged public var bookName: String?
@NSManaged public var chapterNumber: Int16
@NSManaged public var chapterName: String?
@NSManaged public var imageNumber: Int16
@NSManaged public var imageName: String?
@NSManaged public var description: String?
@NSManaged public var timestamp: NSDate?
// ---------When I was using Object-c, I used to have the following
//
// - (NSString *)sortingSectionAttribute
// {
// return [NSString stringWithFormat:@“Book: %@ - Chapter: %@”, self.bookNumber, self. chapterNumber ];
// }

}
///-------------------end file Books+CoreDataProperties.swift ------

But I dont know how to do it in Swift. Though I tried many ways to accomplish this but it gives me an error when I run the app and hit the let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: “sortingSectionAttribute”, cacheName: nil)

1
There are some options: 1) You can write the method in a new extension which isn't going to be generated over at a later date when you update the model. 2) You can change the codegen option in the data model to "Manual/None" as a way to tell it not to generate, then you can update the class directly.josh-fuggle
I am using you second option as you suggested. Everything work okay, I can read the return value of it in the cell but only when I have it in sort or sectionNameKeyPath: “sortingSectionAttribute” generate error. The link stackoverflow.com/questions/31317346/… by can’t use it in itsectionNameKeyPathR. Hobus

1 Answers

0
votes

I created the above method in swift and just added objc infront of the method declaration and it work as I want.