-2

I'm using BeaconKit to detect near beacons around.

I want to check if the beacon detected is of type AltBeacon. My code is showing an error because of the matching expression I'm using.

if beacon.beaconType == AltBeacon {
     print("Detected an AltBeacon")
}

Here is the error message I'm getting

Binary operator == cannot be applied to operands of type Int' and AltBeacon.Type

What can I use instead so I can correct my matching expression please ! Thank you

  • Possible duplicate of [Checking if an object is a given type in Swift](https://stackoverflow.com/questions/24091882/checking-if-an-object-is-a-given-type-in-swift) – wootage Oct 24 '19 at 13:58

2 Answers2

1

Have you tried

if beacon.beaconType is AltBeacon {
   print("Detected an AltBeacon")
}

or instead, you can check

if let beacon.beaconType as? AltBeacon {
   print("Detected an AltBeacon")
} else {
// handle
}
  • Actually according to the source code both `if` expressions will always evaluate to `false`. – vadian Oct 24 '19 at 14:46
1

The error is clear. beaconType is an Int. It cannot be compared with anything else but an Int

Two possible solutions:

  1. Figure out the integer value of AltBeacon (2 is just an example) and compare

    if beacon.beaconType == 2 { ...
    
  2. As AltBeacon is a subclass of Beacon check the type of the instance

    if beacon is AltBeacon { ...
    
vadian
  • 274,689
  • 30
  • 353
  • 361