How to subclass an array class in Swift?

前端 未结 4 1814
予麋鹿
予麋鹿 2021-02-20 00:05

I am new in Swift. I have a base class:

class foo{}

I want to implement a foo collection class:

class foos: Array{}
         


        
4条回答
  •  误落风尘
    2021-02-20 00:25

    In Swift 2.x you can use a protocol extension.

    class Foo : Equatable {}
    // you need to provide the Equatable functionality
    func ==(leftFoo: Foo, rightFoo: Foo) -> Bool {
        return ObjectIdentifier(leftFoo) == ObjectIdentifier(rightFoo)
    }
    
    extension Array where Element : Foo {}
    

    protocol extensions provide "insert points" to extend classes that aren't classes, classes you don't own, etc.

提交回复
热议问题