Swift Data Array with Elements to conform protocol

Hi,

I'm try to convert from CoreData to SwiftData. Problem is that I'm using inheritance in CoreData.

So, I have a base class "animal" and "cat" and "bird" inherit from it. I can have a array of type "animal" which contains both cats and dogs.

Now as I try to move to SwiftData I tried the following approach

protocol Animal {
  var name: String { get set }
}

@Model 
class Cat: Animal {
  var name: String
  var legs: Int
}

@Model
class Bird: Animal {
  var name: String
  var legs: Int
  var wings: Int
}

@Model
class Enviroment {
//leads to error:
//Type 'any Animal' cannot conform to 'PersistentModel'
  var animals: [any Animal]
}

So how to I get around of this? I would like to store multiple types of animals (classes that conform to protocol animal) … but how can I achieve that? Thanks

Swift Data Array with Elements to conform protocol
 
 
Q