Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to migrate my code base to swift 3.0 using xCode. There are few issues which I am not able to understand.

Issue: Type 'Element' constrained to non-protocol type 'IndexPath'

enter image description here

In left side of error navigation panel it's shows only below errors. I am not able to understand for which line of code or branch of code causes the error.

enter image description here

Can anyone help me to solve this please.

UPDATE

After struggling a lot I am stuck at these issues.

enter image description here

UPDATE

Thank you all for your help. Now I faced only the following issues.

enter image description here

Few of you are asking to post the source code but Xcode didn't show any kind of warning or error on pages. There are few generics

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
  }
}
like image 688
Tapas Pal Avatar asked Oct 21 '16 05:10

Tapas Pal


1 Answers

You can use a specific type with a different syntax:

extension Array where Element == IndexPath {

As opposed to the more historic syntax for protocols:

extension Array where Element: Indexable {

Previously you could / had to shuffle around problems by creating a protocol, something like:

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

extension IndexPath: Indexable {}

extension Array where Element: Indexable {
    ...
like image 74
Wain Avatar answered Oct 16 '22 02:10

Wain