I need to be able to reference a file which is stored in my Xcode project in the following way:
I want to be able to use files which I have stored inside of the "data" folder.
How do I reference it to be able to read its contents? What is it's directory path?
I just figured it out. The apple swift documentation and other developer references are really unclear about it.
The way you would reference the "data" folder so as to scan the entire contents is by writing the following:
let path = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("data")
var error: NSError?
let filesInDirectory: [String]! = fileManager.contentsOfDirectoryAtPath(path!, error: &error) as? [String]
This will return the contents of the files in the "data" folder as an array of filenames.
Hope this helps :)
Relative to swift 3
let bundleURL = Bundle.main.bundleURL
let dataFolderURL = bundleURL.appendingPathComponent("data")
let fileURL = dataFolderURL.appendingPathComponent("file.txt")
print(fileURL.path)
print(FileManager.default.fileExists(atPath: fileURL.path))
let bundleURL = NSBundle.mainBundle().bundleURL
let dataFolderURL = bundleURL.URLByAppendingPathComponent("data")
let fileURL = dataFolderURL.URLByAppendingPathComponent("fileName.txt")
Related
I'm making an app with the folder like image below:
As you can see in this picture, Stickers folder have 2 sub folder "1" and "2". In side them is bunch of icon and I wanna load it in a collection view with each sub folder is a package of different sticker. For more details, please see this picture:
So, how can I get it in my project folder? I've read about access document directory but seem like it not solve my problem.
Please help me. Thanks in advance.
Look at Filemanagers API. It offers all you need.
Example to get content at path:
let pathToDir1 = Bundle.main.resourcePath! + "/Stickers/1";
let fileManager = FileManager.default
let contentOfDir1 = try! fileManager.contentsOfDirectory(atPath: pathToDir1)
Example to iterate over:
let docsPath = Bundle.main.resourcePath!
let enumerator = fileManager.enumerator(atPath:docsPath)
let images = [UIImage]()
while let path = enumerator?.nextObject() as! String {
let contentOfCurDir = try! fileManager.contentsOfDirectory(atPath: path)
// do whatever you need.
}
For details see Apples documentation and sample code.
If you're looking to access an image within your project folder you simply need to use the image name. You don't need to define the whole path.
Objective-C
UIImage *img = [UIImage imageWithName:#"IMAGE_NAME"];
swift
var img = UIImage(named:"IMAGE_NAME")
I am using an AVQueuePlayer to play local audio files which I have added to my Xcode project by dragging and dropping the .mp3 files into my folder in the project navigator pane. I then use this code to search thru my files and extract the files that I want, which I then use to fill a table view and to create AVPlayerItems for to play in my AVQueuePlayer.
My code works fine when I run the app on simulator but when i run the app on my iPhone, an error occurs and returns
fatal error: unexpectedly found nil while unwrapping an Optional value
Here is the code causing the issue...
var songNameArray: Array<String> = [String]()
let fileManager = NSFileManager.defaultManager()
let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath("/Users/UsersName/Desktop/Xcode Projects Folder/LocalAudioFilePlayer/LocalAudioFilePlayer")!
while let element = enumerator.nextObject() as? String {
if element.hasSuffix("mp3") {
songNameArray.append(element)
print(element)
}
}
Are the files not being properly copied into the bundle which is then deployed to the iPhone? If so, what is the proper way to add the audio files?
I also tried this...
var songNameArray: Array<String> = [String]()
let path = String(NSBundle.mainBundle().resourcePath)
let songFiles = try! NSFileManager.defaultManager().contentsOfDirectoryAtPath(path)
for item in songFiles {
if item.hasSuffix("mp3"){
songNameArray.append(item)
print("appended to songNameArray \(item)")
}
}
But I get the error
The folder “LocalAudioFilePlayer.app")” doesn’t exist
Any help is greatly appreciated
let path = String(NSBundle.mainBundle().resourcePath)
This line is not doing what you might think it does. NSBundle.mainBundle().resourcePath returns String?, which is an optional type. When you create a string from that by wrapping String() around it, it doesn't unwrap the optional, it creates a string describing the optional, which looks like this:
Optional("/Users/markbes/Library/Developer/CoreSimulator/Devices/18D62628-5F8A-4277-9045-C6DE740078CA/data/Containers/Bundle/Application/3DDC064C-0DD1-4BE9-8EA4-C151B65ED1E1/Resources.app")
What you want there is something more like
var path = NSBundle.mainBundle().resourcePath!
That unwraps the String? type, and gives you a string (or throws an error).
Did you select "Copy files if needed" then dragged mp3s into Project?
Try to load files as following:
let path = NSBundle.mainBundle().pathForResource("filename", ofType: "ext")
let url = NSURL.fileURLWithPath(path!)
var audioPlayer: AVAudioPlayer?
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
} catch {
print("Unable to load file")
}
Note: use only filename here, not the whole path.
It sounds like you haven't added your sound file to your target in the Xcode project. If they don't "belong" to a target, they won't get copied into the "resources" folder during building.
Select the sound files you want to use in the project navigator, and check the file inspector (view->Utilities->File Inspector, and make sure that the appropriate target is selected under "target membership".
edit: this is definitely not the problem in this case. Leaving this answer, as it may be useful for others running into a similar issue.
I'm working on an custom emoji keyboard in Swift and I'm having trouble finding and counting the images in a folder reference called "emojis".
EDIT: To clarify my issue is that let contents always end up as nil.
The structure from the location of the .xcodeproj file looks like this:
EmojiBoard/emojis/emoji-0.png and so on.
I've been trying to use the NSFileManager with no luck.
let fileManager = NSFileManager.defaultManager()
let contents = fileManager.contentsOfDirectoryAtPath("emojis", error: &error)
println(contents)
This prints nil. I've also tried "EmojiBoard/emojis" and "/EmojiBoard/emojis".
I need this to determine how many images there are and loop them all out without having to resort to an insane switch statement or something like that.
Thank you!
P.S. Please note that I'm coding in Swift, not Objective C. I'm not proficient enough to convert C programming to swift I'm afraid. D.S.
if you created folder reference when adding the folder to your project use it like this (emojis folder icon is a blue folder):
let resourceURL = Bundle.main.resourceURL!.appendingPathComponent("emojis")
var resourcesContent: [URL] {
(try? FileManager.default.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil)) ?? []
}
let emojiCount = resourcesContent.count
print(emojiCount)
if you created groups when adding the folder to your project use it like this (emojis folder icon is a yellow folder):
let resourceURL = Bundle.main.resourceURL!
let resourcesContent = (try? FileManager.default.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil)) ?? []
let emojiCount = resourcesContent.filter { $0.lastPathComponent.hasPrefix("emoji-") }.count
print(emojiCount)
From the top of my head, without access to an IDE to test this code, I reckon something like this:
let fileManager = NSFileManager.defaultManager()
let contents = fileManager.contentsOfDirectoryAtPath(path, error: &error)
for var index = 0; index < contents.count; ++index {
println("File is \(contents[index])")
}
If you replace 'path' above with your documents directory, this code should loop through the whole folder and print out all files.
If you just want the count of items just do this:
println("count is \(contents.count)")
The problem (or at least a major part of the problem) is your path. You can't pass in a path that's just a filename. You need an absolute path to one of the sandboxed directories available to your app like the documents directory.
Your code might look like this:
let documentsDir = NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomainMask.UserDomainMask,
true)[0] as! NSString
let emojisPath = documentsDir.stringByAppendingPathCompnent("emojis")
let contents = fileManager.contentsOfDirectoryAtPath(emojisPath,
error: &error)
println(contents)
(That would work if your emojis folder is in your app's documents folder. If instead your emojis are in your app bundle (built into the app) you would need to use different code entirely (using NSBundle functions to get a path to the directory inside the bundle).
EDIT:
If you want to find files in your app's bundle use the NSBundle method resourcePath, and then append the folder name to the bundle's resourcePath using stringByAppendingPathCompnent, like the code above.
1.I creat a new playground,and add my 2.plist to the playground's package content folder.
2.I tried:
var myDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("2", ofType:"plist"){
println(path)// "/var/folders/....../2.plist"
myDict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject>
println(myDict)// nil
let url:NSURL! = NSBundle.mainBundle().URLForResource("2", withExtension:"plist")
myDict = NSDictionary(contentsOfURL: url)
println(myDict) //nil
var str = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)!
// ??? here display the String content of 2.plist
} else {
println("exist")
}
3.I have tried many ways, but could not read the plist to dictionary.Why???
Many thanks for you help.
I think you are adding your Plist file wrongly so follow this step:
File Inspector
In the XCode menu just select View -> Utilities -> Show File Inspector:
You’ll see a panel on the right hand side. The Playground Settings section will have your path to the Resource folder. Just click the little arrow, and open up the Resource folder!
Now just add your plist file in the Resources folder, and code away!
Here you can see you code is working perfectly after following this steps.
Here is my plist format for more information:
Reference from here
Hope this will help you.
There are some thing wrong with the 2.plist.
The content of 2.plist could not be read with NSDictionary,but with NSArray.
I've been trying to create a Today extension that needs access to a .plist file in the documents directory. I have setup the App group for both the app and the extension.
While I have seen examples for NSUserDefaults, I couldn't find anything for accessing files.
I tried accessing the file like this (which works in the app itself)
let paths = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as String
let filePath = "\(documentsDirectory)/config.plist"
if NSFileManager.defaultManager().fileExistsAtPath(filePath) // false
{
…
}
But I get the following error messages in the console:
Warning: CFFIXED_USER_HOME is not set! It should be set to the simulated home directory.
Failed to inherit CoreMedia permissions from 59919: (null)
The documentation says something about using NSFileCoordinator and NSFilePresenter. I couldn't find out how to use them though. I'm sure I'll need to tell them the app group identifier somewhere but I don't where.
I got it to work. Since I don't need to write the file from the extension, I don't think I need to use NSFileCoordinator. Turns out NSFileManager has a method containerURLForSecurityApplicationGroupIdentifier() for retrieving the container URL.
let manager = NSFileManager()
let containerURL = manager.containerURLForSecurityApplicationGroupIdentifier("group.MyGroupIdentifier")
let filePath = "\(containerURL.path)/config.plist"
let settings = NSDictionary(contentsOfFile: filePath)