Enum: toggle case for type variables

How come when I try to deploy and optionally that has a friendstate enum resource I get an error when I try to apply it in the switch statement below? An error has occurred

Enum case "added" was not found in type "friendState!"

(errors are repeated for all cases)

Is there a way to fix this error?

var usernameText : String!
var UID : Int!
var userDetails : UserState?
var buttonText : String{
    switch(userDetails!.state){
    case .added:
        return "remove"//error occurs
    case .incoming:
        return "accept"//error occurs
    case .outgoing:
        return "cancel"//error occurs
    }

}

func setup(){
    ActButton.setTitle(buttonText, forState: .Normal)
}


enum friendState : Int,Printable{

case incoming,added,outgoing

var description : String{
    switch(self){
    case .incoming:
        return "incoming"
    case .added:
        return "Friends"
    case .outgoing:
        return "outgoing"
    }
  }
}


class UserState : Printable , Hashable{
var uid : Int!
var username : String
var isFollowing : Bool
var state : friendState!
var RequestForUser : Request?

init( username : String, isFollowing : Bool, state : friendState, uid :Int){
    self.username = username
    self.isFollowing = isFollowing
    self.state = state
    self.uid = uid
}

var hashValue : Int {
    get{
        return uid
    }
}

var description : String{
    return "UserName: \(username) Following : \(isFollowing) Association: \(state) \n"
}

      

}

+3


source to share


1 answer


In function var buttonText : String

Just change the line:

switch(userDetails!.state)

      



from

switch(userDetails!.state!)

      

You need to expand userDetails as an optional state.

+2


source







All Articles