简体   繁体   中英

Type 'T' does not conform to protocol 'AnyObject'

I am creating a IOS App in Swift which has blocks that move from the right side of the screen to the left side of the screen. After it is out of view I want to delete it. This is the Block Class:

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
        }

    }

}

It inherits from the BasicObject Class:

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)
    }
} 

Now in my Block Class there the line of code:

blocks.removeObject(self)

That line of code refers to this:

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)
            }
        }
    }
}

For some reason the error: Type 'T' does not conform to protocol 'AnyObject'. Can someone please explain why this is happening and provide an example of how to fix it. Or if someone could even provide an easier way to just delete the Block Object from the blocks array.

There are few things here. First of all array is struct, so you cannot modify the content in a method without making it mutating method. Another thing is how can you compare one T object to another unless they conform to Comparable protocol.

I modified few things in your example method removeObject,

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

By the way, you can do exactly same thing with Swift *indexOf** and removeAtIndex method.

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

Array is value type, so to modify need to declare function as mutating . Also declare generic interface as Equatable for compare, and check the same type of operators.

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
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM