I had the same error. Right click on your SwiftData Model class name and Refactor to a different name.
There are some Model names you can't use. Xcode never warns you, it just produces an error.
Post
Replies
Boosts
Views
Activity
Ok, I think I figured it out. So.. when you generate SwiftData Objects from Core Data and if you have Transformable Data like above what I've done with an Array of Strings, you will get
@Attribute(.transformable(by: "NSSecureUnarchiveFromData"))
var yourArrayOfStrings: [String]?
However, SwiftData is so good that it already recognizes these Array of Strings so having that Attribute confuses Xcode to thinking you're using NSKeyedUnarchiveFromData. The solution is to just delete that attribute like so...
var yourArrayOfStrings: [String]?
And it just works! The warning goes away in the Debugger.
It seems with a new Xcode update, the app doesn't crash anymore but still produces the error every time the variable is read from the model. So in a view when it's repeatedly read. The error floods my debugger and doesn't stop.
Here is what the Transformer looks like in the CoreData version of the app.
I tried changing to @Attribute(.transformable(by: "NSSecureUnarchiveFromDataTransformer")) too.
Same issue with my transformable array of strings in Xcode 15.2 and iOS 17.3. I'm switching from CoreData to SwiftData and using NSSecureUnarchiveFromData. But still get this error and then the app crashes due to memory. Frustrating I can't move to SwiftData after all of the work I put into changing my code. Good thing I saved a copy of the CoreData version.
https://developer.apple.com/forums/thread/744688
I had to go back to a UIViewControllerRepresentable as a temporary fix till this bug is resolved.
Same issue. I'm waiting for an Apple Silicon Macbook Pro and my current Mac doesn't support Big Sur so I can't test. I got the macOS version approved but unapproved on an update due to Catalyst's messiness. Nobody has downloaded the app from the Mac App Store anyways, so I'd rather remove that and wait till I get a new Mac rather than just blindly compiling for Mac App Review and praying.
I'm thinking the only solution is to remove the app entirely and resubmitting.
Ok, here's the code. Part of the first view. You can see that I pass the ship name to a view in .sheet
struct ShipEditorView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: Ship.getAllShips()) var ships:FetchedResults<Ship>
@State private var newShip = ""
@State var shipSelected = ""
@State var showingAddShipAlert = false
@State var showingShipEditor = false
@State var edit = false
var body: some View {
ZStack(alignment: .bottomTrailing){
VStack{
List{
ForEach(self.ships) { ship in
if self.edit == true {
Button(action: {
print("Opens Ship Editor")
self.shipSelected = ship.shipName!
self.showingShipEditor = true
}) {
Text(ship.shipName!)
}
.sheet(isPresented: self.$showingShipEditor) { ShipEditingView(shipName: self.shipSelected) }
} else {
NavigationLink(destination: ProductEditorView(shipName: ship.shipName!)) {
Text(ship.shipName!)
}
}
} .onDelete {indexSet in ...
Then the second view. When this view shows up, the ship textfield is blank. I even tried printing the shipName and shipText and nothing shows up.
struct ShipEditingView: View {
let shipName: String
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
@State var context: NSManagedObjectContext!
@State var shipText = ""
var body: some View {
NavigationView{
VStack{
Form{
Section(header: Text("Rename Ship")) {
TextField("Titanic", text: $shipText)
.font(.title)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
Button(action: {
self.saveEdits()
self.mode.wrappedValue.dismiss()
}) {
Text("Done")
.font(.title)
}
Spacer()
}
.navigationBarTitle(Text("Rename Ship"))
.onAppear() {
self.shipText = self.shipName
}
}.navigationViewStyle(StackNavigationViewStyle())
}
func saveEdits() { ...
I'm wondering if it's a bug with SwiftUI. Running Beta 8 and I've noticed that if I tap on the ship I want to edit and dismiss the sheet a few times, then the ship name will show up in the textfield and never fails with different ships after that.