Regarding mtsrodrigues's solution, how would one get to the viewContext property elsewhere in the app?
I'm asking because I want to be able to run a preview but don't know what to give to the PreviewProvider. You can't use (UIApplication.shared.delegate as! AppDelegate).persistentContainer because there isn't an app delegate in this case.
And if you do use an app delegate, you have to be calling UIApplication, which means you lose cross-platform Mac support, or you have to do an @available dance, I guess?