how to handle two different types in an Array in Swift for a UITableView -


we thinking of migrating app obj-c swift. 1 issue have uitableview in our obj-c code has objects either of type header or of type item. basically, resolves type has @ cellforrowatindexpath. swift arrays (to best of knowledge) can handle single type. given this, how handle 2 different types used in uitableview? wrapper object dataobj have nillable instance of each work?

here approach uses protocol unite 2 classes:

protocol tableitem {  }  class header: tableitem {     // header stuff }  class item: tableitem {     // item stuff }  // array can store objects implement tableitem let arr: [tableitem] = [header(), item()]  item in arr {     if item header {         println("it header")     } else if item item {         println("it item")     } } 

the advantage of on [anyobject] or nsmutablearray classes implement tableitem allowed in array, gain type safety.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -