Assuming ",
" only appears between values, separate them into pieces using split
:
let arr = str.characters.split(separator: ",").map(String.init)
println(arr) // [0x6100000b2480 - Message: Phlare, ID: 92329382, Sender: 2077303954]
You can then split the strings in the resulting array again on ":
" to get an array of alternating keys values.
Or perhaps the input was already an array?
Here's an example:
var resultingDictionary = [String:String]()
var stringArray = ["0x6100000b2480 - Message: Phlare", "ID: 92329382", "Sender: 2077303954", "Timestamp: 1505263276721:"]
for eachString in stringArray
{
let arr = eachString.characters.split(separator: ":").map(String.init)
resultingDictionary[arr[0]] = arr[1]
}
print("resulting dictionary is \(resultingDictionary)")
The same concept should work with NSString's componentsSeparatedByString
(now known as components(separatedBy: ",")
.