I created an app that includes an Employee Directory in a tableview. The app is working great using a plist file in my project to create an array of dictionaries, but the file changes often as employees come and go, so I really need to make it external to the Xcode project. I'm trying to do it using URL and NSURL, but having a problem getting this to work and need a fresh set of eyes. What is the best way to accomplish assuming the file is located at www.abc.com/directory.plist?
I'm sorry, I'm pretty much a programming beginner, but am learning as fast as I can!
Here's my functioning code:
class TableViewController: UITableViewController {
var filePath: String?
var employees: [[String: String]] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
filePath = Bundle.main.path(forResource: "directory", ofType: "plist")
employees = NSArray(contentsOfFile: filePath!) as! [[String: String]]
for item in employees {
print(item["Name"] as Any)
print(item.count)
print(employees.count)
}
}
EDIT -
I replaced NSArray with PropertyListSerialization in my code. Still working on adding remote file load with URLSession.
if let filePath = Bundle.main.url(forResource: "directory", withExtension: "plist") {
do {
let data = try Data(contentsOf:filePath)
employees = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [[String : String]]
} catch {
print("Error loading file.")
}
}