Swift operator "*" throwing error on two Ints - swift

I have a very odd error here and i've searched all around and i have tried all the suggestions. None work.
scrollView.contentSize.height = 325 * globals.defaults.integer(forKey: "numCards")
Binary operator '*' cannot be applied to two 'Int' operands
WTF Swift! Why not? I multiply Ints all the time. These ARE two Ints. globals.defaults is just an instance of UserDefaults.standard. I have tried the following with the same error each time.
325 * Int(globals.defaults.integer(forKey: "numCards") //NOPE
Int(325) * Int(globals.defaults.integer(forKey: "numCards")) //NOPE
if let h = globals.defaults.integer(forKey: "numCards"){
325 * h //NOPE, and 'Initializer for conditional binding must have optional type, not Int'
}
let h = globals.defaults.integer(forKey: "numCards") as! Int
325 * h //NOPE, and 'Forced cast of Int of same type as no affect'
325 * 2 //YES! But no shit...
All of those "attempts" seemed like a waste of time as i know for a fact both of these are Ints...and i was correct. Please advise. Thanks!

The error is misleading. The problem is actually the attempt to assign an Int value to a CGFloat variable.
This will work:
scrollView.contentSize.height = CGFloat(325 * globals.defaults.integer(forKey: "numCards"))
The cause of the misleading error (thanks to Daniel Hall in the comments below) is due to the compiler choosing the * function that returns a CGFloat due to the return value needed. This same function expects two CGFloat parameters. Since the two arguments being provided are Int instead of CGFloat, the compiler provides the misleading error:
Binary operator '*' cannot be applied to two 'Int' operands
It would be nice if the error was more like:
Binary operator '*' cannot be applied to two 'Int' operands. Expecting two 'CGFloat' operands.

Related

How to understand the Int Double conversion in Swift 5.5?

Extend the example in “The Swift Programming Language” (Swift 5.5) “Integer and Floating-Point Conversion”:
3 + 0.14 // allowed
let three = 3
let rest = 0.14
3 + rest // allowed
0.14 + three // compile error
three + 0.14 // compile error
I don’t understand why the last two lines are taken as compile error. Can anyone help to explain a bit? Thanks.
There are two basic rules:
A numeric literal without type annotation can be converted implicitly if possible.
A constant or variable is initialized with a fixed type which cannot change. A floating point literal becomes Double and an integer literal becomes Int.
So three is Int and 0.14 is Double.
3 + rest works because 3 can be inferred as Double.
But 0.14 cannot be inferred as Int so the last two lines fail to compile.

Swift-Binary operator cannot be applied to operands, when converting degrees to radians

I'm aware of some relatively similar questions on this site, but if they do apply to my problem (which I'm not certain they do) then I certainly don't understand them. Here's my problem;
var degrees = UInt32()
var radians = Double()
let degrees:UInt32 = arc4random_uniform(360)
let radians = angle * (M_PI / 180)
This returns an error, focused on the multiplication star, reading; "Binary operator "*" cannot be applied to operands of type 'UInt32' and 'Double'.
I'm fairly sure I need to have the degrees variable be of type UInt32 to randomise it, and also that the pi constant cannot be made to be of UInt32, or at least I don't know how, as I'm relatively new to Xcode and Swift in general.
I'd be very grateful if anyone had a solution to my problem.
Thanks in advance.
let degree = arc4random_uniform(360)
let radian = Double(degree) * .pi/180
you need to convert the degree to double before the multiplication .
from apple swift book:
Integer and Floating-Point Conversion
Conversions between integer and floating-point numeric types must be made explicit:
let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double
Here, the value of the constant three is used to create a new value of type Double, so that both sides of
the addition are of the same type. Without this conversion in place, the addition would not be allowed.
Floating-point to integer conversion must also be made explicit. An integer type can be initialized
with a Double or Float value:
1 let integerPi = Int(pi)
2 // integerPi equals 3, and is inferred to be of type Int
Floating-point values are always truncated when used to initialize a new integer value in this way.
This means that 4.75 becomes 4, and -3.9 becomes -3.

Read a double * in Swift

I have an Obj-C method that returns a double *, how is this accessed in Swift as a Double?
I get this error
Cannot convert value of type 'UnsafeMutablePointer<Double>' to expected argument type 'Double'
I am calling this - (double * _Nonnull) modIntensityForDestination:(int) destination;
and failing when I do this
let intensity = audioEngine.modIntensityForDestination(Int32(modDestinationID))
I have tried withUnsafeMutablePointer but cant seem to get it working.
If the method returns a pointer to a single floating point number
then you can dereference it with .memory:
let intensity = audioEngine.modIntensityForDestination(...).memory
In Swift 3 it would be .pointee.

Binary operator "+" cannot be applied to operands of type "CGFloat" and "CGPoint" in Swift

obstacle1B.position = CGPointMake(square.size.width * 2 + obstacle1A.position, self.frame.size.height)
The error says: Binary operator "+" cannot be applied to operands of type "CGFloat" and "CGPoint"
I think I have to convert something to a CGFloat but I'm not sure. Help?
square.size.width * 2 is a CGFloat but obstacle1A.position is a CGPoint.
Perhaps you meant obstacle1A.position.x (or obstacle1A.position.y)?

Multiplying integers

I am certain I am missing something very, very obvious, but can anyone tell me why I am having trouble multiplying two Integers? The following code:
let twenty: Integer = 20
let ten: Integer = 10
let result: Integer = twenty * ten
presents the error Could not find an overload for '*' that accepts the supplied arguments.
Other questions on SO with the same error are caused by trying to multiply different types together, but surely these are both Integer types?
(PS: The actual code I am trying to run is var value = self.value * 10 but I have expanded it to the sample while debugging to make absolutely sure that the correct types are being used)
use Int instead. Integer is a protocol.
Integer is a protocol not a type. Use Int instead.
As already stated , Integer is a protocol not a type .
In your situation, you don't need to do explicit the type because it is of implicit casting.
This could be enough
let twenty = 20
let ten = 10
let result = twenty * ten
NSLog("%d", result)