I have json file that I want to include in the test bundle so I can just read this json for my tests.
So I created a file called "test.json", it is in my Tests folder.
Now I want to read this file. I am reading it like this, the code here is in the test file in test bundle:
let bundle = Bundle(for: type(of: self))
var url = bundle.url(forResource: "test", withExtension: "json")
the url is nil every single time. I swear that file is there, if I open it in finder, it's under the same directory as the test-info.plist. I even tried the main bundle but it is still nil!!
Where is this file????
Try this:
let bundle = Bundle(for: YourTestClass.self)
let path = bundle.path(forResource: "test", ofType: "json")
let data = try Data(contentsOf: URL(fileURLWithPath: path))
Related
I am attempting to load local images from my local Bundle into my WkWebView but have not been successful.
This is my index.html:
<div>
<img src='./images/test.png'>
<p>Test Para</p>
</div>
My folder structure:
- web
-- images
-- test.png
-- index.html
- ViewController
Code in ViewController that loads the HTML
Attempt 1:
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
let htmlUrl = URL(fileURLWithPath: htmlPath!, isDirectory: true)
webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl)
Attempt 2:
let url = Bundle.main.url(forResource: "index", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
Attempt 3:
let htmlPath = Bundle.main.path(forResource: "index", ofType: "HTML")
let folderPath = Bundle.main.bundlePath
let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
do {
let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
webView.loadHTMLString(htmlString as String, baseURL: baseUrl)
} catch {
// catch error
}
The solutions attempted above were suggested from this post and this. All the above returns the following result:
The problem is that this statement is wrong:
My folder structure:
- web
-- images
-- test.png
-- index.html
That may be what the Project navigator looks like, but those yellow things in the Project navigator ("web", "images") are not folders. They are just organizational devices for the Project navigator itself (groups), to help you find your stuff. In the built app, your resources are being loaded flat into the app bundle, so that test.png and index.html are at the same level together, namely, the top level of the bundle.
If what you really wanted was in fact a "web" folder with that structure inside your app bundle, then you needed this to be a folder reference, which is a very different thing.
In my Tests directory in my swift project, I have a directory for test cases and a directory for test files.
+ Tests
+ UnitTest
+ MySwiftTests.swift
+ SourceFiles
+ file1.xml
I need to create a FileManager to load 'file1.xml' in my MySwiftTests. My question is how to specify the SearchPathDirectory and SearchPathDomainMask in the url of the FileManager which is relative the the test cases?
func url(for: FileManager.SearchPathDirectory, in: FileManager.SearchPathDomainMask, appropriateFor: URL?, create: Bool) -> URL
https://developer.apple.com/documentation/foundation/filemanager
let bundle = Bundle(for: type(of: self))
guard let path = bundle.path(forResource: "file1", ofType: "xml") else {
// File not found ... oops
return
}
// Now you can access the file using e.g. String(contentsOfFile:)
let string = try? String(contentsOfFile: path)
// or Data? using the FileManager
let data = FileManager.default.contents(atPath: path)
Make sure to add file1.xml to your Test Target
I would like to get the subfolders structure in the Resources folder of a Swift Playground.
I've tried using this approach with no luck
let docsPath = Bundle.main.resourcePath! + "/Resources"
let directoryContents = try? fileManager.contentsOfDirectory(atPath: docsPath)
This results in nil and I can't get the tree structure of the folders, is this possible?
Solved it!
let docsPath = Bundle.main.resourcePath!
let urls = Bundle.main.urls(forResourcesWithExtension: nil, subdirectory: "dataset/train")
I would recommend to allow NSBundle to find your resources exactly where they are, so instead of
let docsPath = Bundle.main.resourcePath! + "/Resources"
you can use somewhere inside your class (moreover, such class will be transferable to real project)
let docsPath = Bundle(for: type(of: self)).path(forResource: "dataset", ofType: nil)
and you get exact path.
Btw, it can be something like
/var/folders/gs/hkhcv3rx5wd5lqrfvh1ll6g40000gn/T/com.apple.dt.Xcode.pg/resources/620432D5-9F12-4FC8-B271-17153FC9D797/dataset
that is why it's better to avoid hardcoding.
I'm trying to read the data from a txt file on macOS. I use String(contentsOf:) and Bundle.main.path(forResource:) like I would on iOS. However, this doesn't work on macOS. I have tried several solutions from other posts but because of macOS they don't seem to work.
Thanks in advance.
Edit:
My code:
let path = Bundle.main.path(forResource: "file", ofType: "txt")! // no error
let url = URL(string: path)! // no error
let contents: String
do {
contents = try String(contentsOf: url)
} catch {
print(error) // Error Domain=NSCocoaErrorDomain Code=262 "The file couldn’t be opened because the specified URL type isn’t supported."
contents = "ERROR"
}
print(contents)
I can save the data to a folder on disk, but I want to ship this file with my app.
URL(string is the wrong API. It's only for URL strings starting with a scheme (http://, file://)
For file system paths you have to use URL(fileURLWithPath.
But in this situation there is a much better way
Replace
let path = Bundle.main.path(forResource: "file", ofType: "txt")!
let url = URL(string: path)! // no error
with
let url = Bundle.main.url(forResource: "file", withExtension: "txt")
I have a file stored in:
/var/mobile/Containers/Data/Application/083EA15E7/Documents/myFile.zip
It got there after I downloaded it from a server.
If I know the file name is myFile.zip, how can I find it with NSBundle?
Like this:
if let URL = NSBundle.mainBundle().URLForResource("myFile", withExtension: "zip") {
// do stuff
}
currently this returns false, not sure how I can specify the whole path. Any ideas?
This item is not in your bundle, an item in your bundle is something that you add before compiling, such as assets, fonts etc.
iOS provides each app with a sandbox. In that sandbox, the Documents folder exists. To access files from the Documents folder try this:
let documentsURL = NSURL(
fileURLWithPath: NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true).first!,
isDirectory: true
)
To get the file, you will need to get its path and append it to the documents path like so.
let URLToMyFile = documentsURL.URLByAppendingPathComponent("MyFile.zip")
To get the path as a string you can access the path property of the URL.
print(URLToMyPath.path!)
That will print out the path of your downloaded resource.
Tested on: xCode 8.3.2 & Swift 3.1
First drag your file (JPG, MP3, ZIP) inside your project folder and make sure Copy items if needed is checked and project/app is selected in Add to targets
Inside relevant ViewController
let fileName = "fileName"
let fileType = "fileType"
if let filePath = Bundle.main.path(forResource: fileName, ofType: fileType) {
print(filePath)
}
If you need to get the file URL you can use NSBundle method
if let fileURL = Bundle.main.url(forResource: fileName, withExtension: fileType) {
print(fileURL)
}
Also NSBundle method pathForResource has an initializer that you can specify in which directory your files are located like:
if let filePath = Bundle.main.path(forResource: fileName, ofType: fileType, inDirectory: "filesSubDirectory") {
print(filePath)
}
And for getting file URL:
if let fileURL = Bundle.main.url(forResource: fileName, withExtension: fileType, subdirectory: "filesSubDirectory") {
print(fileURL)
}
run time created file are stored in Document directory not in NSBundle. NSBundle stores the files like System file you put in your Xcode project while your developing
Here is the example
This code is fully tested on Swift 2.0
let file = "myFile.zip"
if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
//path will be stored here
let sPath = dir.stringByAppendingPathComponent(file);
print(sPath) // printing the file path
}