UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 85 Challange - Writing to the documents directory

Forums > 100 Days of SwiftUI

Hello,

When doing the challange for HotProspects(Day 85) i was writing the initializer and save method like this, basically copying from the bucketlist project and this worked fine from what i can see.

let savePath = FileManager.documentsDirectory.appendingPathComponent("SavedData")

    init() {
        do {
            let data = try Data(contentsOf: savePath)
            people = try JSONDecoder().decode([Prospect].self, from: data)
        } catch {
            people = []
        }
    }

    private func save() {
        do {
            let data = try JSONEncoder().encode(people)
            try data.write(to: savePath, options: [.atomicWrite, .completeFileProtection])
        } catch {
            print("Unable to save data: \(error.localizedDescription)")
        }
    }

Paul however did it like this:

let saveKey = "SavedData"

    init() {
        if let data = try? Data(contentsOf: savePath {
            if let decoded = try? JSONDecoder().decode([Prospect].self, from: data) {
                people = decoded
                return
            }
        }
        // No saved data, empty array by default
        people = []
    }
    private func save() {
        if let encoded = try? JSONEncoder().encode(people) {
            try? encoded.write(to: savePath, options: [.atomic, .completeFileProtection])
        }
    }

So the code from the Bucketlist is with a "do-catch" block and the one from HotProspects is with if statements, please correct me if im wrong.

To my question then, is there a profound difference here or are both as viable ?

Thanks in advance! Also, first time i write on the forum so if anything is wrong with the copy and pasted code you know why x)

1      

Both work fine, the only difference is that obviously the do - catch version prints out an error message and thus gives you more debugging information if something doesn't work. In a full app you can also use the catch block to give the user an error message or try to recover from it. The try? is basically a short cut.

1      

Using try? like in the second example converts the result to an Optional, with the effect of hiding any error that occurs behind a nil value. That may or may not be what you want to do. You would need to evaluate what the function is doing and which method of error handling is appropriate.

1      

Ah, thank you very much! =)

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.