swift - NSMutableDictionary path "unexpectedly found nil while unwrapping an Optional value" -


i have 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 gets last line crashes , gives me ol'

fatal error: unexpectedly found nil while unwrapping optional value

the variable filename declared var filename: string!

i'm unable write path well. doing wrong?

another potential issue in addition gnasher729's suggestion contentsoffile initializer nsdictionaries , subclasses:

return value:

an initialized dictionary—which might different original receiver—that contains dictionary @ path, or nil if there file error or if contents of file invalid representation of dictionary.

so if there issue dictionary, when force unwrap in line

var dictionary = nsmutabledictionary(contentsoffile: imagespath)! 

it crash.


Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -