I am trying to get the key/values appended to some arrays once each. So my result should be a singular occurrence of each key/value pair in my array. However, after each iteration the key/value pair is added and then the iteration restart back at the beginning again, adding the key/value pair again each time.
How can I make it only append each key/value pair once?
import UIKit
import PlaygroundSupport
var usernameScoreDict : [String:String] = ["erer":"eree", "veev":"veve", "tbtt":"bttbt", "umum":"muumu", "bvbv":"bbbcb"]
var unArray = [String]()
var hsArray = [String]()
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
usernameScoreDict.forEach { (key,value) in
print("key is - \(key) and value is - \(value)")
unArray.append(key)
hsArray.append(value)
}
}
}
You can use a for loop like this:-
for (key, val) in usernameScoreDict{
unArray.append(key)
hsArray.append(value)
}
Afterwards you can remove repeated value if any(as I don't think so it will happen) by using set:-
unArray = (Array(Set(unArray)))
hsArray = (Array(Set(hsArray)))