swift - Good programming style with optionals -
i want read enum parameter nsuserdefaults. in case there nothing set in userdefaults want default value. there better way (more elegant) did here "if let" statement? , singleton want access value in other parts of code using
if gamestate.learnlevel == level.low {..}
how that?
class gamestate { var learnlevel:level enum level:int { case low = 1,medium,high } class var sharedinstance: gamestate { return _somemanagersharedinstance } init(){ // load games state let learnlevel = level(rawvalue: nsuserdefaults.standarduserdefaults().integerforkey("learnlevel")) if let learnlevel1 = learnlevel {self.learnlevel = learnlevel1} else {self.learnlevel = level.low} }
use nil
coalescing operator ??
:
let learnlevel = level(rawvalue: nsuserdefaults.standarduserdefaults().integerforkey("learnlevel")) self.learnlevel = learnlevel ?? level.low
Comments
Post a Comment