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
Post a Comment