I have an abstract class in my mind and I can't implement its several features in swift, so I use C++ to deliver my thoughts:
template <class T>
class Swapping {
public:
    void swap() { _foregroundIndex = backgroundIndex() }
    virtual void cleanup() = 0;
    T* foreground() { return _buffer[foregroundIndex()]; }
    T* background() { return _buffer[backgroundIndex()]; }
    void setForeground(T* foreground) { _buffer[foregroundIndex()] = foreground; }
    void setBackground(T* background) { _buffer[backgroundIndex()] = background; }
private: 
    short foregroundIndex() { return _foregroundIndex; } 
    short backgroundIndex() { return _foregroundIndex ^ 1; }
    short _foregroundIndex = 0;
    T* _buffer[2] = {NULL, NULL};
}
The main contradiction is that
- The pure virtual method cleanup()requires all subclasses to implement it explicitly (can achieve in swift withprotocol)
- The instance variable _foregroundIndexhas an initial value (cannot achieve usingprotocol)
- The instance variable _foregroundIndexis restricted to beprivate( cannot achieve usingprotocol)
On the other hand, if I use a class instead of protocol, then I can't guarantee cleanup() method is overriden. 
One may suggest that put the virtual method in a protocol and the instance variable in a class. That may work but is not a obsession-satisfying one. 
P.S. Objective-C is not Swift. Any objc_runtime related workaround is not preferred.
 
    