swift 数组无法将类型 'AnyObject' 的值转换为预期的参数类型@noescape(AnyObject) throws -> Bool

标签 swift

    let otherArray:[AnyObject] = ["a","aa","aaa"]
    let otherArrayElemnt = otherArray[0]
    let myArray:[AnyObject] = ["qq"]
    if myArray.contains(otherArrayElemnt){     //<<<<<<< Error HERE
        //if contains...doSomething
    }else{
        //none doSomething
    }

错误:无法将“AnyObject”类型的值转换为预期的参数类型 @noscape (AnyObject) throws -> Bool

我不知道怎么解决

最佳答案

  1. String 不是对象,因此不符合 AnyObject 协议(protocol)。请改用 Any
  2. AnyObjectAny 都不是Equatable,所以函数contains 不知道如何比较元素包含 otherArrayElement 的数组。

为什么需要使用 Any 元素的数组?为什么不使用 String 的数组?您还想在其中存储什么?

关于swift 数组无法将类型 'AnyObject' 的值转换为预期的参数类型@noescape(AnyObject) throws -> Bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34786784/

相关文章:

ios - 在 Swift 4 中使用 draw-move for 循环制作动画

ios - iOS 14 中不会出现分页点

ios - 为什么不可能附加一个字符串!通过 += 运营商?

swift - Do - 捕获错误处理 Swift 2.0

ios - 使用 Realm 为新属性赋值时发生崩溃

swift - Mac 上的 DatePicker 不保存日期,直到按下返回键

ios - 如何为 pageControl.currentPage 创建枚举?

ios - 检查 AppDelegate 中 session 中的用户,如果没有则触发 UIViewController

swift - 如何确保我在函数中返回正确的 bool 值?

ios - 类型 'string' 没有成员 'playback'