As the documentation mentions I have implemented an extension CGPoint: CustomPersistable to persist CGPoint values. So far so good. Additionally I have to persist CGPoint values in SwiftUI’s @SceneStorage. For this I added another CGPoint extension which implements RawRepresentable.
extension CGPoint: RawRepresentable {
public var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let string = String(data: data, encoding: .utf8)
else {
return "{}"
}
return string
}
public init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode(CGPoint.self, from: data)
else {
return nil
}
self = result
}
}
Unfortunately these two extensions don’t want to coexist. If I implement the RawRepresentable extension I get an error for the CustomPersistable extension.
When posting, please include the code textually - that way if we need to use it in an answer we won’t have to re-type it. Also, we need a clearer picture of your objects.
The PersistedType on CGSize must be a type Realm Supports - what is CGSizeObject?
Ah, now I see my mistake. It’s the wrong screenshot. It is for CGSize instead CGPoint. However, I made the same extensions for all three types CGPoint, CGSize and CGRect. It’s every time the same error for all three.
CGSizeObject is undefined. We don’t know what that is and there’s a possibility Realm doesn’t know what it is either as it’s not one of the supported types - unless it’s Type Projected as well - but we don’t know that.
Please include what CGSizeObject looks like so we can eliminate it as being the issue.
It’s similar to the CGPointObject of my example from above. It doesn’t matter. The error message is always the same, for all three types when I add the RawRepresentable extension.
For the sake of completeness, this is the CGSizeObject:
public class CGSizeObject: EmbeddedObject {
@Persisted var width: Double
@Persisted var height: Double
}
and its belonging CustomPersistable extension:
extension CGSize: CustomPersistable {
public typealias PersistedType = CGSizeObject
public init(persistedValue: CGSizeObject) {
self.init(width: persistedValue.width, height: persistedValue.height)
}
public var persistableValue: PersistedType {
CGSizeObject(value: [width, height])
}
}