Swift 错误:“序列"需要类型“T"和“ArraySlice<T>"是等价的 [英] Swift Error: &#39;Sequence&#39; requires the types &#39;T&#39; and &#39;ArraySlice&lt;T&gt;&#39; be equivalent

查看:28
本文介绍了Swift 错误:“序列"需要类型“T"和“ArraySlice<T>"是等价的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新数学库以与 Swift 3 兼容,但遇到错误:

I'm trying to update a math library to be compatible with Swift 3, but I'm running into an error:

'Sequence' 需要类型 'T' 和 'ArraySlice'等价

Apple 关于 Sequence 的文档建议 makeIterator() 方法返回一个迭代器,它确实如此.似乎迭代器正在返回 grid 变量中的一个元素,该元素属于变量 T.我不太确定我在这里错过了什么.任何意见将是有益的.

Apple's documentation on Sequence recommends that makeIterator() method returns an iterator, which it does. And it seems that the iterator is returning an element in the grid variable, which is of variable T. I'm not quite sure what I'm missing here. Any advice would be helpful.

public struct Matrix<T> where T: FloatingPoint, T: ExpressibleByFloatLiteral {
    public typealias Element = T

    let rows: Int
    let columns: Int
    var grid: [Element]

    public init(rows: Int, columns: Int, repeatedValue: Element) {
        self.rows = rows
        self.columns = columns

        self.grid = [Element](repeating: repeatedValue, count: rows * columns)
    }
... 
}

extension Matrix: Sequence { // <-- getting error here
    public func makeIterator() -> AnyIterator<ArraySlice<Element>> {
        let endIndex = rows * columns
        var nextRowStartIndex = 0

        return AnyIterator {
            if nextRowStartIndex == endIndex {
                return nil
            }

            let currentRowStartIndex = nextRowStartIndex
            nextRowStartIndex += self.columns

            return self.grid[currentRowStartIndex..<nextRowStartIndex]
        }
    }
}

推荐答案

您的代码可以像 Swift 3.1 (Xcode 8.3.3) 一样正常编译.错误

Your code compiles fine as Swift 3.1 (Xcode 8.3.3). The error

'Sequence' requires the types 'T' and 'ArraySlice<T>' be equivalent

在编译为 Swift 4(Xcode 9,目前是测试版)时发生,因为Sequence 协议已经定义了

occurs when compiling as Swift 4 (Xcode 9, currently beta), because then the Sequence protocol already defines the

associatedtype Element where Self.Element == Self.Iterator.Element

这与您的定义相冲突.您可以选择不同的为您的类型别名命名,或者将其删除(并使用 T 代替):

which conflicts with your definition. You can either choose a different name for your type alias, or just remove it (and use T instead):

public struct Matrix<T> where T: FloatingPoint, T: ExpressibleByFloatLiteral {

    let rows: Int
    let columns: Int
    var grid: [T]

    public init(rows: Int, columns: Int, repeatedValue: T) {
        self.rows = rows
        self.columns = columns

        self.grid = [T](repeating: repeatedValue, count: rows * columns)
    }
}

extension Matrix: Sequence {
    public func makeIterator() -> AnyIterator<ArraySlice<T>> {
        let endIndex = rows * columns
        var nextRowStartIndex = 0

        return AnyIterator {
            if nextRowStartIndex == endIndex {
                return nil
            }

            let currentRowStartIndex = nextRowStartIndex
            nextRowStartIndex += self.columns

            return self.grid[currentRowStartIndex..<nextRowStartIndex]
        }
    }
}

这可以在 Swift 3 和 4 中编译和运行.

This compiles and runs with both Swift 3 and 4.

这篇关于Swift 错误:“序列"需要类型“T"和“ArraySlice<T>"是等价的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆