ios - Dynamically (generic) inherit from <T> in swift -


i didn't find solution , there's chance it's not possible i'll give try (if it's not possible i'd happy small explanation why).

i'm trying create class in swift, let's call foo, , want foochild inherit foo. far, no problems. thing is, want foo dynamically inherit "any class", maybe generic type.

something like

class foo<t> : <t>{  }  class foochild : foo<nsobject> {  }  class foochild2 : foo<uiview> {  } 

i want both foochild , foochild2 inherit foo, want foo inherit once nsobject, , once uiview (used random classes example).

is possible? in objective-c code i'll bridge somehow.

swift 1.2

no, not possible.

what can like:

protocol foo { }  class foochild1: nsobject, foo { }  class foochild2: uiview, foo { } 

swift 2

yes, possible.

non-generic classes may inherit generic classes. (15520519)

see: xcode 7 beta release notes, chapter "new in swift 2.0 , objective-c", section "swift language features"

e.g. following possible:

import uikit  class foo<t> { }  class foochild: foo<nsobject> { }  class foochild2: foo<uiview> { }  let foochild = foochild()  let foochild2 = foochild2() 

also, in swift 2 if need protocol foo swift 1.2 example provide default behaviour, can use default protocol implementations.

you can use protocol extensions provide default implementation method or property requirement of protocol. if conforming type provides own implementation of required method or property, implementation used instead of 1 provided extension.

see: the swift programming language, chapter "protocols", section "providing default implementations"


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -