How to Read a Property List from Data in Swift 3

How can I read a property list from data in Swift 3

Dictionary is a generic type which needs type information for the keys and the values.

Use Dictionary<String,Any> or shorter [String:Any]:

let datasourceDictionary = try! PropertyListSerialization.propertyList(from:data!, format: nil) as! [String:Any]

The options parameter can be omitted.

How to read from a plist with Swift 3 iOS app

Same way you have done in Swift 2.3 or lower just syntax is changed.

if let path = Bundle.main.path(forResource: "fileName", ofType: "plist") {

//If your plist contain root as Array
if let array = NSArray(contentsOfFile: path) as? [[String: Any]] {

}

////If your plist contain root as Dictionary
if let dic = NSDictionary(contentsOfFile: path) as? [String: Any] {

}
}

Note: In Swift it is better to use Swift's generic type Array and Dictionary instead of NSArray and NSDictionary.

Edit: Instead of NSArray(contentsOfFile: path) and NSDictionary(contentsOfFile:) we can also use PropertyListSerialization.propertyList(from:) to read data from plist file.

if let fileUrl = Bundle.main.url(forResource: "fileName", withExtension: "plist"),
let data = try? Data(contentsOf: fileUrl) {
if let result = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [[String: Any]] { // [String: Any] which ever it is
print(result)
}
}

Read plist in swift 3 playground

To finally get the program working I used.

devices = NSArray(contentsOfFile: path) as! [AnyObject]

The issue with playground continuously running was solved by going to activity monitor and force quitting the process named com.apple.coresimulator which was identified as not responding. After doing this the playground ran instantly.

How do I read a value of a property list file key into a string for iphone app using Xcode

Load the .plist into a NSDictionary like:

NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

Loop through the NSDictionary using something like:

for (id key in dictionary) {
NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}

Swift: How to read multiple array within array in plist?

The recommended way in Swift to read a property list file is PropertyListSerialization. It avoids the usage of Foundation NSArray.

The code prints the section numbers and all items in the inner array.

If the property list is is supposed to populate a table view I recommend to declare the inner array as dictionary.

let url = Bundle.main.url(forResource:"items", withExtension: "plist")!
do {
let data = try Data(contentsOf:url)
let sections = try PropertyListSerialization.propertyList(from: data, format: nil) as! [[Any]]

for (index, section) in sections.enumerated() {
print("section ", index)
for item in section {
print(item)
}
}
} catch {
print("This error must never occur", error)
}

Swift unable to locate and read property list (.plist) file

Normally, this is what you do:

In the Project Navigator (on the left), select the plist file. In the File Inspector (on the right), examine the target membership settings. You will probably find that the app target is not checked. This means that the plist file is not being copied into the app bundle when you build it. Check the app target, and all will be well.

But you can't do that, because this is a command line program. It is an executable and no more. There is no bundle to copy into. What you're trying to do is metaphysically impossible. /p>

How to Read Plist without using NSDictionary in Swift?

The native Swift way is to use PropertyListSerialization

if let url = Bundle.main.url(forResource:"Config", withExtension: "plist") {
do {
let data = try Data(contentsOf:url)
let swiftDictionary = try PropertyListSerialization.propertyList(from: data, format: nil) as! [String:Any]
// do something with the dictionary
} catch {
print(error)
}
}

You can also use NSDictionary(contentsOf: with a type cast:

if let url = Bundle.main.url(forResource:"Config", withExtension: "plist"),
let myDict = NSDictionary(contentsOf: url) as? [String:Any] {
print(myDict)
}

but you explicitly wrote: without using NSDictionary(contentsOf...

Basically don't use NSDictionary without casting in Swift, you are throwing away the important type information.



Meanwhile (Swift 4+) there is still more comfortable PropertyListDecoder which is able to decode Plist directly into a model.

How do I get a plist as a Dictionary in Swift?

In swift 3.0 Reading from Plist.

func readPropertyList() {
var propertyListFormat = PropertyListSerialization.PropertyListFormat.xml //Format of the Property List.
var plistData: [String: AnyObject] = [:] //Our data
let plistPath: String? = Bundle.main.path(forResource: "data", ofType: "plist")! //the path of the data
let plistXML = FileManager.default.contents(atPath: plistPath!)!
do {//convert the data to a dictionary and handle errors.
plistData = try PropertyListSerialization.propertyList(from: plistXML, options: .mutableContainersAndLeaves, format: &propertyListFormat) as! [String:AnyObject]

} catch {
print("Error reading plist: \(error), format: \(propertyListFormat)")
}
}

Read More
HOW TO USE PROPERTY LISTS (.PLIST) IN SWIFT.



Related Topics



Leave a reply



Submit