【发布时间】: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