Swift 2.0 - Binary Operator | cannot be applied to two UIUserNotificationType operands
In Swift 2, many types that you would typically do this for have been updated to conform to the OptionSetType protocol. This allows for array like syntax for usage, and In your case, you can use the following.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
And on a related note, if you want to check if an option set contains a specific option, you no longer need to use bitwise AND and a nil check. You can simply ask the option set if it contains a specific value in the same way that you would check if an array contained a value.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
if settings.types.contains(.Alert) {
// stuff
}
In Swift 3, the samples must be written as follows:
let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
and
let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
if settings.types.contains(.alert) {
// stuff
}
Binary operator '~=' cannot be applied to two 'OptionalError' operands
Here, the word error
actually refers to the parameter error
, rather than a new variable:
case .ExportError(error):
So you are essentially saying:
// code for illustration purposes only, not supposed to work
(error as? ClassError) == .some(.ExportError(error))
Note that the two occurrences of error
refers to the same thing. That's probably not what you meant, but the compiler thinks that is what you meant, and tries to pattern match the associated value of ExportError
against error
using the ~=
operator, as error
here is considered an expression pattern
However, both operands are of type Optional<Error>
. The ~=
operator on Optional<Wrapped>
requires that Wrapped
conforms to Equatable
, but Error
does not conform to Equatable
as it is a protocol. Hence the error message.
You probably meant to introduce a new identifier, so you should use a binding pattern, rather than just an expression pattern.
case .ExportError(let exportError):
print("Error :", exportError)
getting error: Binary operator '==' cannot be applied to two 'x' operands, how to remove certain number of elements in this array of objects
==
is defined on Equatable
, which your Cards
type is not. You either need to make Cards
conform to equatable, and decide which of your properties counts towards "equal" (The same type? the same income? both? What about the images?), or compare directly on the properties you do care about.
Error comparision - Binary operator == cannot be applied two Error operands
You need to cast the Swift.Error
to your custom ServiceError
. The common approach is to use a switch:
enum ServiceError: Error {
case serverDown, userNotExist
}
let error: Error? = ServiceError.serverDown as Error
switch error as? ServiceError {
case .serverDown: print("Server is down") // "Server is down\n"
case .userNotExist: print("User not found")
case .none: print("error:", error ?? "nil")
}
Note: It is Swift naming convention to name your enumeration cases starting with a lowercase letter.
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 'Comparable' operands
The <
operator applies to two values of a single type that conforms to Comparable — not to two things designated merely as Comparable. A protocol does not conform to itself.
The use of any
with MyElement doesn't affect this basic fact about the Comparable-constrained generic placeholder T. Since MyElement is generic over T, we would have to know that the two different MyElement-conforming objects you propose to sort resolve their T to the same type as one another — and in the code you wrote, we don't know that, so the compiler balks.
binary operator ' ' cannot be applied two T operands
You need to tell Swift that T
conforms to Comparable
protocol in order for it to allow using operator <
on objects of type T
:
func maxNumer<T : Comparable>(array:[T]) -> T {
// ^^^^^^^^^^
var maxNumer = array[0]
for var i = 0; i < array.count-1; i++ {
if maxNumer < array[i+1] { //This line gets error as title
maxNumer = array[i+1]
}
}
return maxNumer
}
Binary operator '' cannot be applied to two 'T' operands
As Tomasz Wójcik already said, the left shift
operator is in Swift 3 only defined for concrete integer types,
but not for the UnsignedInteger
protocol.
A possible workaround
is to replace the left shift by a repeated multiplication:
func doubleHexMask<T: UnsignedInteger>(offset: Int) -> T {
var mask: T = 0xFF
for _ in 0..<offset {
mask = mask &* 16
}
return mask
}
This also makes the forced cast as! T
unnecessary. The
"overflow operator &*
" is used
here so that bits moved beyond the bounds of the integer’s storage are discarded, corresponding to what the left shift operator does.
Related Topics
Custom Uitableviewcell from Nib in Swift
How to Find Specific Subclass of Nsmanagedobject
Move View With Keyboard Using Swift
Is [Uiscreen Mainscreen].Bounds.Size Becoming Orientation-Dependent in Ios8
Submit to App Store Issues: Unsupported Architecture X86
How to Opt-Out of Dark Mode on iOS 13
Any Way to Pre Populate Core Data
How Disable Copy, Cut, Select, Select All in Uitextview
Setting Action For Back Button in Navigation Controller
Objective-C and Swift Url Encoding
How to Add Uitableview Within a Uitableviewcell
Conditional Binding: If Let Error - Initializer For Conditional Binding Must Have Optional Type
Ios 7: Uitableview Shows Under Status Bar
How to Apply Gradient to Background View of iOS Swift App
How to Get Current Location from User in Ios
Get Location Updates For iOS App Even When Suspended
How to Get a Background Location Update Every N Minutes in My iOS Application