objective-c - 无法将“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 数组转换为信标对象,我得到一个“无法将序列元素类型'NSArray.Element'(又名'Any')转换为预期类型'Beacon'”。
我很迷茫,因为这是我第一次接触 swift。有没有办法解决这个问题?
解决方案
你可以试试
func onBeaconDetected(_ detectedBeacons: NSMutableArray!) {
for beacon in (detectedBeacons as! [Beacon]) {
//do staff
}
}
推荐阅读
- ajax - laravel 中的 ajax 没有显示。但在控制台中,我可以看到获取网址
- autodesk-forge - 核对清单 完全符合 Autodesk
- c# - Xamarin 中的 ListView 备用行颜色
- python - 如何将通过 POST 方法传递的参数与 Django rest 框架模型进行比较并生成自定义响应?
- iot - 用于渗透物联网设备的框架
- python - 如何使用 filedialog 打开一个 excel 文件和 pandas 来保存/读取/写入该文件
- javascript - Uint8Array.from 行为与 new Uint8Array 不同
- angular - 在reducer中获取后如何清除NgRx存储中的数据
- git - 如何从本地 windows 存储库推送到 windows git 远程服务器
- python - 如何使用类装饰器来实现单例模式?