I have 2 protocols, one that gives an object a serverId and the other one that gives him a status (aka is it published on the server or not).
The two of them allow me to handle synchronization between my server model and Core Data.
/// By conforming to this protocol, classes AND struct have an identifiable serverid `Int64`
protocol RemoteObject {
var serverId: ServerId { get set }
}
/// This can only be applied to class
protocol Syncable: class {
var syncStatus: SyncStatus { get set }
}
enum SyncStatus: Int64 {
case published
case local
}
The idea behind it is that RemoteObject can be applied to struct (i.e. The JSON structures I get back from server) and class (NSManagedObject). On the other hand, Syncable can only be applied to class (NSManagedObject).
Next step for me is, when the syncStatus is set to .local I also need to get rid of the serverId from RemoteObject on my object by setting it to -1, but when I try I get this error:
extension Syncable where Self: RemoteObject {
var syncStatus: SyncStatus {
get {
SyncStatus(rawValue: syncStatusValue) ?? .local
}
set {
syncStatusValue = newValue.rawValue
if newValue == .local { serverId = -1 } // Cannot assign to property: 'self' is immutable
}
}
}
Cannot assign to property: 'self' is immutable
I understand that I get this error because RemoteObject can be applied to structs, which are immutable.
However, considering that Syncable can only be applied to class types, doesnt it force RemoteObject to be applied on a class? and then be mutable?
Is there a way to force RemoteObject to be class type in my extension? for instance extension Syncable where Self: RemoteObject & class ?