简体   繁体   English

类型 Int 不符合协议序列

[英]Type Int does not conform to protocol sequence

I have the following code in Swift 3:我在 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].我正在检查索引 [number] 上的值是否始终高于索引 [number + 1] 上的值。 I am getting an error:我收到一个错误:

Type Int does not conform to protocol sequence类型 Int 不符合协议序列

Any idea?任何的想法?

It may be swift.它可能很快。 You can use this iteration.您可以使用此迭代。

for number in 0..<(numbers.count-1)

The error is because Int is not a Sequence .错误是因为Int不是Sequence You can create a range as already suggested, which does conform to a sequence and will allow iteration using for in .您可以按照已经建议的方式创建一个范围,该范围确实符合序列并允许使用for in进行迭代。

One way to make Int conform to a sequence is:使Int符合序列的一种方法是:

extension Int: Sequence {
    public func makeIterator() -> CountableRange<Int>.Iterator {
        return (0..<self).makeIterator()
    }
}

Which would then allow using it as a sequence with for in .这将允许将其用作for in的序列。

for i in 5 {
    print(i)
}

but I wouldn't recommend doing this.但我不建议这样做。 It's only to demonstrate the power of protocols but would probably be confusing in an actual codebase.这只是为了展示协议的强大功能,但在实际代码库中可能会造成混淆。

From you example, it looks like you are trying to compare consecutive elements of the collection.从您的示例来看,您似乎正在尝试比较集合的连续元素。 A custom iterator can do just that while keeping the code fairly readable:自定义迭代器可以做到这一点,同时保持代码的可读性:

public struct ConsecutiveSequence<T: IteratorProtocol>: IteratorProtocol, Sequence {
    private var base: T
    private var index: Int
    private var previous: T.Element?

    init(_ base: T) {
        self.base = base
        self.index = 0
    }

    public typealias Element = (T.Element, T.Element)

    public mutating func next() -> Element? {
        guard let first = previous ?? base.next(), let second = base.next() else {
            return nil
        }

        previous = second

        return (first, second)
    }
}

extension Sequence {
    public func makeConsecutiveIterator() -> ConsecutiveSequence<Self.Iterator> {
        return ConsecutiveSequence(self.makeIterator())
    }
}

which can be used as:可以用作:

for (x, y) in [1,2,3,4].makeConsecutiveIterator() {
    if (x < y) {
        print(x)
    }
}

In the above example, the iterator will go over the following pairs:在上面的例子中,迭代器将遍历以下对:

(1, 2)
(2, 3)
(3, 4)

This maybe a little late but you could have done:这可能有点晚了,但你可以这样做:

for number in numbers { }

instead of:代替:

for number in numbers.count - 1 { }

For a for loop to work a sequence (range) is needed.要使 for 循环工作,需要一个序列(范围)。 A sequence consists of a stating a value, an ending value and everything in between.一个序列由一个声明值、一个结束值以及介于两者之间的所有内容组成。 This means that a for loop can be told to loop through a range with ether这意味着可以告诉 for 循环使用 ether 循环遍历一个范围

for number in 0...numbers.count-1 { }   `or`   for number in numbers { } 

Both example give the nesasery sequences.两个例子都给出了nesasery序列。 Where as:然而:

 for number in numbers.count - 1 { }

Only gives one value that could either be the starting or the ending value, making it impossible to work out how many time the for loop will have to run.只给出一个可能是起始值或结束值的值,因此无法计算 for 循环必须运行多少时间。

For more information see Apple's swift control flow documnetation有关更多信息,请参阅Apple 的 swift 控制流文档

This error can also come about if you try to enumerate an array instead of the enumerated array.如果您尝试枚举数组而不是枚举数组,也会出现此错误。 For example:例如:

for (index, element) in [0, 3, 4] { 

}

Should be:应该:

for (index, element) in [0, 3, 4].enumerated() { 

}

So first you need to understand what is sequence.. A type that provides sequential, iterated access to its elements.因此,首先您需要了解什么是序列。一种提供对其元素的顺序、迭代访问的类型。

A sequence is a list of values that you can step through one at a time.序列是一个值列表,您可以一次遍历一个值。 The most common way to iterate over the elements of a sequence is to use a for-in loop:迭代序列元素的最常见方法是使用 for-in 循环:

let oneTwoThree = 1...3.让 oneTwoThree = 1...3。 // Sequence // 序列

for loop actually means for 循环实际上意味着

For number in Sequences {}对于序列中的数字 {}

So you need to use for number in 0..<(numbers.count-1) {}所以你需要使用 for number in 0..<(numbers.count-1) {}

The error is because number is not an index, but the element of the array on each iteration.错误是因为number不是索引,而是每次迭代时数组的元素。 You can modify your code like this:你可以像这样修改你的代码:

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

Or there is a trick using the sort method, but that's kind of a hack (and yes, the subindexes are right, but look like inverted; you can try this directly on a Playground):或者有一个使用 sort 方法的技巧,但这是一种黑客(是的,子索引是正确的,但看起来像倒置;你可以直接在 Playground 上尝试这个):

var numbers = [1,2,1,0,3]
numbers.sort {
    if $0.1 < $0.0 {
        print ($0.1)
    }
    return false
}

For me, this error occurred when I tried writing a for loop, not for an array but a single element of the array.对我来说,当我尝试编写 for 循环时发生了这个错误,不是为数组而是为数组的单个元素。

For example:例如:

let array = [1,2,3,4]
let item = array[0]

for its in item
{
   print(its)
}

This gives an error like: Type Int does not conform to protocol 'sequence'这给出了一个错误,如:类型Int不符合协议“序列”

So, if you get this error in for loop, please check whether you are looping an array or not.因此,如果您在 for 循环中遇到此错误,请检查您是否在循环数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM