I have a model class in my app which uses SwiftData / CloudKit:
@Model
final class Project
{
var name: String = ""
var avatarData: Data?
var hasAvatar: Bool = false
...
As you can see there is an optional avatarData property for the user to store an image in Project. I’ve included a hasAvatar bool so that I can tell the difference between the user having not set an avatar vs CloudKit not loading the avatarData. This way works but obviously it’s annoying having to manage two variables. I’ve thought about doing it this way:
@Model
final class Project
{
var name: String = ""
var avatarData: Data = Data()
...
Which is obviously simpler, if avatarData is nil then CloudKit hasn’t loaded it, if it’s empty then the user never set an avatar. But I’m just confused. I know in SwiftData / CloudKit, you have to make model class properties / relationships optionals but are still able to initialize value types like Strings, Ints, etc. I believe Data is a value type, but in my program I would be storing image files in avatarData that can be big. Just confused about how I should approach this and wondering if initializing avatarData to empty instead of nil is a better way to go about it.