swift - Enum Case not found in type -


 enum operator: character {     case substract = "-"     case add = "+"     case multiply = "*"     case divide = "/" } 

i have enum above , function declared below checks if have valid operator. e.g. isoperator("+")

func isoperator(_ symbol: character)-> operator? {                 let op = operator(rawvalue: symbol)                switch op {             case .substract, .add, .multiply, .divide:                 return op             default:                 return nil         }     } 

what compiler returns here "enum case not found in type" means cases defined in switch statement (.add .. etc) not available in operator type. why compiler not able find case since op operator type swift inver types atuomatically?

yes, because let op = operator(rawvalue: symbol) optional , in switch case matching exact values. can apply optional in case while comparing. below.

func isoperator(_ symbol: character)-> operator? {     let op = operator(rawvalue: symbol)     switch op {     case .substract?, .add?, .multiply?, .divide?:         return op     default:         return nil     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -