0

My code below gives a breakpoint error:

func doScore(num: Float, binaural: Bool, noise: Bool) -> Float {


if 50 ... 100 ~= num{

  let numDoubled = num + (Float(noise.intValue()!) * weighting)// <--- this is where I get my error 
  return numDoubled.rounded()


}

All I want to do is multiply the number I am putting into the function by the value of binaural or noise which are boolean values. To do this I am getting the Int value however I need it to be a float as 0 or 1 since the number I am putting in is a float. Why would this cause a crash? Thanks.

Luke Ireton
  • 479
  • 1
  • 3
  • 18

3 Answers3

3

By doing a simple workaround, you can fix it easily

let numDoubled = num + (noise ? weighting : 0.0)

Converting bool into int is here but in my solution, there's no need to do double job( convert to int and then again convert into float)

Updated as per vacawama comment

Updated answer from your comment:

let numDoubled = num + ( (noise && binaural) ? weighting : 0.0 ) )
Mani
  • 17,549
  • 13
  • 79
  • 100
  • 3
    I wouldn't call it a workaround since multiplying a float by boolean does not make any sense :) – Sulthan Apr 27 '18 at 09:48
  • I like this it works well, is there a way of multiplying by both NOISE and BINAURAL? My example is: let numOutput = num * (noise && binaural ? weighting : 0.0) So I basically want to multiply by both binaural and noise even if either of them is 0 or false in this case. :) In essence I am trying to get it to multiply by both binaural and noise and then the weighting. – Luke Ireton Apr 27 '18 at 12:56
  • @LukeIreton Updated my answer pls check. If you want multiply, replace the `+` with `*`. – Mani Apr 27 '18 at 13:02
  • If I answer with 100 and both binaural and noise = true then I just get 125 as my return num, that works great but I am basically trying to get it to where it will multiply the number I put in by binaural and noise and then the weighting. If I put in 100 and binaural and noise are true the output should be 150 if I put in 100 and only one of the bools is false it will only multiply it by 1 and then by the weighting giving the output of 125. Thank you. – Luke Ireton Apr 27 '18 at 13:15
  • 1
    Thanks for all your help Mani, I have figured out the last part on my own. :) – Luke Ireton Apr 27 '18 at 13:59
3

Unlike in (Objective-)C where nil, 0, and false are treated as the same value depending on the environment Int and Bool are not related in Swift and a Swift Bool doesn't have an intValue.

Practically you want to add weighting to num if noise is true.

let numDoubled = noise ? num + weighting : num

If you really need to convert false to 0 and true to 1 write

let boolAsInt = aBool ? 1 : 0
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Even though implicit conversion between bool and numeric types has been removed, you can explicitly convert a bool to numeric types by first converting it to an NSNumber.

let numDoubled = num + Float(truncating: NSNumber(booleanLiteral: noise)) * weighting

You can also do Float(truncating: noise) as a shorthand notation, but that call implicitly converts the bool to an NSNumber, so for clarity its better to write out the conversion explicitly.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116