Search code examples
swiftcodableanyfile-manager

How to turn Any into Data


I want to save Any? in Swift (to FileManager).

It could be everything, also types that don't conform to Codable (like SCNNodes).

I would like to wrap it into Data. I know, I can't do it with a PropertyListEncoder.

Could you make it easy for me to understand how it works?


Solution

  • Could you make it easy for me to understand how it works?

    It doesn't. The whole meaning of Any is that it could be anything. But not just anything can be turned into a Data. That is why protocols like Codable and NSCoding (to which SCNNode conforms) exist — and why Any cannot conform to either of them. These protocols pick out the types that can be turned into a Data.