编译器错误会告诉你。我把你的代码放在游乐场里,看到了以下错误。
Playground execution failed: MyPlayground.playground:15:1: error: type 'BookCollection' does not conform to protocol 'Indexable'
extension BookCollection: CollectionType
^
Swift.Indexable:6:15: note: unable to infer associated type 'Index' for protocol 'Indexable'
typealias Index : ForwardIndexType
^
Swift.CollectionType:2:12: note: inferred type 'Range<BookCollection.Index>' (by matching requirement 'subscript') is invalid: does not conform to 'ForwardIndexType'
public subscript (bounds: Range<Self.Index>) -> Slice<Self>
^
MyPlayground.playground:15:1: error: type 'BookCollection' does not conform to protocol 'SequenceType'
extension BookCollection: CollectionType
^
Swift.SequenceType:35:17: note: protocol requires function 'generate()' with type '() -> Generator'
public func generate() -> Self.Generator
^
Swift.SequenceType:2:17: note: candidate has non-matching type '<`Self`> () -> `Self`' (aka '<τ_0_0> () -> τ_0_0')
public func generate() -> Self
^
Swift.SequenceType:5:17: note: candidate has non-matching type '<`Self`> () -> `Self`.Base.Generator' (aka '<τ_0_0> () -> τ_0_0.Base.Generator')
public func generate() -> Self.Generator
^
Swift.CollectionType:2:17: note: candidate has non-matching type '<`Self`> () -> IndexingGenerator<`Self`>' (aka '<τ_0_0> () -> IndexingGenerator<τ_0_0>')
public func generate() -> IndexingGenerator<Self>
从头开始看,我发现我们需要一个与索引类型相关联的类型,然后有很多错误与它没有定义有关,所以我在扩展中为协议Indexable添加了一个typealias,这给了我更多的错误,所以我暂时将CollectionType更改为Indexable。
extension BookCollection: Indexable
{
typealias Index = Int
}
现在我有以下内容:
Playground execution failed: MyPlayground.playground:15:1: error: type 'BookCollection' does not conform to protocol 'Indexable'
extension BookCollection: Indexable
^
Swift.Indexable:21:15: note: protocol requires nested type '_Element'
typealias _Element
^
_Element
应该是一项实现细节(它以下划线开头),但现在让我们使用它并为其添加一个类型别名。
extension BookCollection: Indexable
{
typealias Index = Int
typealias _Element = Book
}
现在我有以下内容。
Playground execution failed: MyPlayground.playground:15:1: error: type 'BookCollection' does not conform to protocol 'Indexable'
extension BookCollection: Indexable
^
Swift.Indexable:12:16: note: protocol requires property 'startIndex' with type 'Index' (aka 'Int')
public var startIndex: Self.Index { get }
^
Swift.Indexable:20:16: note: protocol requires property 'endIndex' with type 'Index' (aka 'Int')
public var endIndex: Self.Index { get }
^
Swift.Indexable:22:12: note: protocol requires subscript with type 'Index -> _Element'
public subscript (position: Self.Index) -> Self._Element { get }
现在我实现了两个属性和下标。我注意到实现下标可以让编译器推断出这两个类型别名,所以我可以删除那些声明。
extension BookCollection: Indexable
{
var startIndex: Int { return books.startIndex }
var endIndex: Int { return books.endIndex }
subscript(index: Int) -> Book
{
return books[index]
}
}
这段代码没有错误,所以我们将协议改回
CollectionType
,但仍然没有错误,说明我们已经完成了。
CollectionType
可以跳转到协议。任何不是optional
的内容都应该被实现。 - hannad@optional
。这意味着任何想要遵循该协议的类,可以选择性地实现这个函数。任何没有在其前面加上@optional
的函数或属性都必须被实现,以避免代码产生编译错误。 - hannadCollectionType
,将会为你打开代码文件,以显示该协议的属性和方法。 - hannad