采用RandomAccessCollection协议

时间:2017-03-12 08:14:54

标签: swift swift-protocols

public class Container2: RandomAccessCollection
{
    public typealias Indices = DefaultRandomAccessIndices<Container2>;
    enter code here

    public typealias Index = Int;
    //typealias SubSequence = Container2;
    public var arr:[Int] = [1,2,3,4,5];

    public var endIndex: Index {

        return 5;
    }

    public var startIndex:Index{
        return 0;
    }

    public func index(after i: Int) -> Int{
        return i+1;
    }



    public subscript(position: Int) -> Int{
        get{
            return arr[position];
        } set{
            arr[position] = newValue;
        }
    }

    /*func index(before i: Int) -> Int{
        return i-1;
    }*/

     public subscript(bounds: Range<Int>) -> Container2{
        get{
            return Container2();
        }   set{

            }
        }

}

我试图从类中采用RandomAccessCollection协议。我故意省略func index(before:)函数,因为RandomAccessCollection中已经存在该函数的默认实现。我确实理解由于RandomAccessCollection继承的BiDirectionalCollection,该函数的实现是必需的。我的问题是,为什么编译器会抱怨在我可以使用的RandomAccessCollection中已经有一个默认实现时需要func索引(之前:)实现?感谢。

0 个答案:

没有答案