Я бы предложил использовать Set вместо Array.У него есть метод go substracting
, который легко обрабатывает ваши данные.
//: Playground - noun: a place where people can play
import UIKit
var previousStr = """
[
{
"current_location" : [
90.458456400000003,
23.746056500000002
],
"vehicle_type" : "BIKE",
"_id" : "5a8fd7e50ed19875687dcf8c"
},
{
"current_location" : [
90.3727272,
23.8216228
],
"vehicle_type" : "BIKE",
"_id" : "5a97f48edf192f6e54725c78"
},
{
"current_location" : [
90.397113300000001,
23.778111200000001
],
"vehicle_type" : "BIKE",
"_id" : "5a980243df192f6e54725c85"
},
{
"current_location" : [
90.379659399999994,
23.7221121
],
"vehicle_type" : "BIKE",
"_id" : "5a9a66586f27706a7a10783a"
},
{
"current_location" : [
90.400972899999999,
23.872647400000002
],
"vehicle_type" : "BIKE",
"_id" : "5a9fff2e31eb895bc79f7ee0"
}
]
"""
var str = """
[
{
"current_location" : [
90.458456400000003,
23.746056500000002
],
"vehicle_type" : "BIKE",
"_id" : "5a8fd7e50ed19875687dcf8c"
},
{
"current_location" : [
90.3727272,
23.8216228
],
"vehicle_type" : "BIKE",
"_id" : "5a97f48edf192f6e54725c78"
},
{
"current_location" : [
90.397113300000001,
23.778111200000001
],
"vehicle_type" : "BIKE",
"_id" : "5a980243df192f6e54725c85"
},
{
"current_location" : [
90.379659399999994,
23.7221121
],
"vehicle_type" : "BIKE",
"_id" : "5a9a66586f27706a7a10783a"
},
{
"current_location" : [
90.400972899999999,
23.872647400000002
],
"vehicle_type" : "BIKE",
"_id" : "5a9fff2e31eb895bc79f7ee0"
}
]
"""
struct Vehicle: Decodable, Hashable {
let current_location: [Double]
let vehicle_type: String
let _id: String
var hashValue: Int {
return _id.hashValue
}
}
let array: [Vehicle] = try! JSONDecoder().decode([Vehicle].self, from: str.data(using: .utf8)!)
let set = Set<Vehicle>(array)
let previousArray: [Vehicle] = try! JSONDecoder().decode([Vehicle].self, from: previousStr.data(using: .utf8)!)
let previousSet = Set<Vehicle>(previousArray)
print(previousSet)
let added = set.subtracting(previousSet)
let lost = previousSet.subtracting(set)
print(added)
print(lost)
Возвращает это:
[__lldb_expr_63.Vehicle (current_location: [90.3727272, 23.8216228], vehicle_type): "ВЕЛОСИПЕД", _id: "5a97f48edf192f6e54725c78"), __lldb_expr_63.Vehicle (CURRENT_LOCATION: [+90,397113300000001, +23,778111200000001], vehicle_type: "ВЕЛОСИПЕД", _id: "5a980243df192f6e54725c85"), __lldb_expr_63.Vehicle (CURRENT_LOCATION: [90,379659399999994, 23,7221121], vehicle_type: "ВЕЛОСИПЕД", _id: "5a9a66586f27706a7a10783a"), __lldb_expr_63.Vehicle (CURRENT_LOCATION: [+90,400972899999999, +23,872647400000002], vehicle_type: "ВЕЛОСИПЕД", _id: "5a9fff2e31eb895bc79f7ee0"), __lldb_expr_63.Vehicle (CURRENT_LOCATION: [90,458456400000003, +23,746056500000002], vehicle_type: "BIKE", _id: "5a8fd7e50ed19875687dcf8c")] добавлено >>>>> [__lldb_expr_63.Vehicle (current_location: [90.400972899999999, 23.872647400000002], тип транспортного средства: "BIKE", _ff2ef0b5b5a5 )5 )5 )5 )5):__lldb_expr_63.Vehicle (current_location: [90.400972899999999, 23.872647400000002], тип_транспорта: "BIKE", _id: "5a9fff2e31eb895bc79f7ee0")]
В вашем случае вы можете легко сохранять предыдущие данные, используя let previousSet = set
.