Search code examples
iosswift3nsarrayplist

How do I read in one entry from a plist array


I am currently reading in an array from a plist using this code

let filePath = Bundle.main.path(forResource: "levelList", ofType: "plist")


if let path = filePath {
    devices = NSArray(contentsOfFile: path) as! [AnyObject]
}

let device1 = devices[0] as! [String: AnyObject]
let device2 = devices[1] as! [String: AnyObject]

If I know before I read from the file that I am only interested in device 1 is there a way to selectively only read in that part of the array?


Solution

  • If you want to selectively read your plist file only the first device1 value, you should read the file line by line by NSFileHandle. However, you have to decide your terminate point while reading line by line, e.g. when you read to the first </device> tag.

    You could check this link out as your reference How to read data from NSFileHandle line by line?

    Hope this helps you.