In my game app, I've been loading resources by building paths directly based on the Bundle resourcePath property, e.g. (error-handling omitted):
let path = "\(Bundle.main.resourcePath!)/data/text/example.txt"
do {
let text =
try String(contentsOfFile: path, encoding: String.Encoding.utf8)
} catch {
}I have reasons for doing it this way having to do with a JavaScript scripting framework, and it's always worked fine.
However, I've seen multiple suggestions online to use URLs rather than strings for paths, and to use bundle functions such as url(forResource...) to acquire resource paths rather than building paths manually.
What I haven't found is an explanation of why it shouldn't be done the way I'm doing it. I understand that URLs offer features that string paths don't, but these features don't seem relevant for my particular use case. I could use URLs and the Bundle resourceURL property instead, but it still seems to be the case that building resource paths or URLs manually is discouraged, and that using functions such as url(forResource...) is recommended instead.
In short, my question is, is there actually anything wrong with using resourcePath or resourceURL in this way? Could this approach possibly fail in some circumstances? Why exactly is this approach discouraged?
I want to follow best practices, but I'd just like to have a better understanding of the underlying motivations behind these recommendations.
Edit: I should add that the resources in question aren't localized, so that isn't a factor in this particular case.