arrays - How to generic a function where params are different structs with different properties? -


please refer following code:

import uikit  struct item {     var brandid = 1     var name: string = "" } struct store {     var areaname = ""     var name: string = "" }  let itemarray = [item(brandid: 1, name: "item1"), item(brandid: 2, name: "item2"), item(brandid: 1, name: "item3") ]  let storearray = [store(areaname: "hk", name: "store1"), store(areaname: "bj", name: "store2"), store(areaname: "hk", name: "store3")]   var intkeys = [int]() var groupeditems = [[item]]()  var stringkeys = [string]() var groupedstores = [[store]]()  extension array {      func transto2d() -> [[element]] {         let grouped = [[element]]()          return grouped     }  }  itemarray.foreach { (item) in     let brandid = item.brandid     if !intkeys.contains(brandid) {         intkeys.append(brandid)         var newarray = [item]()         newarray.append(item)         groupeditems.append(newarray)     } else {         let index = intkeys.index(of: brandid)!         groupeditems[index].append(item)     } } 

my final goal using itemarray.transto2d() 2d array based on item's brandid, using storearray.transto2d() 2d array based on store's areaname. don't how generic function trans 1d array 2d array based on key?

i don't think can write generic extension array elements either of type item or store since both of them don't share relation write common generic method. can write extensions array elements of mentioned type. need conform both of structs equatable protocol.

struct item {     var brandid = 1     var name: string = "" }  extension item : equatable{     static func ==(lhs: item, rhs: item) -> bool{         return lhs.brandid == rhs.brandid     } }  struct store {     var areaname = ""     var name: string = "" }  extension store : equatable{     static func ==(lhs: store, rhs: store) -> bool{         return lhs.areaname == rhs.areaname     } }  extension array element == store{     func transform()->[[store]]{         var storearray = self         var groupedarray = [[store]]()         while storearray.count > 0{             if let firstelement = storearray.first{                 groupedarray.append(storearray.filter{$0.areaname == firstelement.areaname})                 storearray = storearray.filter{$0.areaname != firstelement.areaname}             }         }         return groupedarray     } }  extension array element == item{     func transform()->[[item]]{         var itemarray = self         var groupedarray = [[item]]()         while itemarray.count > 0{             if let firstelement = itemarray.first{                 groupedarray.append(itemarray.filter{$0.brandid == firstelement.brandid})                 itemarray = itemarray.filter{$0.brandid != firstelement.brandid}             }         }         return groupedarray     } } 

using transform function

let storearray = [store(areaname: "hk", name: "store1"), store(areaname: "bj", name: "store2"), store(areaname: "hk", name: "store3")] let itemarray = [item(brandid: 1, name: "item1"), item(brandid: 2, name: "item2"), item(brandid: 1, name: "item3") ] print(storearray.transform()) print(itemarray.transform()) 

this print output believe wanted.

[[store(areaname: "hk", name: "store1"), store(areaname: "hk", name: "store3")], [store(areaname: "bj", name: "store2")]] [[item(brandid: 1, name: "item1"), item(brandid: 1, name: "item3")], [item(brandid: 2, name: "item2")]] 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -