Reputation: 2863
I am try to set the Enum value to a variable , but the value of variable didn't change when I get it back.
The code is like the following:
CouchbaseEnum.swift save the enum value.
class CouchbaseEnum{
enum ReplyStatus{
case NONE
case LOGIN_SUCCESS
case LOGIN_ERROR
case LOGIN_RETRY
}
}
Couchbase.swift define the function for set
and get
the value.
class Couchbase {
var STATUS = CouchbaseEnum.ReplyStatus.NONE;
func setReplyStatus(status: CouchbaseEnum.ReplyStatus){
STATUS = status;
}
func getReplyStatus() -> (CouchbaseEnum.ReplyStatus){
return STATUS;
}
}
ViewControler.swift call the function and set the enum value to variable.
@IBAction func login(sender: AnyObject) {
Couchbase().setReplyStatus(CouchbaseEnum.ReplyStatus.LOGIN_SUCCESS);
if(Couchbase().getReplyStatus() == CouchbaseEnum.ReplyStatus.LOGIN_SUCCESS){
println("login---LOGIN_SUCCESS");
}else if(Couchbase().getReplyStatus() == CouchbaseEnum.ReplyStatus.NONE){
println("login---NONE");
}
}
When I click the button , it will call setReplyStatus
and send CouchbaseEnum.ReplyStatus.LOGIN_SUCCESS
.
But it always show login---NONE
, why the value didn't change when I call Couchbase().setReplyStatus(CouchbaseEnum.ReplyStatus.LOGIN_SUCCESS);
in ViewController.swift
?
Thanks in advance.
Upvotes: 0
Views: 162
Reputation: 24572
STATUS
is an instance variable of the class Couchbase
.
Every time you call Couchbase()
, you are creating a new instance of the class Couchbase
and each time a new instance is created the STATUS
gets set to CouchbaseEnum.ReplyStatus.NONE
. Instead use a variable like the code below.
var cbase = Couchbase()
cbase.setReplyStatus(CouchbaseEnum.ReplyStatus.LOGIN_SUCCESS);
if(cbase.getReplyStatus() == CouchbaseEnum.ReplyStatus.LOGIN_SUCCESS){
println("login---LOGIN_SUCCESS");
}
else if(Couchbase().getReplyStatus() == CouchbaseEnum.ReplyStatus.NONE){
println("login---NONE");
}
Upvotes: 1