ios - limit input options in init() in swift -
i'm making simple dice class in swift.
i dice initializer called desired amount of eyes/sides dice should have. have 2 variables set min , max of number of sides should able give dice upon init...
however, i'm not quite sure of how make init fail if dice being initialized number outside of range, when can't make sue of try/catch in swift.
my code follows:
class dice : skspritenode { let sides : uint32 var score : int init(sides : int){ let min_sides = 2 let max_sides = 6 self.sides = uint32(sides) self.score = 1 let imagename = "1.png" let cardtexture = sktexture(imagenamed: imagename) super.init(texture: cardtexture, color: nil, size: cgsize(width: 100, height: 100)) userinteractionenabled = true }
use failable initializer instead. can return nil
value if condition doesnt satisfied
init?(sides : int){ if sides > max_sides{ return nil } }
Comments
Post a Comment