I have a string with many pipes I want to convert them into dictionary [String:Any]
how can I do that ?
Example
let input = "boolFirst=false|onProject=Active|passed=false"
Into -->
let output = [String: Any] = [boolFirst: false, onProject: "Active", passed: false]
so far tried with components(separatedBy: "|")
& components(separatedBy: "=")
Is there any cleaner way to achieve ?
2
Answers
U can try the following code.
Output Printed —
We can use the swift Regex and RegexBuilder functionality for this.
The regex would be
and then convert the result of matching the regex to a dictionary by using
reduce(into:)