# Fully qualified class names in combination with CoreData

**URL:** <https://forums.swift.org/t/fully-qualified-class-names-in-combination-with-coredata/69193>\
**Category:** Using Swift\
**Tags:** coredata\
**Created:** [December 28, 2023, 8:32pm UTC](https://forums.swift.org/t/fully-qualified-class-names-in-combination-with-coredata/69193 "2023-12-28T20:32:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredap](https://avatars.discourse-cdn.com/v4/letter/f/94ad74/32.png) [@fredap](https://forums.swift.org/u/fredap)\
**Post date:** [December 28, 2023, 8:32pm UTC](https://forums.swift.org/t/fully-qualified-class-names-in-combination-with-coredata/69193/1 "2023-12-28T20:32:25Z")

</div>

Hi,

I have a problem with a combination of CoreData and Class naming.

I am generating the CoreData model dynamically by code. I. Have the following code fragment (highly simplified) that works:

```swift
class Test: NSManagedObject {
    @NSManaged var attribute: Date
}

class AttributeTests: XCTestCase {

  func testDefaultValueDate3() throws {
    let model = try CoreDataModel(databaseName) {
      Entity(Const.entityName, managedObjectClass: Test.self) {
        Attribute("attribute", type: Date.self)
          .defaultValue(Const.defaultDateValue3)
        }
    }
    let container = model.createInMemoryContainer()
    let testRecord(context: container.viewContext)
  }
}

```

The above code works as expected. Inside the model I am using `String(reflecting:)` to get the fully qualified name of the `Test` class. So far so good.

The problem arises if I move the `Test` class inside the test framework.

```swift
class AttributeTests: XCTestCase {

  func testDefaultValueDate3() throws {
    class Test: NSManagedObject {
        @NSManaged var attribute: Date
    }

    let model = try CoreDataModel(databaseName) {
      Entity(Const.entityName, managedObjectClass: Test.self) {
        Attribute("attribute", type: Date.self)
          .defaultValue(Const.defaultDateValue3)
        }
    }
    let container = model.createInMemoryContainer()
    let testRecord(context: container.viewContext)
  }
}

```

This now fails with an error message like:

> An NSManagedObject of class '\_TtCFC18CoreDataModelTests14AttributeTests21testDefaultValueDate3FzT\_T\_L\_4Test' must have a valid NSEntityDescription. (NSInvalidArgumentException)

The problem is coming from the fact that I cannot figure out what the fully qualified name of the `Test` class is. If I check the output of the statement `print(String(reflecting: Test.self))` I get the output:

> CoreDataModelTests.AttributeTests.(unknown context at $1014d58f4).(unknown context at $1014d5900).Test

Does someone know how to get the fully qualified name of the `Test` class? Can I somehow address the "unknown context" message.

---

<div class="post-metadata">

**Author:** ![Dlemex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dlemex/32/9827_2.png) [@Dlemex](https://forums.swift.org/u/Dlemex)\
**Post date:** [December 28, 2023, 9:38pm UTC](https://forums.swift.org/t/fully-qualified-class-names-in-combination-with-coredata/69193/2 "2023-12-28T21:38:14Z")

</div>

Add an @objc(Test) to the class. It doesn't matter that you are not using Objective-C. There are requirements in CoreData. Finally, it is best to keep the model in a static (especially when testing) as multiple model instances will confuse everything.

---

<div class="post-metadata">

**Author:** ![fredap](https://avatars.discourse-cdn.com/v4/letter/f/94ad74/32.png) [@fredap](https://forums.swift.org/u/fredap)\
**Post date:** [December 29, 2023, 7:35am UTC](https://forums.swift.org/t/fully-qualified-class-names-in-combination-with-coredata/69193/3 "2023-12-29T07:35:25Z")

</div>

Thank you for your reply. Just for clarity I changed it into:

```swift
class AttributeTests: XCTestCase {
  func testDefaultValueDate3() throws {
    @objc class Test: NSManagedObject {
        @NSManaged var attribute: Date
    }
    ....
}

```

Unfortunately this does not solve the issue. The behavior is identical.

---

<div class="post-metadata">

**Author:** ![fredap](https://avatars.discourse-cdn.com/v4/letter/f/94ad74/32.png) [@fredap](https://forums.swift.org/u/fredap)\
**Post date:** [December 29, 2023, 11:19am UTC](https://forums.swift.org/t/fully-qualified-class-names-in-combination-with-coredata/69193/4 "2023-12-29T11:19:39Z")

</div>

To follow-up on myself. It turns out that you can set the class name using the following syntax:

```swift
class AttributeTests: XCTestCase {
  func testDefaultValueDate3() throws {
    @objc(Test) class Test: NSManagedObject {
        @NSManaged var attribute: Date
    }
    ....
}

```

I can work with this. Thanks for the support.

---

<div class="post-metadata">

**Author:** ![Dlemex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dlemex/32/9827_2.png) [@Dlemex](https://forums.swift.org/u/Dlemex)\
**Post date:** [December 29, 2023, 5:31pm UTC](https://forums.swift.org/t/fully-qualified-class-names-in-combination-with-coredata/69193/5 "2023-12-29T17:31:46Z")

</div>

Yeah, adding the class name is required to the objc declaration is required. Sorry, I should have been clearer. Good luck.
