Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Int does not conform to protocol sequence

Tags:

I have the following code in Swift 3:

var numbers = [1,2,1]
for number in numbers.count - 1 { // error
    if numbers[number]  < numbers[number + 1] {
        print(number)
    }
}

I am checking if the value on the index [number] is always higher than the value on the index [number + 1]. I am getting an error:

Type Int does not conform to protocol sequence

Any idea?

like image 863
Toby V. Avatar asked Feb 05 '17 23:02

Toby V.


People also ask

How do you conform a sequence?

To add Sequence conformance to your own custom type, add a makeIterator() method that returns an iterator. Alternatively, if your type can act as its own iterator, implementing the requirements of the IteratorProtocol protocol and declaring conformance to both Sequence and IteratorProtocol are sufficient.

What is sequence protocol?

A sequence protocol requires a factory method that returns an Iterator type as shown in the code below: An iterator is a type that conforms to Iterator protocol. The protocol has a mutating method next() that returns individual values one after the other until it can't anymore, in which case it returns a nil.


1 Answers

It may be swift. You can use this iteration.

for number in 0..<(numbers.count-1)
like image 158
mqz.kim Avatar answered Sep 18 '22 15:09

mqz.kim