-1

I am trying to parse an array of items, each of which is structured like below:

[0x6100000b2480 - Message: Phlare, ID: 92329382, Sender: 2077303954, Timestamp: 1505263276721]

But I am just trying to get the value after "Sender:" so in this case "2077303954". But I will be iterating through a list of these, parsing the value out of each array index.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
dgelinas21
  • 641
  • 3
  • 9
  • 22

1 Answers1

1

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: ",").

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215