This turns out to be flexible. I ended up putting my files in a directory I named âSampleFilesâ, inside my âTestsâ directory (which contains separate directories for each test target, such as unit tests and UI tests).
Important: The key to having the files available through the bundle is to ensure they are included in the âCopy Bundle Resourcesâ phase of the âBuild Phasesâ tab for the target.
If the resources are in the appâs main bundle, just use Bundle.main
.
But if they are in a different target bundle, I havenât found a better answer than this âbrute forceâ approach, which requires there be a known file in the bundle resources (in this example, sample.file
):
private static func findSampleBundle() -> Bundle? {
for bundle in Bundle.allBundles {
if bundle.url(forResource: "sample", withExtension: "file") != nil {
return bundle
}
}
return nil
}
See question 1.
See question 2.
Having got the bundle
(answer to question 2):
let sampleFileURL = bundle.url(forResource: "sample", withExtension: "file")
No answer yet.