swiftdataios18xcode16

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?


Solution

  • 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.