Search code examples
arraysswiftuitableviewdictionaryplist

i Want to read an Array of Dictionary with properties(btnImage+btnTitle) from Plist into TableViewCell


Created array of dictionary having buttons.

-created UITableviewcell.xib and .swift -created ib -linked ib -linked dataSource and Delegates need help further to read my plist data and show it into tableViewCell


Solution

  • Use Decodable, it's very simple

    struct Section : Decodable {
        let title, icon : String
    }
    

    If the file is in the bundle, read it as Data and decode it

    let url = Bundle.main.url(forResource: "sections", withExtension: "plist")!
    do {
        let data = try Data(contentsOf: url)
        let result = try PropertyListDecoder().decode([Section].self, from: data)
    } catch { print(error) }
    

    result will contain an array of Section items