You can compare properties of elements within a dataModel if they are inherently comparable/equatable, like String or Int. If you want to be able to test equality for specific elements within the dataModel array itself, then you will have to make your dataModel conform to Equatable.
I believe the problem you are facing has to do with making your dataModel class or struct conform to Equatable, or with how you are populating data and compareData.
This basic example should give you an idea of how to do it. It tests equality for different portions of the data (smaller and larger entities):
struct dataModel: Equatable {
let id: Int
let name: String
}
var data : [dataModel] = [dataModel]()
var compareData : [dataModel] = [dataModel]()
data = [
dataModel(id: 5, name: "five"),
dataModel(id: 3, name: "three")
]
compareData = [
dataModel(id: 5, name: "five"),
dataModel(id: 2, name: "three")
]
if data[0].id == compareData[0].id {
print("First item ID is equal")
}
if data[0].name == compareData[0].name {
print("First item name is equal")
}
if data[0] == compareData[0] {
print("First item of both data and compareData are equal")
}
if data == compareData {
print("Data Arrays are equal")
} else {
print("Data Arrays are not equal")
}
I saw that you just showed the code for your DataModel (class should usually be capitalized) so here's a more customized example using your actual implementation of the class:
class DataModel: Equatable {
static func == (lhs: DataModel, rhs: DataModel) -> Bool {
guard lhs.aboutTeams == rhs.aboutTeams else {return false}
guard lhs.messageID == rhs.messageID else {return false}
guard lhs.teamName == rhs.teamName else {return false}
return true
}
var teamName : String = ""
var aboutTeams : String = ""
var messageID : String = ""
}
var data : [DataModel] = [DataModel]()
var compareData : [DataModel] = [DataModel]()
data = [
DataModel(),
DataModel()
]
data[0].teamName = "raptors"
data[0].messageID = "5"
compareData = [
DataModel(),
DataModel()
]
compareData[0].teamName = "raptors"
compareData[0].messageID = "4"
if data[0].teamName == compareData[0].teamName {
print("Element 0 team names are equal")
} else {
print("Element 0 team names are not equal")
}
if data[0].messageID == compareData[0].messageID {
print("Elemeant 0 message IDs are equal")
} else {
print("Elemeant 0 message IDs are not equal")
}
if data[1] == compareData[1] {
print("Element 1 of data and compareData are equal")
}
if data == compareData {
print("Arrays are equal")
} else {
print("Arrays are not equal")
}
The console output is:
Element 0 team names are equal
Elemeant 0 message IDs are not equal
Element 1 of data and compareData are equal
Arrays are not equal
Hope this helps!