Swift实现removeObject
如果类没有继承自NSObject,那么类必须要实现Equatable
协议.否则不需要.
1 | class Observer { |
然后在通过扩展给Array增加一个removeObject(object: Element)
方法:
1 | extension Array where Element: Equatable { |
由于Array是结构体类型,而removeObject函数会改变结构体的值,所以该函数需要添加mutating关键字.“Notice the use of the mutating keyword in the declaration of SimpleStructure to mark a method that modifies the structure. The declaration of SimpleClass doesn’t need any of its methods marked as mutating because methods on a class can always modify the class.”