swift - Universal class for Mac OS and iOS -
this question has answer here:
i want create class mac os- , ios-app. unfortunately, can't use nscolor ios , no uicolor mac os.
actually have following code:
#if os(ios) func myfunc(color: uicolor?) { self.myfuncx(color) } #elseif os(osx) func myfunc(color: nscolor?) { self.myfuncx(color) } #endif private func myfuncx(color: anyobject?) { #if os(ios) mycolor = color as! uicolor #elseif os(osx) mycolor = color as! nscolor #endif }
is there better way?
you should able use typealias
color class, this:
#if os(ios) typealias xcolor = uicolor #elseif os(osx) typealias xcolor = nscolor #endif func myfunc(color: xcolor?) { self.myfuncx(color) }
the idea limit conditional compile type definition xcolor
, , using type alias in place of uicolor
or nscolor
, required on particular system.
Comments
Post a Comment