SwiftData: var dates: [Date]? or var dates: [Date] = []

I am trying to get my head around SwiftData, and specifically some more "advanced" ideas that I have not seen covered in the various tutorials.

Specifically, I have a class that includes a collection that may or may not contain elements. For now I am experimenting with a simple array of Date, and I don't know if I should make it an optional, or an empty array. Without SwiftData in the mix it seems like it's probably programmers choice, but I wonder if SwiftData handles those two scenarios differently, that would suggest one over the other.

So, the lack of a response to what I thought was a pretty straightforward question has me worried that the Apple forums are really focused on much more in depth/interesting questions. I looked for a FAQ, to see if there is any guidance on what kind of questions are considered "appropriate", but as far as I can tell there isn't. I found someone back in 2015 asking about a FAQ, and someone at Apple saying good idea. But still no FAQ. I would try on StackOverflow, but I have found the quality there isn't what it used to be. Anyway, if anyone wants to chime in saying this original question really doesn't meet the requirements here, please do! And for now I am using the Optional approach because it works better for other reasons, and I guess if there is a problem I will find out the hard way once I start dogfooding my app. :)

Personally I'd go for an empty array instead of an optional array as it's a lot more readable to simply check for dates.isEmpty vs. if let dates = dates or guard let dates = dates else { .. } and then still having to check if there are actually any values inside the array.

From a SwiftData-Perspective neither version has advantages because you're storing an array of basic data types data types which SwiftData will have to serialise and store as a blob along with the Model instance they belong to.

Things would be different if we were talking about an array of @Model instances because then you'd be building a one-to-many relationship which is something that can be represented efficiently in any database and in which case you absolutely want to use var foos: [Foo] = []() in the class holding the array.

SwiftData: var dates: [Date]? or var dates: [Date] = []
 
 
Q