Given that Swift does not have exception handling how do I communicate errors back to the caller when they pass an incorrect value for a property. Essentially, can properties have validation?
Here is an example. Consider numberOfShoes property below. It's computed
class Animal {
var name : String
var numberOfLegs : Int
var numberOfShoes : Int {
get {return numberOfLegs }
set {
if newValue < 0 {
// TODO: WHAT GOES HERE?
}
numberOfLegs = newValue
}
}
init(name : String, numberOfLegs : Int) {
self.name = name
self.numberOfLegs = numberOfLegs
}
}
We could (should maybe) also have a willSet / didSet observer on numberOfLegs but I don't see how validation can be done here either.
Are we required to stick with things like:
var cat = Animal("Cat", 4)
if !cat.setNumberOfLegs(3) {
// I guess that didn't work...
}
What is everyone else doing?