Tapas Pal Tapas Pal - 1 month ago 16
Swift Question

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
}
}

Answer

In swift 3 you can only use protocols to constrain your extension because it's a generic, so extension Array where Element: IndexPath { won't work because IndexPath isn't a protocol.

In swift 3.1 this is changing (see here) so you'll be able to use a specific type with a slightly different syntax:

extension Array where Element == IndexPath {

If you can't move to swift 3.1 yet then you can shuffle around this by creating a protocol, something like:

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

extension IndexPath: Indexable {}

extension Array where Element: Indexable {
    ...
Comments