Try having a look at this answer
Your ==
method should not be added in an extension
, it should be added globally. Even though it might seem to be a static
method, it should still be declared as a global function. You can find a discussion about the details here.
Now...if you had declared your Post
as a struct
or a class
then yes, you could have added a static ==
method inside your struct/class
. However, you have declared a protocol
and a protocol
can not have any methods.
This answer shows you how to have a protocol
implement Equatable
.
Armed with all that we can implement your Post protocol
and have it implement Equatable
like so:
protocol Post: Equatable {
var referenceIndex: Int { get set}
var likeCount: Int { get set}
var likeStatus: Bool { get set}
var commentCount: Int { get set}
var commentStatus: Bool { get set}
}
func ==<T : Post>(lhs: T, rhs: T) -> Bool {
return lhs.referenceIndex == rhs.referenceIndex
}
And then, to prove that things are working:
struct SomePost: Post {
var referenceIndex: Int
var likeCount: Int
var likeStatus: Bool
var commentCount: Int
var commentStatus: Bool
}
let somePost1 = SomePost(referenceIndex: 1, likeCount: 1, likeStatus: true, commentCount: 1, commentStatus: true)
let somePost2 = SomePost(referenceIndex: 2, likeCount: 1, likeStatus: true, commentCount: 1, commentStatus: true)
let somePost3 = SomePost(referenceIndex: 1, likeCount: 1, likeStatus: true, commentCount: 1, commentStatus: true)
somePost1 == somePost2 //false
somePost1 == somePost3 //true
Hope that helps you