Post

Replies

Boosts

Views

Activity

Core Data setting issue
After 7-8 months of intense studying, I'm working on my first App. After a couple of months coding it, I saw that Swift Data wasn't cutting it, I decided to restart the project and implement Core Data. To cut to the chase, I'm using 3 models (entities) in my app. One of those entities has 2 Transformable attributes: an array of strings and an array of doubles. I've implemented a Transformer, made sure it's added to each entity, but no matter what, I'm getting this error with random Entities and Attributes. At first it referenced the entity Quote, but then it referenced the entity MoneyElement (attribute title). This is the error: autonome crashed due to an uncaught exception NSInvalidArgumentException. Reason: -[MoneyElement setTitle:]: unrecognized selector sent to instance 0x600001736c00. I'm attaching screenshots of the different entities, as well as the code for the Entity's class that has both Transformable attributes. If anyone could help me, I'd appreciate it greatly. I'm getting the error not even making a fetch request, just trying to paint a mere and humble Text("Hello, world!") in my canvas. public class Quote: NSManagedObject, Identifiable { @NSManaged public var clientName: String @NSManaged public var quoteNumber: String @NSManaged public var dateOfCreation: Date @NSManaged public var dateOfApproval: Date? @NSManaged public var dateOfCancellation: Date? @NSManaged public var ServicesProvided: [String]? @NSManaged public var servicesCost: [Double]? @NSManaged public var netAmount: Double @NSManaged public var vatTax: Int @NSManaged public var irpfTax: Int @NSManaged public var grossAmount: Double @NSManaged public var currentStatus: String } class QuoteContainer { let persistentContainer: NSPersistentContainer init(forPreview: Bool = false) { ValueTransformer.setValueTransformer(QuoteStringArrayTransformer(), forName: NSValueTransformerName("QuoteStringArrayTransformer")) ValueTransformer.setValueTransformer(QuoteDoubleArrayTransformer(), forName: NSValueTransformerName("QuoteDoubleArrayTransformer")) persistentContainer = NSPersistentContainer(name: "Models") if forPreview { persistentContainer.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") } persistentContainer.loadPersistentStores { _, _ in} if forPreview { addMockData(moc: persistentContainer.viewContext) } } } class QuoteStringArrayTransformer: NSSecureUnarchiveFromDataTransformer { override class func transformedValueClass() -> AnyClass { return NSData.self } override func transformedValue(_ value: Any?) -> Any? { guard let array = value as? [String] else { return nil } return try? JSONEncoder().encode(array) } override func reverseTransformedValue(_ value: Any?) -> Any? { guard let data = value as? Data else { return nil } return try? JSONDecoder().decode([String].self, from: data) } } class QuoteDoubleArrayTransformer: ValueTransformer { override class func transformedValueClass() -> AnyClass { return NSData.self } override func transformedValue(_ value: Any?) -> Any? { guard let array = value as? [Double] else { return nil } return try? JSONEncoder().encode(array) } override func reverseTransformedValue(_ value: Any?) -> Any? { guard let data = value as? Data else { return nil } return try? JSONDecoder().decode([Double].self, from: data) } }
0
0
171
Oct ’24