Bronze Challenge Question

Thank you for your help! I tried what you suggested and that causes the app to trap, even if the notificationCenter.addObserver method is called within the do block in the do/catch block that was added in the initializer of ItemStore.

Adding try anywhere in the aforementioned method just causes the compiler to show the warning “No calls to throwing functions occur within ‘try’ expression” so I guess any errors thrown in a method called in the selector parameter of notificationCenter.addObserver need to be handled differently? in an Objective-C kind of manner?

Here’s what my ItemStore initializer looks like, here’s where I attempt to catch the errors:

init() {
        do {
            let data = try Data(contentsOf: itemArchiveURL)
            let unarchiver = PropertyListDecoder()
            let items = try unarchiver.decode([Item].self, from: data)
            allItems = items
            
            let notificationCenter = NotificationCenter.default
            notificationCenter.addObserver(self,
                                           selector: #selector(saveChanges),
                                           name: UIScene.didEnterBackgroundNotification,
                                           object: nil)
        } catch Error.encodingError {
            print("Couldn't encode items.")
        } catch Error.writingError {
            print("Couldn't write to file.")
        } catch {
            print("Error reading in saved items: \(error)")
        }
    }