1

I want to use [String] as generic parameter.

public class Cell<T : Equatable> {}

Here is a line where I get an error:

class TagsCell : Cell<[String]>, CellType {
}

I've added this code, but it doesn't help

public func == (lhs: [String], rhs: [String]) -> Bool {
    return lhs.count == rhs.count && (zip(lhs, rhs).contains { $0.0 != $0.1 }) == false
}
Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61
  • It's not a duplicate. I've tried answers from another questions, and added some code to compare arrays and it doesn't help case [String] still doesn't conform to Equatable protocol – Semyon Tikhonenko Mar 06 '16 at 22:07
  • Please add the code block where you get the error (and indicate the line where you get it). – 0x416e746f6e Mar 06 '16 at 22:10

1 Answers1

1

Array doesn't conform to Equatable in Swift. Conforming to Equatable implies there exists == operators for this type but not vice verse. Therefore even if you implement == for [String] type, there's no way you can use Cell<[String]>.

In your case, I would suggest using the following protocol:

public class Cell<T : SequenceType where T.Generator.Element: Equatable> {}

class TagsCell : Cell<[String]> {
}

Use a wrapper out of [String]:

public class Cell<T : Equatable> {}

struct StringArray: Equatable {
    var value: [String]
}

class TagsCell : Cell<StringArray> {
}

func ==(lhs: StringArray, rhs: StringArray) -> Bool {
    return lhs.value == rhs.value
}
J.Wang
  • 1,136
  • 6
  • 12
  • It is not good to change the source of the Cell class, cause it's from external library. Is there any solution to make Swift array Equatable? – Semyon Tikhonenko Mar 07 '16 at 00:41
  • @SemyonTikhonenko As far as I know, there's no way to make Array `Equatable` that makes sense. Is it possible for you to use a wrapper out of `[String]`? – J.Wang Mar 07 '16 at 00:44