Can someone explain what someVar.successor() is? Apple documentation says "Returns the next consecutive value after self
.". I don't understand its implementation meaning.
Thanks.
The successor()
method returns the next value after the current one (if any, if the current value is 0 then calling successor()
will return 1 and so on)
A typical successor() implementation will look like:
class ForWardIndexDemo: ForwardIndex
{
private var _myIndex = 0
init(index: Int)
{
_myIndex = index;
}
func successor() -> ForWardIndexDemo
{
return ForWardIndexDemo(index:_myIndex++)
}
}
The collection associated type IndexType specifies which type is used to index the collection. Any type that implements ForwardIndex can be used as the IndexType.
The ForwardIndex is an index that can only be incremented, for example a forward index of value 0 can be incremented to 1,2,3 etc…, This protocol internally inherits from Equatable and _Incrementable protocols. In order to adhere to the ForwardIndex protocol successor() -> Self method and the Equatable protocols must be implemented.
Read more about this here
Instead of adding 1, we can call successor() on index.
For example this :
func naturalIndexOfItem(item: Item) -> Int? {
if let index = indexOfItem(item) {
return index + 1
} else {
return nil
}
}
Is equal to this:
func naturalIndexOfItem(item: Item) -> Int? {
if let index = indexOfItem(item) {
return index.successor()
} else {
return nil
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With