objective-c - Swift beta 5 中的 bridgeToObjectiveC 和 makeObjectsPerformSelector

标签 objective-c swift

我的这段代码带有一个在 Xcode 6 beta 4 中工作的完成处理程序,它不再在 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”的成员

请注意,方法中的 CGFloat 和 Uint 转换是针对 beta 4 解决方法的,我只是还没有更新那部分。该问题涵盖在: ‘CGFloat’ is not convertible to ‘UInt8' and other CGFloat issues with Swift and Xcode 6 beta 4

我认为处理完成处理程序的解决方案可能是将数组视为 NSArray,详见: What is the swift equivalent of makeObjectsPerformSelector?

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

但是,假设我的语法正确,只会导致另一个错误 'makeObjectsPerformSelector' is unavailable: 'performSelector' methods are unavailable

这是一个新的 Swift 错误,还是我在发行说明中遗漏了什么?

最佳答案

bridgeToObjectiveCbridgeFromObjectiveC 函数在 Xcode 6.0 beta 5 中不可用。当您需要在一个 Swift 对象。例如:

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

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

作为the question you cited注意,您可以使用 map 对数组的每个元素调用函数/方法。您还可以使用 filterfindfor-in 循环,或者在转换为 NSArray 之后enumerateObjects 方法之一。请注意,许多人认为使用 functional-programming 是不好的风格。为非“功能性”任务构造(mapfilterreducefind)——是,运行具有 side effects 的代码.因此,for-in 循环可能是实现您所追求的目标的最简洁方法。

关于objective-c - Swift beta 5 中的 bridgeToObjectiveC 和 makeObjectsPerformSelector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25126188/

相关文章:

ios - 如何在快速按下按钮时添加一个独立的 UIView?

ios - 将砌体 x/y 约束设置为 View 宽度/高度的百分比

ios - 填充 uilabel 的渐变时文本消失,自定义 uilabel

ios - 如何在 Objective-C 中重置对象

ios - 这个片段在后台运行 block 有多好?

objective-c - 模拟 CLLocationManager (swift) 用于测试

iphone - 如何将数组传递给方法,然后确定数组的大小?

ios - Swift - 发送到实例的无法识别的选择器

swift - 获取 SKNode 父节点中第一个和最后一个子节点的属性

swift - 使用 alamofire 发送 base64 字符串