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

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -