繁体   English   中英

Swift 3.0迁移错误:键入'Element'约束为非协议类型'IndexPath'

[英]Swift 3.0 migration error: Type 'Element' constrained to non-protocol type 'IndexPath'

我正在尝试使用xCode将我的代码库迁移到swift 3.0。 我无法理解的问题很少。

问题: Type 'Element' constrained to non-protocol type 'IndexPath'

在此输入图像描述

在错误导航面板的左侧,它仅显示错误。 我无法理解哪行代码或代码分支导致错误。

在此输入图像描述

任何人都可以帮我解决这个问题。

UPDATE

经过艰苦的努力,我陷入了这些问题。

在此输入图像描述

UPDATE

感谢大家的帮助。 现在我只面临以下问题。

在此输入图像描述

很少有人要求发布源代码,但Xcode没有在页面上显示任何类型的警告或错误。 仿制药很少

private extension Array where Element: IndexPath {

    func indexOf(_ indexPath: IndexPath) -> Int {
        var counter = 0
        for object in self {
            if object.section == indexPath.section && object.row == indexPath.row {
                return counter
            }
            counter += 1
        }
        return 0
    }
}


fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

您可以使用具有不同语法的特定类型:

extension Array where Element == IndexPath {

与协议的更具历史性的语法相反:

extension Array where Element: Indexable {

以前你可以/不得不通过创建协议来解决问题,例如:

protocol Indexable {
    var row: Int { get }
    var section: Int { get }
}

extension IndexPath: Indexable {}

extension Array where Element: Indexable {
    ...

暂无
暂无

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

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