binary operator '/' cannot be applied to two 'Double' operands
The error is a bit misleading.
In the first set of code, array2
is implicitly declared as an array of Int
. So any attempt to assign a value to an index of array2
will require an Int
value.
The problem is that Double(value) / 2.0
results in a Double
value, not an Int
. So the compiler is looking for a version of /
that returns an Int
. And that version expects two Int
parameters. Since you are supplying two Double
parameters, you get the error mentioned in your question.
The solution is to either cast the result to an Int
or use two Int
parameters to /
.
var array2 = [8, 7, 19, 20]
for (index, value) in array2.enumerated() {
array2[index] = Int(Double(value) / 2.0) // cast to Int
}
or
var array2 = [8, 7, 19, 20]
for (index, value) in array2.enumerated() {
array2[index] = value / 2 // Use two Int
}
The result will be the same in this case. 8
will be replaced with 4
. 7
will be replaced with 3
, etc.
The second set of code works as-is because you declare the array to be filled with Double
so everything matches up with the correct type.
Swift - Binary Operator == cannot be applied to two [[Double]] operands
In this case there is nothing misleading.
Normally the ==
operator is defined for Equatable
items. That allows two Double
values to be compared to each other, e.g. 1.0 == 1.0
.
Then we have a specific ==
operator defined on Arrays of Equatable
items:
public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool
That means that you can compare any arrays with equatable items. However, the arrays themselves are not Equatable
.
There is no such operator defined for nested arrays.
You would have to define:
public func ==<Element : Equatable>(lhs: [[Element]], rhs: [[Element]]) -> Bool {
...
}
Swift-Binary operator cannot be applied to two Double operands
The error message is a bit misleading. The real problem is that scale(by:duration:)
takes a CGFloat
as the scale:
open class func scale(by scale: CGFloat, duration sec: TimeInterval) -> SKAction
So, you need to pass a CGFloat
. You can either work to make sure variable1
and variable3
are CGFloat
s, or you can use the CGFloat
constructor to convert it from a Double
:
let scaleAction = SKAction.scale(by: CGFloat(variable1 * variable3), duration: 1)
Binary operator '/' cannot be applied to two 'T' operands
Simply specify one of the Numeric
protocols.
For floating point precision:
struct PickerRange<T:FloatingPoint> {
var start: T
var end: T
var step: T
var length: T {
return max(start, end) - (min(start,end) / step)
}
}
SignedInteger
, UnsignedInteger
or other Numeric
protocols are also an option.
// Comparable
Neither of these require of you to specify Comparable
additionally, so specifying one of these is suffice for the case.
EDIT:
How to make one structure for Int and Double?
Essentially, you can, by specifying SignedNumeric:
struct PickerRange<T:SignedNumeric & Comparable> {
var start: T
var end: T
var step: T
}
// Treated as PickerRange<Double>
let a = PickerRange(start: 1.0, end: 5.0, step: 1)
// Treated as PickerRange<Int>
let b = PickerRange(start: 1, end: 5, step: 1)
However, you have specialized length
property, which includes /
operator not supported by SignedNumeric
. A workaround would consist in type-checking/force-casting to a type you have initialized your struct with:
struct PickerRange<T:SignedNumeric & Comparable> {
var start: T
var end: T
var step: T
var length: T {
if T.self is Double.Type {
return (max(start, end) as! Double) - ((min(start, end) as! Double) / (step as! Double)) as! T
} else if T.self is Int.Type {
return (max(start, end) as! Int) - ((min(start, end) as! Int) / (step as! Int)) as! T
} else {
return 0
}
}
}
let a = PickerRange(start: 1.0, end: 5.0, step: 1)
let b = PickerRange(start: 1, end: 10, step: 1)
print(a.length)
print(b.length)
Binary operator '*' cannot be applied to operands of type 'Float' and 'Double'
var lowPassReslts: Double = 0.0
var lowPassReslts1: Double = 0.0
let ALPHA: Float = 1.05
let averagePowerForChannel = pow(10, (0.05 * audioPlayer.averagePower(forChannel: 0)))
lowPassReslts = Double(ALPHA * Float(averagePowerForChannel) + (1.0 - ALPHA) * Float(lowPassReslts))
let averagePowerForChannel1 = pow(10, (0.05 * audioPlayer.averagePower(forChannel: 1)))
lowPassReslts1 = Double(ALPHA * Float(averagePowerForChannel1) + (1.0 - ALPHA) * Float(lowPassReslts1))
Binary operator '*' cannot be applied to two 'Int?' operands
The problem are the pointless and wrong type annotations. Remove them! All values are non-optional (and constants)
func calculateIMC(){
let textHeight = height.text
let textWeight = weight.text
let intHeight = Int(textHeight!) ?? 0
let intWeight = Int(textWeight!) ?? 0
let calculateHeight = intHeight * intHeight // probably intHeight * intWeight
}
Swift binary operator '+' cannot be applied to two CGFloat operands
The error message is wrong. The problem is that you are trying to multiply an Int
and a CGFloat
.
Replace:
innerY = innerY - CGFloat(gridHeight * row)
with:
innerY = innerY - gridHeight * CGFloat(row)
The answer above is for the current version of your code. For the commented out version that corresponds to the error message you posted:
Replace:
var innerY: CGFloat = CGFloat(relativePosition.y) + CGFloat(gridHeight * row)
with
var innerY: CGFloat = CGFloat(relativePosition.y) + gridHeight * CGFloat(row)
Related Topics
Show Am/Pm in Capitals in Swift
How to Add Alamofire Url Parameters
How to Create a Generic Array Extension That Sums Number Types in Swift
Using as a Concrete Type Conforming to Protocol Anyobject Is Not Supported
How to Check If Time Is Within a Specific Range in Swift
How to Find the Number of Days in Given Month and Year Using Swift
Difference Between 2 Dates in Weeks and Days Using Swift 3 and Xcode 8
Why Use Required Initializers in Swift Classes
Find Difference in Seconds Between Nsdates as Integer Using Swift
What's the Difference Between Struct Based and Class Based Singletons
Swift 2/iOS 9 - Libz.Dylib Not Found
Any Way to Iterate a Tuple in Swift
Swift Protocol Error: 'Weak' Cannot Be Applied to Non-Class Type
Init(Start:End:)' Is Deprecated: It Will Be Removed in Swift 3. Use the '..<' Operator
What Is the Use of "Static" Keyword If "Let" Keyword Used to Define Constants/Immutables in Swift