pwb2103
pwb2103

Reputation: 540

CoreData fault: Could not materialize Objective-C class named "Array"

Since upgrading to Xcode 16, when I compile I've been seeing my logs fill up with the following notification, warn, and error:

CoreData: fault: Could not materialize Objective-C class named "Array" from declared attribute value type "Array<String>" of attribute named categories

fault: Could not materialize Objective-C class named "Array" from declared attribute value type "Array<String>" of attribute named categories

CoreData: Could not materialize Objective-C class named "Array" from declared attribute value type "Array<String>" of attribute named categories

The first line shows up in white, the second in yellow, third in red. I see it for both Array<String> and Array<UUID>, and each variable it complains about gives all three of those messages.

But I have multiple SwiftData models with Array and Array types and only a few of them are triggering this in the logs. Its possible it is just log spam that can be ignored but my app is super slow and crashes often on iOS18 (still great on iOS17!) so I can't rule this out yet.

Here also is the entire unexciting definition of the model (luckily I only have one model that has a categories parameter).

@Model
class CategoryGroupDetailsSD {
    @Attribute(.unique) var id: String
    var name: String
    var categories: [String]
    var icon: String?
    var updatedAt: Date?

    init(id: String, name: String, categories: [String], icon: String?, updatedAt: Date?) {
        self.id = id
        self.name = name
        self.categories = categories
        self.icon = icon
        self.updatedAt = updatedAt
    }

    convenience init(item: CategoryGroupDetailsDTO) {
        self.init(
            id: item.id,
            name: item.name,
            categories: item.categories,
            icon: item.icon,
            updatedAt: item.updatedAt
        )
    }
}

extension CategoryGroupDetailsSD {
    func updateProperties(from other: CategoryGroupDetailsDTO) {
        self.name = other.name
        self.categories = other.categories
        self.icon = other.icon
        self.updatedAt = other.updatedAt
    }
}

Anyone know more about these errors? How I can diagnose or solve the problem?

Upvotes: 14

Views: 2635

Answers (3)

I have the same issue with [String] and [Int]. However, I didn't make a new type for each property, but rather made both of them String (each value is separated with a comma) and now with the help of computed properties I can get the necessary value.

Upvotes: 0

Joakim Danielson
Joakim Danielson

Reputation: 52043

When using SwiftData the best way forward today is probably to wrap the base type in a custom type (struct) instead that conforms to Codable.

struct Category: Codable {
    let name: String
}

and then

@Model
class CategoryGroupDetailsSD {
    @Attribute(.unique) var id: String
    var name: String
    var categories: [Category]
    //... 
}

This might not be a solution for everyone but it will make the warnings go away. And on a personal note I think it can make the intention of the type become clearer and the code easier to read, using Category instead of String like in this case.

Upvotes: 7

Artem Golovchenko
Artem Golovchenko

Reputation: 1

Also got similar error, but with CoreData, just changed attributeValueClassName to NSArray and cast it as a type I need. Here is more information: https://forums.developer.apple.com/forums/thread/760985

Upvotes: 0

Related Questions