0

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?

2
  • Here Beacon is your model class?? Commented Jan 13, 2021 at 11:40
  • Yes, is a customClass Commented Jan 13, 2021 at 11:44

1 Answer 1

1

You can try

func onBeaconDetected(_ detectedBeacons: NSMutableArray!) {
  for beacon in (detectedBeacons as! [Beacon]) {
    //do staff
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

but doing "func onBeaconDetected(_ detectedBeacons:[Beacon])", I get does not conform to protocol 'ScanBeaconsDelegate', and I cant change the static library code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.