UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Problem creating Custom Object with CoreData

Forums > Swift

I am trying to create a dictionary app using CoreData. The data model I'm trying to recreate with CD is like this (for demonstration purposes):

struct Word: Codable {
    let id, word, transliteration: String
    let wordClasses: [WordClass]
    let userNotes: [UserNote]
    let userTag: String
}

struct UserNote: Codable {
    let userNote: String
}

struct WordClass: Codable {
    let wordClass: String
    let translations: Translations
    let examples: [Example]
}

struct Example: Codable {
    let example: String
}

struct Translations: Codable {
    let en, tr, ru: String
}

Here's the relationship model I've created.

model

When I create NSManagedObjectSubclass from these entities, for instance, WordClass and/or UserNote variables are created as objects, not object arrays, like below. Also every child object has the parent as a variable. Is that right?

I am still new to CoreData, and before I dive into this, I'd appreciate any feedback.

extension Word {

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

    @NSManaged public var id: UUID?
    @NSManaged public var word: String?
    @NSManaged public var transliteration: String?
    @NSManaged public var tag: String?
    @NSManaged public var userNotes: UserNote?
    @NSManaged public var wordClasses: WordClass?
}

extension WordClass {

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

    @NSManaged public var wordClass: String?
    @NSManaged public var word: Word?
    @NSManaged public var translations: Translation?
    @NSManaged public var examples: Examples?
}

extension Translation {

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

    @NSManaged public var en: String?
    @NSManaged public var tr: String?
    @NSManaged public var ru: String?
    @NSManaged public var wordClass: WordClass?

}

extension Examples {

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

    @NSManaged public var example: String?
    @NSManaged public var wordClass: WordClass?

}

extension UserNote {

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

    @NSManaged public var userNote: String?
    @NSManaged public var word: Word?

}

Thank you!

3      

You need to set your relationships properly. For those that you are expecting multiple values, such as userNotes and wordClasses, need to be To Many rather than To One. Your screenshot of the Core Data model shows them to be the latter.

You can set the relationship type in the inspector over on the right.

4      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.