Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Codable conformity to DrawItem #40

Merged
merged 2 commits into from
Aug 21, 2020
Merged

Conversation

blu3mo
Copy link
Contributor

@blu3mo blu3mo commented Aug 15, 2020

Issue #21
I've added Codable conformity to DrawItem.

encode(to encoder:) first converts CGMutablePath to UIBezierPath, which conforms to NSSecureCoding.
It then converts UIBezierPath to Data using NSKeyedArchiver.
Further Information: https://www.hackingwithswift.com/example-code/language/how-to-store-nscoding-data-using-codable

DrawItem could be encoded/decoded by the code below.

let encodedData = JSONEncoder().encode(drawItem)
let decodedDrawItem = JSONDecoder().decode(SwiftyDrawView.DrawItem.self, from: encodedData)

@blu3mo
Copy link
Contributor Author

blu3mo commented Aug 16, 2020

The code below could be used to test encoding and decoding.

    var encodedDatas: [Data] = []
    
    @IBAction func encodeDrawItems() {
        encodedDatas = []
        for drawItem in drawView.drawItems {
            let encodedData = try! JSONEncoder().encode(drawItem)
            encodedDatas.append(encodedData)
        }
        
        drawView.clear()
    }
    
    @IBAction func decodeData() {
        drawView.clear()
        
        for encodedData in encodedDatas {
            let decodedDrawItem = try! JSONDecoder().decode(SwiftyDrawView.DrawItem.self, from: encodedData)
            drawView.addLine(decodedDrawItem)
        }
        
        drawView.setNeedsDisplay()
    }

@LinusGeffarth LinusGeffarth merged commit 0108964 into Awalz:master Aug 21, 2020
@LinusGeffarth
Copy link
Collaborator

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants