枚举:切换类型变量的大小写

时间:2015-06-07 04:52:00

标签: swift enums

当我尝试打开包含enum friendstate的可选项时,为什么我尝试在下面的switch语句中应用它时会出现错误?提供的错误是

在“friendState!”类型中找不到“添加”的枚举案例

(错误复制所有情况)

有没有办法解决这个错误?

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"
}

}

1 个答案:

答案 0 :(得分:2)

在函数var buttonText : String

只需更改一行:

switch(userDetails!.state)

switch(userDetails!.state!)

您需要解包userDetails可选状态。