Make use of components(separatedBy:) and compactMap.
let str = "5~895893799,,6~898593679,,7~895893679,,8~895893799,,5~895893799,,6~898593679,,7~895893679,,8~895893799"
let nums = str.components(separatedBy: ",,")
.compactMap { $0.components(separatedBy: "~").first }
That gives the string array:
["5", "6", "7", "8", "5", "6", "7", "8"]
If you want an array of Int, add:
.compactMap { Int($0) }
to the end.
let nums = str.components(separatedBy: ",,")
.compactMap { $0.components(separatedBy: "~").first }
.compactMap { Int($0) }
That gives:
[5, 6, 7, 8, 5, 6, 7, 8]