Tom
Tom

Reputation: 3627

swift ios and geing final grip on copy bundle resources

I am currenly converting an iOS project built in another tool to xcode/swift.

I currently have an xcode swift ios project with multiple targets defined (one for each customer)

For each customer I have a folder "customerxyzassets" that I have added to "target > build phases > copy bundle resources" using the process described here Include a resource directory hierarchy into app bundle

This folder "customerxyzassets" contains subfolders with images and data files which the app is born with.

I would like to grab a path to this folder upon startup, so I can access load datafiles, images etc. from it.

However, the code I have found, e.g. NSBundle.mainBundle, seems to require speciel access to the files through the above. I would rather have raw file access to it. Am i missing something obvious?

Upvotes: 1

Views: 293

Answers (1)

Rob Napier
Rob Napier

Reputation: 299325

It's not clear what you mean here by "special access" or "raw access." NSBundle just returns you paths or URLs so you can directly access the files using normal file APIs.

If you've created a directory structure, then you would generally use pathForResource(_:ofType:inDirectory:) to fetch the path to your specific file. Alternately, you can build a path using NSBundle.resourcePath and append your relative path using stringByAppendingPathComponent. The advantage of the pathForResource methods is that they handle localization for you, and this is preferred unless the resource should never be localized (which is rare).

Upvotes: 1

Related Questions