无法将“NSArray.Element”类型的值转换为预期参数

问题描述

我有一个用 Objective C 编写的静态库,我有一个协议来获取对区域结果的监控回调。

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

然后,当我想使用 de 委托方法时,我会这样使用:

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

现在,我在 Swift 中开发了一个项目,我想以同样的方式使用该协议,但是 xCode 将委托方法翻译成这样:

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

我不知道如何将检测到的Beacons Array 转换为Beacon 对象,我得到一个“无法将序列元素类型'NSArray.Element'(又名'Any')转换为预期类型'Beacon'”。

我很失落,因为这是我第一次接触swift。有什么办法可以解决这个问题吗?

解决方法

你可以试试

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