How to access Plist file by Swift

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to access Plist file by Swift

Eddy Wong
Any one can help me about accessing plist file in Swift 2.0

I have use below code to access an Array type of plist file and it works fine but I use the same code to open Dictionary type of plist file. It returns 'nil'. Can anyone help me or point me out what is wrong with below code. I tried to cast the NSDictionary but the return is 'nil'

 var plistPathInDocument:String = String()

       

 let rootPath = NSSearchPathForDirectoriesInDomains(.DocumentationDirectory, .UserDomainMask, true)[0]

 let url = NSURL(string: rootPath)

 plistPathInDocument = (url?.URLByAppendingPathExtension("TestList.plist").absoluteString)!

        

 let resultDictionary = NSDictionary(contentsOfFile: plistPathInDocument) 

        

 print("\(resultDictionary)")

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/36c7ffea-f289-499a-880e-88a978401fa0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to access Plist file by Swift

Jens Alfke

On Oct 19, 2015, at 4:27 AM, Eddy Wong <[hidden email]> wrote:

 plistPathInDocument = (url?.URLByAppendingPathExtension("TestList.plist").absoluteString)!

        

 let resultDictionary = NSDictionary(contentsOfFile: plistPathInDocument) 



plistPathInDocument isn’t a path, it’s a URL string (it starts with “file:///…” instead of “/…”.)

You can call .path on the URL instead of .absoluteString to get the filesystem path, or even better, call NSDictionary(contentsOfURL:…) directly.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/315EC525-7DEC-4E65-9FF5-44EDA2237FA1%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.