)
I have updated a "Workout Object" to have both a minimum and maximum number of reps.
When I've been hardcoding the lower bound in a playground, I've been using :
let numberOfExercises = Int(arc4random_uniform(4) + 3)
When I try to use variables in a function/with a class object I get an error of "+'is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics" e.g. here ...
class ExerciseGeneratorObject: Object {
@objc dynamic var name = ""
@objc dynamic var minReps = 0
@objc dynamic var maxReps = 0
convenience init(name: String, minReps: Int, maxReps: Int) {
self.init()
self.name = name
self.minReps = minReps
self.maxReps = maxReps
}
func generateExercise() -> WorkoutExercise {
return WorkoutExercise(
name: name,
//get error on this line...
reps: Int(arc4random_uniform(UInt32(maxReps))+minReps)
)
}
}
There is an answer here + is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics but that method is already using so don't see how it's applicable here.
Also here '+' is deprecated: Mixed-type addition is deprecated in Swift 3.1 but again think this is a different problem
'+' is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics.
Example:
let a: UInt32 = 4
let b = 3
let result = a + b //error
Basically means you can't add mixed-types.
In your case when you do arc4random_uniform(UInt32(maxReps)) + minReps
, arc4random_uniform()
returns a UInt32
which cannot be added to minReps
because that's an Int
.
Update your parenthesis:
let numberOfExercises = Int(arc4random_uniform(UInt32(maxReps))) + minReps
Here Int(arc4random_uniform(UInt32(maxReps)))
gives an Int
that we can add to the minReps
Int
.
BTW, the following works out-of-the-box:
let numberOfExercises = Int(arc4random_uniform(4) + 3)
Because of Swift's automatic type inference. Basically it just went ahead with UInt32
without bothering you. That is... until you give it explicit mixed types.