Swift loop through array of dictionaries -


why getting error when looping through array of dictionaries ?

import uikit  let tableview = uitableview()  func meh() {     let product = buildcells()     (identifier, nib) in product {         tableview.registernib(nib, forcellreuseidentifier: identifier)     } }  func buildcells() -> [[string: uinib]] {     var collector = [[string: uinib]]()     let identifier = "identifier"     let nib = uinib(nibname: "tableviewcell", bundle: nil)     let asdf = [identifier: nib];     collector.append(asdf)     return collector } 

the forin loop in meh() method produces following error:

'dictionary' not convertible '([[string : uinib]], [[string : uinib]])'

we can't iterate through keys , values of array of dictionaries. can iterate through dictionaries though. , each dictionary, can iterate through keys , values:

let product = buildcells() dict in product {     (identifier, nib) in dict {         tableview.registernib(nib, forcellreuseidentifier: identifier)     } } 

but think problem more nonsense going on in buildcells(). why returning array of dictionaries? plan have duplicate keys? don't think tableview let register multiple nibs same identifier.

why don't return dictionary?

func buildcells() -> [string: uinib] {     var dict = [string: uinib]()     dict["identifier"] = uinib(nibname: "tableviewcell", bundle: nil)     // rinse & repeat of other id/nib combos no duplicate ids     return dict } 

now can iterate on key/value pairs in dictionary without outer loop:

for (identifier, nib) in dict {     tableview.registernib(nib, forcellreuseidentifier: identifier) } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -