SwiftData iOS18: "Could not materialize Objective-C class named "Set" from declared attribute value type "Set<String>" of attribute named..."

Posting here to see if folks have workarounds or if I have a misunderstanding of SwiftData supported types.

In adopting SwiftData, I have swiftData properties of collection type (Array or Set - both have this issue). E.g:

@Model
final class Item {
    var timestamp: Date
    var strings = ["aa", "bb"]
    
    var display: String {
        strings.joined(separator: " ")
    }
    
    init(timestamp: Date) {
        self.timestamp = timestamp
    }
}

So far in development I haven't had issues on iOS 17, but on the iOS 18 betas 4-5 the app logs show the following error:

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

It happens immediately in my app when creating an object with a collection attribute. In a minimal test example, the error log appears only after a few minutes and doesn't seem to affect the template app's basic functionality.

Anyone else running into this?

Was filed as FB14397250

Swapping out the Item.swift file in Xcode's default project with Swift Data seems to work fine. Could you provide more steps to reproduce?

With that minimal example, (now testing on beta 6), if you add some items, after ~45 seconds those faults appear in the logs. This happens consistently, though doesn't seem to affect functionality.

In beta 6, my larger app seems to be able to function fine while still emitting these log lines (previously it would fail to save new models in the context)

I'm getting similar logs in my production app on iOS 18. CoreData: Could not materialize Objective-C class named "Array" from declared attribute value type "Array<String>" of attribute named ***

Have a similar issue (same error) just using CoreData with a transformable attribute with a NSSecureUnarchiveFromDataTransformer transformer and a Custom Class of Set<String>.

Frustratingly this error happens in some unit tests, but not consistently. Using NSSet appears to be working for the moment, but I'm not confident that it's gone unless I can reproduce the issue consistently.

@mark-ger

Was filed as FB14397250

Shows "Feedback not found"

I see the same issue. I have a handful of @Model classes with string arrays; they each produce this error consistently, whether the property is optional or not.

@Model
class Monster: Decodable, {
 var something: [String] = []
 var somethingElse: [String]? = nil
}

Same. With both optional and non optional arrays.

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

I just stumbled onto the same problem, and I still can't find a solution documented online. But I had read already, that CoreData has issues with arrays of strings. What seems to work is creating a different data type when it comes to storing... The simplest solution for me was to convert the array to a string before storing, if you don't want to create a new class, as suggested in the comments.

In my case it looks like this:

    private var longString: String = ""
    var strings: [String] {
        get {
            longString.isEmpty ? [] : longString.components(separatedBy: .newlines)
        }
        set {
            longString = newValue.joined(separator: "\n")
        }
    }```

SwiftData iOS18: "Could not materialize Objective-C class named "Set" from declared attribute value type "Set&lt;String&gt;" of attribute named..."
 
 
Q