swift - A way to inherit from multiple classes -


i have 2 classes want use in new class. first 1 implements swipe delete , second enables long press gesture:

class deleteitem: uitableviewcell { }  class opendetail: uitableviewcell { } 

since swift doesn't allow class inherit multiple classes following example won't work:

class itemviewcell: deleteitem, opendetail { } 

so in order create itemviewcell , having both options, i'll have have 1 of classes inherit each other:

class deleteitem: uitableviewcell { }  class opendetail: deleteitem { }  class itemviewcell: opendetail { } 

the problem is, if want long press gesture i'll have create new class without inheriting deleteitem. there better way of doing this?

this perfect case using protocols , protocol extension. swift protocol interface in java example. protocol can define set of functions has implemented entities want conform protocol, protocol can define properties has present in these entities too. example:

protocol itemdeleter {   var deletedcount: int {get set}   func deleteitem(item: itemtype) } 

the problem is, each entity have provide own implementation of func deleteitem(item: itemtype) if multiple entities share same logic of deleting item, protocol extension comes in handy. example:

extension itemdeleter {   func deleteitem(item: itemtype) {     // logic needed delete item      // maybe incremented deletedcount     deletedcount++   } } 

then make itemviewcell conform itemdeleter protocol, in case need make sure itemviewcell has property deletedcount: int. not need provide implementation func deleteitem(item: itemtype) protocol provides default implementation function, can override in class, , new implementation used. same applies detailopener protocol.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -