ios - Swift: using plist - error: value of optional type 'String?' not unwrapped -
i beginner in swift , got stuck in question:
given resource named "crazyinformation.plist" located in main application directory, first retrieve path resource , assign string constant named plistpath. once have plistpath, create instance of nsdictionary containing contents of plist.
i trying this:
import foundation // add code below var plistpath = nsbundle.mainbundle().pathforresource("crazyinformation ",oftype: "plist") var information = nsarray(contentsoffile: plistpath)
however, got error:
swift_lint.swift:7:43: error: value of optional type 'string?' not unwrapped; did mean use '!' or '?'? var information = nsarray(contentsoffile: plistpath)
how unwrapped this?
the return type of pathforresource
string?
i.e. if there no file of name in bundle, might return nil.
a way of writing code be
if let plistpath = nsbundle.mainbundle().pathforresource("crazyinformation", oftype: "plist") { var information = nsarray(contentsoffile: plistpath) }
this ensure code inside if condition executed if pathforresource
returns not-nil value
Comments
Post a Comment