How to get all png from a specific folder in Xcode? - ios

I would like to get all png in a specific folder in Xcode. I am currently using this code right now to get PNG file.
(Bundle.main.paths(forResourcesOfType: "png", inDirectory: nil) as NSArray).enumerateObjects({ obj, idx, stop in
var path = (obj as! NSString).lastPathComponent
print("Path - ",path)
})
but this get all PNG from all folder for example - Example-1
I would like to get images only inside the Fonts folder. How can i do that ?

As I look at your code, I assume you want to get all image paths from Fonts folder from Bundle App Folder. In this case, you must make your Fonts folder to be Reference Folder (blue icon), not Group folder (yellow icon), like this:
You can drag your folder in your Project Navigator, it will display this box:
Be sure to choose Create Folder References and choose your project target.
The reason is Reference folder is a real folder in your Bundle. All files in Group Folder will be flatted, which mean they are all in the root directory of your Bundle, then you won't know the parent folder of these files.
Then you can use your code to retrieve the png (+ you should specify "Fonts" for inDirectory parameter):
(Bundle.main.paths(forResourcesOfType: "png", inDirectory: "Fonts") as NSArray).enumerateObjects({ obj, idx, stop in
var path = (obj as! NSString).lastPathComponent
print("Path - ",path)
})
Hope this helps.

You need to perform these two steps to achieve your goal.
Pass the folder name in inDirectory argument.
Something like this:
let pngs = Bundle.main.paths(forResourcesOfType: "png", inDirectory: "Fonts")
Make sure that the Fonts directory is added into project as Folder Reference instead of a Group.
Hope this helps

Related

Playwright cant find a located file using a wsl

I want to upload a file using playwright, but as I'm using a wsl, I don't know where I should put the file, for sure not in the windows folder, but where? Cause I tried to put it in the mounted disk in ubunbtu, but it isn't editable, I'm using SetInputFiles().
In the image you can see the file manager that playwright opens, i cant find the node folder in the image.Image
You can keep the file in the project fiolder itself and give relative path for that file. Make one folder in project name it as 'TestData' and copy file in the folder. if file name is test.png then you can give path as give below.
let path = require('path');
let fileToUpload = '/testData/fileUpload/' + fileName;
let absolutePath = path.resolve(process.cwd() + fileToUpload);
await this.page.locator("input[type='file']").setInputFiles(absolutePath);

How to access a list of all files in resource folder of my project?

I have some .TXT files saved in Resource folder of my project. I want to display a list to user in which all files from resource folder are shown and user can select the file he desires.
Later on i will read the user selected file and show it on screen.
Take a look at the NSBundle function pathsForResourcesOfType:inDirectory: That will give you a list of the paths to all the files in a sub-bundle of a bundle. if you call that method on the main bundle you'll get a list of all the files of a certain type in a sub-directory of the main bundle.
(I have no idea how to make use of these functions from xamarin.)
This gets FileInfo's on all txt files in the resources:
var fileInfos = NSBundle.GetPathsForResources(".txt", path)
.Select(a => new FileInfo(a));
Now you have the short name, full name etc to play with:
foreach (var fileInfo in fileInfos)
{
System.Diagnostics.Debug.WriteLine(fileInfo.Name);
using (var streamReader = new StreamReader(new FileStream(fileInfo.FullName, FileMode.Open)))
{
System.Diagnostics.Debug.WriteLine(streamReader.ReadToEnd());
}
}

Scanning the contents of a directory which is stored in an Xcode project in swift

This seems to be a problem which is very difficult to explain. I am trying to read data from a folder full of files and the way I am trying to do this is by scanning the whole directory for all of the files. The file in question is stored in my Xcode project in the same directory as the view controllers and storyboards. Like so:
I would like to be able to scan the contents of the "data" folder. Consider the following code:
var tmpDir = NSTemporaryDirectory()
var error: NSError?
let filesInDirectory: [String]! = fileManager.contentsOfDirectoryAtPath(tmpDir, error: &error) as? [String]
This scans the contents of the app's local folder and returns it as an array of the file names. I would like to be able to do this with the "data" folder in my Xcode project. The following code will find the contents of a single file which is stored in the same directory as the "data" folder.
let files = NSBundle.mainBundle().pathForResource("sampleFile", ofType: "csv")
var contents = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
Is there a way in which I can combine the capabilities of the two in order to scan the contents of the "data" folder in the Xcode project and then return the file names as an array?
Add the data folder to Copy Bundle Resources in Build Phases of your target.
Then you'll be able to read the path as it's within the same sandbox scope. (Assuming you are applying sandbox.) If not, just use absolute path/URL and you can access it directly without the copying process.
You might want to have a look here: Cocoa contents of directory

Read file in Xcode project with Swift

I made a method to read a file from the temporary folder. I use the following lines to get the document's path:
path = NSTemporaryDirectory().stringByAppendingPathComponent(folder)
path = path.stringByAppendingPathComponent(fileName + "." + fileType)
And it works great. My question is: how do I get the path for a file located in the main folders of the Xcode project? They are two text files that I drag and drop from a folder to the Xcode project. I selected the option "copy files if needed" when dropping the files. Thanks!
What you are looking for is app's main bundle. You get a path of a file in it like this:
if let path = NSBundle.mainBundle().pathForResource(fileName, ofType:fileType) {
// use path
}
Keep in mind this folder is read-only so you will need to copy the files to temp or documents folders if you want to make changes.
Update the Swift 3.1 - Thanks #Filip Radelic
if let path = Bundle.main.path(forResource: fileName, ofType: fileType) {
// use path
print(path)
}
Swift 4
if let path = Bundle.main.path(forResource: "imagename.png", ofType: nil) {
print("[check] FILE AVAILABLE \(path)")
}
You would use NSBundle to get your main bundle, then pathForResource(_ name: String?, ofType extension: String?) -> String?.

read file from res folder blackberry

I want to read file from "res" folder on blackberry. The file that i used is a file javascript.
I used this code InputStream in = classs.getResourceAsStream("file.js");. But i get "could not find this path" and I use also
String srcFile = "/res/ressourcesWeb/file.js";
FileConnection srcConn = (FileConnection) Connector.open(srcFile, Connector.READ);
InputStream in = srcConn.openInputStream();
but i got an exception.
Can any one help me to read the file and give me the right path that should I use?
Your res folder has to be inside src folder to be accessed from your code.
src folder is the root folder of your project package. And all folders outside of src folder are invisible for the code at runtime.
Check this post for more details: Blackberry runtime error: FRIDG: could not find img/logo.png
There's file location principle described.
You actually do not need to put your resources under the src folder for them to be accessible from your code.
That is one way to do it, but I don't think it's the best way. Files under the src folder should really be source code, not images, or other resources. For JavaScript resources, it's debatable whether those should be under src or not. Most projects I've seen have used the src folder for only Java source code.
In any case, if you would like to keep your file (or other resources, like images) outside the src folder, you can do so. The BlackBerry plugin for Eclipse actually sets it up like this by default, when you create a new project. There is a res folder at the top level, next to (not under) src.
If you have
src\
src\com\mycompany\myapp\
res\
res\resourcesWeb\
res\resourcesWeb\file.js
Then, you can open the file like this:
String jsPath = "/resourcesWeb/file.js";
InputStream input = getClass().getResourceAsStream(jsPath);
byte [] content = IOUtilities.streamToBytes(input);
String contentAsString = new String(content);
P.S. You also can probably do this:
String jsPath = "/file.js";
InputStream input = getClass().getResourceAsStream(jsPath);
and not specify the path to the resource. Obviously, this will only work if there are no naming conflicts in your resource folders (e.g. you don't have /res/resourcesWeb/file.js and also /res/otherPath/file.js)

Resources