How to get different output from function using if Bool.random() within generic struct in Swift?
Solution 1:
The reason is the lines where you say:
if Bool.random() {
coolIn = ColdAirIntake(car: Azda()) // coolIn = ColdAirIntake(car: coolIn)
}
The second line where you set coolIn has no effect on the underlying options property of the Azda struct you passed in. I was able to rectify the problem by using the random boolean in the constructor of Azda:
struct Azda: Car {
var kind = "8 MPS"
var options: String {
if(Bool.random()){
return "Azda Option A"
}
else{
return "Azda Option B"
}
}
}
Which outputs a random result each time. There are many solutions to this problem but the bottom line is that fundamentally your call to Boolean.random() does not do anything because it does not alter the underlying default value of the Azda instance you are using.