saoudrizwan / Disk

Easily persist structs, images, and data on iOS
MIT License
3.1k stars 170 forks source link

Fatal error with trying to save Image #18

Closed feodormak closed 6 years ago

feodormak commented 6 years ago

I've tried to save an image as shown in your example, and the app always crashes with,

fatal error: Optional does not conform to Encodable because UIImage does not conform to Encodable

When I hold down Alt/Option and hover over my declaration, it shows the declaration for the Codable initializer even though I've used Disk.save with an UIImage.

Any ideas if I am using it wrong?

saoudrizwan commented 6 years ago

Hey @feodormak, can you please comment the code that you're trying to run?

feodormak commented 6 years ago

The code i was trying to run is

@IBAction func saveToAppSupp(_ sender: UIButton) {
        let image = UIImage(named: "apple.png")
        do {
            try Disk.save(image, to: .applicationSupport, as: "apple.png")
        }
        catch { }
    }

At runtime, it gives this error,

fatal error: Optional<UIImage> does not conform to Encodable because UIImage does not conform to Encodable.: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.65.2/src/swift/stdlib/public/core/Codable.swift, line 3962
2017-10-27 14:44:56.078139+0800 boxApp[986:26400] fatal error: Optional<UIImage> does not conform to Encodable because UIImage does not conform to Encodable.: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.65.2/src/swift/stdlib/public/core/Codable.swift, line 3962

Not sure if it's related, by when I Alt-Control over the function, this is what I get, untitled and I am guessing that's why it's throwing the 'does not conform to Encodable' error.

feodormak commented 6 years ago

Ok, apologies. Solved it. My noob mistake. The UIImage optional needs to be unwrapped to be used. Thanks!

saoudrizwan commented 6 years ago

Glad you figured it out :^)