I have an array (items) and an array of dictionaries (data) in Swift:
let items = ["test", "2test"]
var data = [Model(title: "Title", number: 1, weight: 10, text: "text1", url: "http://google.com", questRepeat: false, code: "test"),
Model(title: "Title1", number: 1, weight: 10, text: "text2", url: "http://google.com", questRepeat: false, code: "1test"),
Model(title: "Title2", number: 1, weight: 10, text: "text3", url: "http://google.com", questRepeat: false, code: "2test")]
I'd like to create an array that contains those arrays of dictionaries whose "code" don't equals to the numbers provided in the 'items' array. Aka I'd like to end up with having an array of:
var result = [
Model(title: "Title1", number: 1, weight: 10, text: "text2", url: "http://google.com", questRepeat: false, code: "1test")
]
They seem insanely complex for this task.
Is there a clever way to do this using predicates or something else?
CodePudding user response:
By id
I think you mean the code
property in your Model
as this is the only data that seems to match what is in your items
array.
You could do something like this with filter
on the data
array
let items = ["test", "2test"]
var data = [Model(title: "Title", number: 1, weight: 10, text: "text1", url: "http://google.com", questRepeat: false, code: "test"),
Model(title: "Title1", number: 1, weight: 10, text: "text2", url: "http://google.com", questRepeat: false, code: "1test"),
Model(title: "Title2", number: 1, weight: 10, text: "text3", url: "http://google.com", questRepeat: false, code: "2test")]
let result = data.filter { !items.contains($0.code) }
print(result)
This should give you:
[Model(title: "Title1", number: 1, weight: 10, text: "text2", url: "http://google.com", questRepeat: false, code: "1test")]