Get All Urls for Resources in Sub-Directory in Swift

Get all URLs for resources in sub-directory in Swift

Consider that the yellow folders Sample Image are virtual groups, not real folders (although Xcode creates real folders in the project directory). All files in the yellow folders are moved into the Resources directory in the bundle when the app is built.

Real folders in the bundle are Sample Image in the project navigator.

Get resource URL from project specific path

You would use one of the bundle resourse URL methods

[[NSBundle mainBundle] URLForResource:@"acknowledge1"
withExtension:@"wav"
subdirectory:@"snd/archer"];

NSBundle.mainBundle().URLForResource("acknowledge1", withExtension:"wav" subdirectory:"snd/archer")

In latest Swift:

Bundle.main.url(forResource: "acknowledge1", withExtension:"wav")

Get nil when looking for file in subdirectory of main bundle

The solution is to use

Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "games/game1")

I don't know why, but it doesn't work if we get a path, then construct an URL from it.

Note: You must

  1. Copy items if needed
  2. Create folder references (not "Create groups")
  3. Add to targets

How to get path to a subfolder in main bundle?

In Swift-3 make URL, and call appendingPathComponent:

let resourcePath = Bundle.main.resourcePath
let subdir = URL(fileURLWithPath:resourcePath!).appendingPathComponent("sub").path

or simply

let subdir = Bundle.main.resourceURL!.appendingPathComponent("sub").path

(thanks, Martin R!)

See this Q&A on information on stringByAppendingPathComponent method in Swift.

Get Path to Subdirectory in Resources Folder

Just solved it.

NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *pngs = [mainBundle pathsForResourcesOfType:@".png"
inDirectory:@"random pictures"];

NSLog(@"pngs in my dir:%@", pngs);

dir structure: "Resources/random pictures".

This works, BUT, when you add the files to 'Resources', you need to check "Create folder references for any aded folders", and NOT "Create groups for any added folders".

important step

Cheers!

Iterate through files in a folder and its subfolders using Swift's FileManager

Use the nextObject() method of enumerator:

while let element = enumerator?.nextObject() as? String {
if element.hasSuffix("ext") { // checks the extension
}
}

Getting a list of files in the Resources folder - iOS

You can get the path to the Resources directory like this,

NSString * resourcePath = [[NSBundle mainBundle] resourcePath];

Then append the Documents to the path,

NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"];

Then you can use any of the directory listing APIs of NSFileManager.

NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

Note : When adding source folder into the bundle make sure you select "Create folder references for any added folders option when copying"

iPhone : Get the file path which is within subfolder of Resource folder

To continue psychotiks answer a full example would look like this:

NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *filePath = nil;

if (filePath = [thisBundle pathForResource:@"Data" ofType:@"txt" inDirectory:@"Folder1"]) {

theContents = [[NSString alloc] initWithContentsOfFile:filePath];

// when completed, it is the developer's responsibility to release theContents

}

Notice that you can use -pathForResource:ofType:inDirectory to access ressources in sub directories.

(Swift) How to make subfolders in one folder

Your code should be like below,

extension FileManager.SearchPathDirectory {
func createSubFolder(named: String, withIntermediateDirectories: Bool = true) -> Bool {
guard let url = FileManager.default.urls(for: self, in: .userDomainMask).first else { return false }
do {
try FileManager.default.createDirectory(at: url.appendingPathComponent(named), withIntermediateDirectories: withIntermediateDirectories, attributes: nil)
return true
} catch {
print(error)
return false
}
}
}

I have just set withIntermediateDirectories's default value to true.

createIntermediates: If true, this method creates any nonexistent parent directories as part of creating the directory in url. If false, this method fails if any of the intermediate parent directories does not exist.



Related Topics



Leave a reply



Submit