How do I access an enum from another class in Swift? -


say have following example:

class classone {     enum color {         case red         case blue     }      func givecolor() -> color {         return .red     } }  class classtwo {     let classone = classone()     var color: color = classone.givecolor() } 

the compiler complains doesn't know color in classtwo. how best handle this?

your color enumeration nested type -- you'll access classone.color. moreover, can't assign 1 property in declaration that. leave unassigned , in init():

class classone {     enum color {         case red         case blue     }      func givecolor() -> color {         return .red     } }  class classtwo {     let classone = classone()     var color: classone.color      init() {         self.color = self.classone.givecolor()     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

c# - How do I get the Nth largest element from a list with duplicates, using LINQ? -

jsp - "Sending a redirect is forbidden after the response has been committed" in sendRedirect -