Search code examples
objective-cswiftdelegatesnsmutablearrayprotocols

Cannot convert value of type 'NSArray.Element' to expected argument


I have a static library writen in Objective C where I have a protocol to get the callbacks of monitoring for regions results.

@protocol ScanBeaconsDelegate <NSObject>
@required
- (void) onBeaconDetected: (NSMutableArray <Beacon*> *) detectedBeacons
@end

Then, when I want to use de delegate method I use like this:

- (void) onBeaconDetected: (NSMutableArray <Beacon*> *) detectedBeacons
{
    for(Beacon *b in detectedBeacons)
    {
        //do staff
    }
}

Now, I developing a project in Swift, and I want to use the protocol in the same way, but xCode traduces the delegate method like this:

 func onBeaconDetected(_ detectedBeacons: NSMutableArray!) {
    for beacon: Beacon in detectedBeacons
    {
        //do staff
    }
}

I don't know how to cast, the detectedBeacons Array to Beacon Object, im getting a "Cannot convert sequence element type 'NSArray.Element' (aka 'Any') to expected type 'Beacon'".

I am very lost, since it is my first contact with swift. is there any way to solve this?


Solution

  • You can try

    func onBeaconDetected(_ detectedBeacons: NSMutableArray!) {
      for beacon in (detectedBeacons as! [Beacon]) {
        //do staff
      }
    }