NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Advice on how to debug a strange bug.

Forums > SwiftUI

Hi All,

I hope someone maybe able to help me track down a mysterious bug that exists in my app when running on device only.

Basically, my app is very simple in that it saves its data to the users documents directory and on the simulator everything works as expected, it even works on device as you would expect too, however at some point of the night the data saved on device resets and I lauch the app to a completely blank screen... strange thing is this DOES NOT happen on the simulator at all.

My question is this, how would I best debug this kind of scenario, if it were a bug on the simulator too then maybe it'd be easier to debug but it never occurs on the simulator and running the app on device through Xcode debugging doesn't reveal much to me either as it functions as expected on every run and when the bug happens at some point of the night the app launches as you'd expect for a clean install on every launch after.

Any help and advice would be very much appreciated.


Hi All,

Quick update to my above question, I now have an error to report regarding this issue and it's one that I am struggling to understand. As this issue only occurs on device I programmed in a debug option to capture the error and report to me and the error was: "The file "SavedCounts" couldn't be opened because you don't have permission to view it" this is strange as it's my own device and I cannot for the life of me understand where this error is coming from, or how to solve this issue.

Now as I know the error is coming from and what it's related to, here is my loading code.

init() {
        do {
            let savePath = getUserPath().appendingPathComponent("SavedCounts")
            let data = try Data(contentsOf: savePath)
            items = try JSONDecoder().decode([Item].self, from: data)
        } catch {
            errorString = error.localizedDescription
            items = []

func save() {
        do {
            let savePath = getUserPath().appendingPathComponent("SavedCounts")
            let data = try JSONEncoder().encode(items)
            try data.write(to: savePath, options: [.atomic, .completeFileProtection])
        } catch {
            print("Unable to save due to an error!")

 extension FileManager {

    static var documentsDirectory: URL {
        let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)

        return path[0]


As you can see, this is all pretty standard code and used by many, can anyone help or even give more context into why this is happening please?

Thanks in advance!


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.