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

时间:2021-01-13 18:06:34
    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
    }

Error: Cannot convert value of type 'AnyObject' to expected argument type @noscape (AnyObject) throws -> Bool

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

I don't know how to fix it

我不知道如何解决它

1 个解决方案

#1


1  

  1. String is not an object, so it does not conform to protocol AnyObject. Use Any instead.
  2. String不是对象,因此它不符合协议AnyObject。请改用Any。

  3. Neither AnyObject nor Any is Equatable, so function contains doesn't know how to compare elements of the array with otherArrayElement.
  4. AnyObject和Any都不是Equatable,因此function contains不知道如何将数组的元素与otherArrayElement进行比较。

Why do you need to use arrays of Any elements ? Why don't you use arrays of Strings ? What else do you want to store in them ?

为什么需要使用Any元素的数组?你为什么不使用字符串数组?你还想在其中存储什么?

#1


1  

  1. String is not an object, so it does not conform to protocol AnyObject. Use Any instead.
  2. String不是对象,因此它不符合协议AnyObject。请改用Any。

  3. Neither AnyObject nor Any is Equatable, so function contains doesn't know how to compare elements of the array with otherArrayElement.
  4. AnyObject和Any都不是Equatable,因此function contains不知道如何将数组的元素与otherArrayElement进行比较。

Why do you need to use arrays of Any elements ? Why don't you use arrays of Strings ? What else do you want to store in them ?

为什么需要使用Any元素的数组?你为什么不使用字符串数组?你还想在其中存储什么?