text = ["lorem", "ipsum", "dolor", "sit", "amet"]
after random:
wrongText = ["lorem", "ipsum", "amet", "sit", "dolor"]
How do i know which word was misplaced ? In example above i need only "amet" and "dolor"
text = ["lorem", "ipsum", "dolor", "sit", "amet"]
after random:
wrongText = ["lorem", "ipsum", "amet", "sit", "dolor"]
How do i know which word was misplaced ? In example above i need only "amet" and "dolor"
Try something like:
let changed = zip(text,wrongText).filter { $0 != $1 }.map { $0.0 }
This will pair the elements of both arrays and output the non-matching pairs. The map
will extract the first word of each non-matching pair, since you'll only need that.
var changed = [String]()
for i in 0..<text.count {
if text[i] != wrongText[i]{
changed.append(wrongText[i])
}
}
A shorter version that takes advantage of flatMap
as nil filter:
let changed = zip(text, wrongText).flatMap { $0 != $1 ? $0 : nil }