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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -