I have a simple:
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let dataPath = documentsPath.stringByAppendingPathComponent("Images")
let imagesPath = dataPath.stringByAppendingPathComponent(fileName)
var dictionary = NSMutableDictionary(contentsOfFile: imagesPath)!
And after it gets to the last line it crashes and gives me the ol'
fatal error: unexpectedly found nil while unwrapping an Optional value
The variable fileName is declared as var fileName: String!
I'm unable to write to the path as well. What am I doing wrong?
Another potential issue in addition to gnasher729's suggestion is that the contentsOfFile initializer for NSDictionaries and their subclasses:
Return Value:
An initialized dictionary—which might be different than the original receiver—that contains the dictionary at path, or nil if there is a file error or if the contents of the file are an invalid representation of a dictionary.
So if there is an issue with that dictionary, when you force unwrap it in this line
var dictionary = NSMutableDictionary(contentsOfFile: imagesPath)!
it will crash.
Declaring a fileName as String! means it might not contain a String, but you are quite sure it does, and you accept that your app crashes if you use the variable fileName and it doesn't contain a String. That seems to be the case here.
As others have noted, the problem is likely to be one of the force unwrapped optionals that you are using. There's a reason why a ! is sometimes referred to as a Bang!. They have a tendency to blow up :) Unwrapping things one at a time and using some print statements will help you find out what went wrong:
if let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as? String,
let filePath = filePath {
println("Determined that both documentsPath and filePath are not nil.")
let dataPath = documentsPath.stringByAppendingPathComponent("Images")
let imagesPath = dataPath.stringByAppendingPathComponent(fileName)
if let dictionary = NSMutableDictionary(contentsOfFile: imagesPath) {
println("Determined that dictionary initialized correctly.")
// do what you want with dictionary in here. If it is nil
// you will never make it this far.
}
}
Related
I guess this is basic Swift, so I'm a bit embarrassed to ask:
In my app, I download a plist file from a server as such:
Alamofire.download(url, to: destination).response { response in
if let url = response.destinationURL {
self.holidays = NSDictionary(contentsOf: url)!
}
}
The file is a valid file, and it is succesfully dowloaded and sitting physically in my Documents folder.
However, the app crashes on
self.holidays = NSDictionary(contentsOf: url)!
saying
Fatal error: Unexpectedly found nil while unwrapping an Optional value
What gives?
Your NSDictionary is failing to initialize, so when you try to force unwrap it (with the exclamation mark) it fails and crashes.
Try something like this:
if let dictionary = NSDictionary(contentsOf: url) {
self.holidays = dictionary
}
Alternatively you can use the guard statement:
guard let dictionary = NSDictionary(contentsOf: url) else {
print("NSDictionary failed to initialise")
return
}
self.holidays = dictionary
I am working on a decibel measuring app for my class, and I have been stumped by an error that keeps on coming up: 'ambiguous use of appendingPathComponent'. Here is where the problem is occurring:
//set up the URL for the audio file
var documents: AnyObject = NSSearchPathForDirectoriesInDomains( FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as AnyObject
var str = documents.appendingPathComponent("recordTest.caf")
var url = NSURL.fileURL(withPath: str as String)
The error is happening here:
var str = documents.appendingPathComponent("recordTest.caf")
I can't seem to get this resolved.
Help,
Paul
Why are you casting documents to AnyObject? Get rid of that.
But that then brings up a new issue since appendingPathComponent is a method of NSString, NSURL, or URL. But documents is a String.
And why use NSURL instead of URL?
Since your goal is to get a URL, use the more direct approach with FileManager:
let docURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = docURL.appendingPathComponent("recordTest.caf")
I have a variable copiedURL which contains a URL string, I am trying to use that string variable to display an image programmatically
let url = NSURL(string: copiedURL)
let data = NSData(contentsOfURL: url!)
let image2 = UIImage(data: data!)
When I build and run I get a EXC_BAD_INSTRUCTION error fatal error: unexpectedly found nil while unwrapping an Optional value
I am certain that copiedURL has a value because I used print() to debug and everything printed just fine.
Also, I also tried:
let url = NSURL(string: "\(copiedURL)")
but that didn't solve it.
NSURL(string:) is a failable initializer. In the case that the URL isn't valid, it will not initialize a new NSURL instance, but will instead return nil.
Check that your copiedURL is a valid URL.
I'm really confused on this one. There are dozens of questions around the web asking "How do I get info from my plist file in Swift?" and the same answer is posted everywhere:
let path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist")
However, this line produces always produces nil for me. I have replaced Config with other components found in the default plist file, but get nil as well.
I am trying to access my custom ProductIdentifiers Array like so:
let url = NSBundle.mainBundle().URLForResource("ProductIdentifiers", withExtension: "plist")!
var productArray = NSArray(contentsOfURL: url) as! [[String:AnyObject!]]
I get a crash stating fatal error: unexpectedly found nil while unwrapping an Optional value on productArray. I have also tried this with other default plist values in place of ProductIdentifiers.
Does anyone know why this is not working for me even though there are so many posts around of people using this successfully?
I've never heard of the OP's approach working before. Instead, you should open the Info.plist file itself, then extract values from it, like so:
Swift 3.0+
func getInfoDictionary() -> [String: AnyObject]? {
guard let infoDictPath = Bundle.main.path(forResource: "Info", ofType: "plist") else { return nil }
return NSDictionary(contentsOfFile: infoDictPath) as? [String : AnyObject]
}
let productIdentifiers = getInfoDictionary()?["ProductIdentifiers"]
Swift 2.0
func getInfoDictionary() -> NSDictionary? {
guard let infoDictPath = NSBundle.mainBundle().pathForResource("Info", ofType: "plist") else { return nil }
return NSDictionary(contentsOfFile: infoDictPath)
}
let productIdentifiers = getInfoDictionary()?["ProductIdentifiers"]
Resource represents the file name of the plist rather than its contents.
The root object of the plist is probably a dictionary.
Replace MyPlist with the real file name.
This code prints the contents of the plist
if let url = NSBundle.mainBundle().URLForResource("MyPlist", withExtension: "plist"),
root = NSDictionary(contentsOfURL: url) as? [String:AnyObject]
{
print(root)
} else {
print("Either the file does not exist or the root object is an array")
}
According to How to find NSDocumentDirectory in swift? I wrote the next code:
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let files = NSFileManager().enumeratorAtPath(documentsPath)
var myString:String = "/"
while let file: AnyObject = files?.nextObject() {
myString += (file as String + "/")
}
println(myString)
But, actually the problem is that, when I debug the program, documentsPath equals to (String) "", after executing NSSearchPathForDirectoriesInDomains command. Besides, I'm trying to do this not in MainViewController, I'm trying to execute this command in TableViewController.
However, it works in playground;
It doesn't work for any project that I create
let docdir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String + "Test.txt"
returns the correct path, wtf?
I looked in the documentation for NSSearchPathForDirectoriesInDomains and it says,
You should consider using the NSFileManager methods
URLsForDirectory:inDomains: and
URLForDirectory:inDomain:appropriateForURL:create:error:. which return
URLs, which are the preferred format.
And the following works perfectly:
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
println(urls[0])
That said, your code is failing because the items in the array returned by NSSearchPathForDirectoriesInDomains are not Strings. If you want to keep your original code, just change as String to as NSString