Vote count:
0
I'm trying to learn Swift and I'm stuck to a very simple thing. How to write a function that returns an enum. The following code doesn't compile:
import Foundation
enum MyResponse {
case Even (String, String)
case Odd (String)
}
func checkNumber(number : Int) -> MyResponse // <---- Error message
{
if (number % 2 == 0) {
return MyResponse.Even(String(number), " is even")
}
return MyResponse.Odd("odd")
}
let v1 = checkNumber(1)
switch v1 {
case .Even(arg1, arg2):
println("\(arg1) --> \(arg2)")
case .Odd(arg):
println("\(arg)")
}
I get the error message "Use of undeclared type".
What am I doing wrong? What is the correct way to return enum value from a function.
asked 45 secs ago
Swift function returning an enum
Aucun commentaire:
Enregistrer un commentaire