Search code examples
macosdictionarypersistenceplistnsimage

Linking NSImages to a URL stored in a dictionary


I am building an MacOS application that allows a user to select an image file from their computer and then displays the image file in an NSImageView when called upon via an NSPopUpButton.

When the user selects the image, its URL is stored in a [String : URL] with a user-provided key. Those keys are mapped to an array from which the pop-up menu pulls its titles. Finally, the dictionary is persisted to a plist for the application.

Upon opening, the application populates the dictionary (and in turn the NSPopUpButtons) with the persisted data.

The issue is that upon reopening the images do not display, nor do they return nil. They are just blank.

I have verified that the URLs still exist and that they are associated to the same keys and I have not moved the files (i.e., it seems that the dictionary is indeed reading the plist correctly when it loads) Additionally, I have added a default image to display should an image not load through the NSImageDelegate's imageDidNotDraw(_:) method. The default does not display.

Obviously, there is something that I have not checked, but I am at a loss. Any advice that you may have would be greatly appreciated.


Solution

  • I figured it out. I had not added security-scoped bookmarks to the file URLs. Once I added them, the issue was resolved.