【问题标题】:Type 'T' does not conform to protocol 'AnyObject'类型“T”不符合协议“AnyObject”
【发布时间】:2015-06-24 22:25:49
【问题描述】:

我正在用 Swift 创建一个 IOS 应用程序,它的块从屏幕右侧移动到屏幕左侧。在它看不见后,我想删除它。这是块类:

class Block : BasicObject {

    var type : Int
    var velocity = CGVectorMake(-playerVelocityY, -playerVelocityY)

    init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat, type: Int) {
        self.type = type
        super.init(x : x, y : y, width : width, height : height)
    }

    func update() {

        self.position.x += self.velocity.dx

        if self.position.x + self.size.width < 0 {
            blocks.removeObject(self) // <-- line of code to delete object from list
        }

    }

}

它继承自 BasicObject 类:

class BasicObject {
    var position : CGPoint
    var size : CGSize
    init (x : CGFloat, y : CGFloat, width : CGFloat, height : CGFloat){
        self.position = CGPointMake(x, y)
        self.size = CGSizeMake(width, height)
    }
} 

现在在我的 Block Class 中有一行代码:

blocks.removeObject(self)

那行代码指的是这个:

var blocks : [Block] = []

extension Array {
    func removeObject<T>(object: T) {
        for items in self {
            if self[items] === object { // <-- Error occurs on this line
                self.removeAtIndex(items)
            }
        }
    }
}

由于某种原因,错误:类型“T”不符合协议“AnyObject”。有人可以解释为什么会发生这种情况,并提供一个如何解决它的例子。或者如果有人甚至可以提供一种更简单的方法来从块数组中删除块对象。

【问题讨论】:

    标签: ios swift compiler-errors


    【解决方案1】:

    这里的东西很少。首先数组是结构体,所以你不能修改一个方法中的内容,而不使它成为 mutating 方法。另一件事是如何将一个 T 对象与另一个对象进行比较,除非它们符合 Comparable 协议。

    我在你的示例方法 removeObject 中修改了一些东西,

    extension Array where T:Comparable{
        mutating func removeObject(object: T) {
            for (index, item) in self.enumerate() {
                if item == object {
                    self.removeAtIndex(index)
                    break
                }
            }
        }
    }
    

    顺便说一句,你可以用 Swift *indexOf** 和 removeAtIndex 方法做同样的事情。

    extension Array where T:Comparable{
        mutating func removeObject(object: T) {
            while  let index =  indexOf(object) {
                removeAtIndex(index)
            }
        }
    }
    

    【讨论】:

    • 是的。你用的是哪个版本?
    • 我现在明白我必须将 mutating 和 ComparableI 放入此代码中,但是当我将新代码放入其中时,第一行显示错误:Statement cannot begin with a closure expression .而且我没有使用 Swift 2。
    【解决方案2】:

    数组是值类型,所以修改需要声明函数为mutating。 还将泛型接口声明为 Equatable 以进行比较,并检查相同类型的运算符。

    extension Array {
      mutating func removeObject<T: Equatable>(value: T) {
        for (index, item) in enumerate(self) {
          if let item = item as? T where item == value {
            removeAtIndex(index)
            return
          }
        }
      }
    }
    

    【讨论】:

    • 我之前尝试过这样做,但显示错误:类型'Block'不符合协议'Equatable'。如何使 Block Equatable
    • 我把它拿走了,我没有收到任何错误。然后我在操场上测试它,我相信它变成了一个运行时错误
    • 你需要在你的 Block 中添加和实现 Equatable 协议。
    猜你喜欢
    • 2014-10-23
    • 2015-01-02
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多