bridgeToObjectiveC()在Swift中不可用于数组

时间:2022-06-25 16:56:29

I know it's not available since Xcode-beta 5. Please refer to this and this.

我知道从Xcode-beta 5开始就没有了。请参考这个和这个。

I have this source which is an extension for Array:

我有这个源是数组的扩展:

extension Array {
    func contains(object:AnyObject!) -> Bool {
        if(self.isEmpty) {
            return false
        }

        let array: NSArray = self.bridgeToObjectiveC();

       return array.containsObject(object)
    }
}

I modified it:

我修改:

extension Array {
    func contains(object:AnyObject!) -> Bool {
        if(self.isEmpty) {
            return false
        }

        return (self as NSArray).containsObject(object);
    }

Unfortunately this doesn't work. The error message is:

不幸的是,这是行不通的。错误信息是:

Cannot convert the expression's type 'AnyObject!' to type 'NSArray'

无法转换表达式的类型'AnyObject!“输入”NSArray”

What should I do? Thanks

我应该做什么?谢谢

1 个解决方案

#1


1  

Note that Swift has a 'find' function that you can use to see if an element is in an array or not:

请注意,Swift有一个“find”函数,您可以使用它来查看元素是否在数组中:

find(array,element):C.Index?

It will return nil if the item is not found. You should probably use this instead of contains.

如果没有找到项,它将返回nil。您可能应该使用这个而不是contains。

#1


1  

Note that Swift has a 'find' function that you can use to see if an element is in an array or not:

请注意,Swift有一个“find”函数,您可以使用它来查看元素是否在数组中:

find(array,element):C.Index?

It will return nil if the item is not found. You should probably use this instead of contains.

如果没有找到项,它将返回nil。您可能应该使用这个而不是contains。