Codable and structs

the documentation abruptly ends where my questions begin.

I have a mac os X document app. please refer to this if your experience is in IOS, it most likely will not be of very much help.


Background:

I am currently trying to adopt Codable, very much against my better instincts, because a number of subsystems in the Mac OS have been fairly recently uncerimoniously kicked down a flight of stairs when NSCoding was damaged. I backed into the knowlege that NSCoding no longer worked for custom classes. The system returns nil, on decoding these classes. Then I got neck deep in a mess trying to implement NSSecureCoding, which went precisely nowhere.

Anyway, If I want to continue working on any aspect of my app, I need to get archiving working. NSCoding, and NSSecureCoding don't work. The only other option is Codable. This is not the time to take my fledgling app and cut it's head off and try to graft a new on on it... But I have no choice.


the issue:

public struct BKColorComponents: Codable{
    public var red : CGFloat = 1.0
    public var green : CGFloat = 1.0
    public var blue : CGFloat = 1.0
    public var alpha : CGFloat = 1.0
}


by all accounts of online tutorials and the byzantine documentation from Apple HQ, this struct is now Codable compliant.

except that it crashes the file save. Here's that code in context:


open var components : BKColorComponents = BKColorComponents()


enum CodingKeys: String, CodingKey {
        case components
    }



public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(components, forKey: .components)
    }



my document class saving method:

override func data(ofType typeName: String) throws -> Data {
    return try PropertyListEncoder().encode(docData)
}


there's not much documentation about how to do that at all. I'm not fond of the PropertyListEncoder, but whatever. I'm following along with someone's tutorial, because Apple has dropped the ball in a major way on the subject of archives and serialization. Believe me, I'd rather look up the answer than ask in the forums, I tried. The most important thing about this is that it serializes, and it runs through a host of other objects before it hits this structure, and they all encode without complaint. But for some reason, as soon as this struct comes up, the app crashes.


there's no errors, no red flags of any kinds.

I just get this error during a save (which seems to take about 10 times longer than it should):

Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3fff50)


thoughts appreciated.

I did just find out that PasteboardEncoder is not the same thing as Encoder. But Encoder has no accessible initializers. That seems to indicate it's not a concrete class.


Encoder is a protocol. It is clearly documented: https://developer.apple.com/documentation/swift/encoder


because pasteboard behaves slightly different from everything else. it wants a Data object that has keys for the properties in your object,


I do not understand this part. Generally macOS accepts arbitrary NSData from `pasteboardPropertyList(forType:)`.

What happens when you return a simple Data?

Codable and structs
 
 
Q