本文介绍了Swift Beta 5中的bridgeToObjectiveC和makeObjectsPerformSelector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此代码中使用了在Xcode 6 beta 4中工作的完成处理程序,而该处理程序在Xcode 6 beta 5中不再工作.

I had this code with a completion handler working in Xcode 6 beta 4 that no longer works in Xcode 6 beta 5.

dropsToRemove.bridgeToObjectiveC().makeObjectsPerformSelector("removeFromSuperview")

完整方法...

func animateRemovingDrops(dropsToRemove: [UIView]) {
    println(__FUNCTION__)
    UIView.animateWithDuration(1.0,
        animations: {
            for dropView in dropsToRemove {
                let x = CGFloat(UInt(arc4random_uniform(UInt32(UInt(self.gameView.bounds.size.width) * 5)))) - self.gameView.bounds.size.width * 2
                let y = self.gameView.bounds.size.height
                dropView.center = CGPointMake(x, -y)
        }}, completion: { finished in
                dropsToRemove.bridgeToObjectiveC().makeObjectsPerformSelector("removeFromSuperview")
        })
}

错误是"[UIView]"没有名为"bridgeToObjectiveC"的成员

The error is '[UIView]' does not have a member named 'bridgeToObjectiveC'

请注意,该方法中的CGFloat和Uint强制转换是针对beta 4的解决方法,我只是尚未更新该部分.该问题位于:"CGFloat"不可转换Swift和Xcode 6 beta 4导致'UInt8'和其他CGFloat问题

Note that the CGFloat and Uint casting in the method is for a beta 4 workaround, I just haven't updated that part yet. That issue is covered at:‘CGFloat’ is not convertible to ‘UInt8' and other CGFloat issues with Swift and Xcode 6 beta 4

我认为处理完成处理程序的解决方案可能是将数组作为NSArray对待,详细信息如下: makeObjectsPerformSelector的快速等效项是什么?

I thought the solution for dealing with the completion handler might be to treat the array as an NSArray as detailed in:What is the swift equivalent of makeObjectsPerformSelector?

(dropsToRemove as NSArray).makeObjectsPerformSelector("removeFromSuperview")

但是,假设我正确使用了语法,只会导致另一个错误"makeObjectsPerformSelector"不可用:"performSelector"方法不可用

However, assuming I got the syntax right, simply results in another error 'makeObjectsPerformSelector' is unavailable: 'performSelector' methods are unavailable

这是Swift的新错误,还是发行说明中缺少的内容?

Is this a new Swift bug, or something I'm missing in the release notes?

推荐答案

bridgeToObjectiveCbridgeFromObjectiveC函数在Xcode 6.0 beta 5中不可用.相反,当需要使用时,强制转换为适当的Foundation类型或从中转换为适当的Foundation类型. Swift对象上该类型的API.例如:

The bridgeToObjectiveC and bridgeFromObjectiveC functions are not available in Xcode 6.0 beta 5. Instead, cast to/from the appropriate Foundation type when you need to use that type's API on a Swift object. For example:

var arr = ["One", "Two"]
(arr as NSArray).indexOfObject("One")

自第一个Swift测试版以来,Apple已使用performSelector和相关方法警告(或明确使其不可用).可能是在beta 5之前仍然可用的任何此类API都是无意的.

Apple has warned against (or explicitly made unavailable) using performSelector and related methods since the first Swift beta. Presumably any such API that were still available before beta 5 were unintentionally so.

作为您引用的问题注释,您可以使用map在数组的每个元素上调用函数/方法.您还可以使用filterfindfor-in循环,或者在转换为NSArray之后使用enumerateObjects方法之一.请注意,许多人认为使用功能编程结构(mapfilterreducefind)来执行非功能性"任务-即运行具有副作用.因此,for-in循环可能是执行您要执行的操作的最干净的方法.

As the question you cited notes, you can use map to call a function/method on every element of an array. You can also use filter, find or a for-in loop, or after casting to NSArray, one of the enumerateObjects methods. Note that many consider it bad style to use the functional-programming constructs (map, filter, reduce, find) for tasks that aren't "functional" -- that is, to run code that has side effects. So a for-in loop might be the cleanest way to do what you're after.

这篇关于Swift Beta 5中的bridgeToObjectiveC和makeObjectsPerformSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:07