I have an array of strings that contains a time pattern (e.g '30 min'):
let arrayOfStrings: [String] = ["Episode 1 - 23 min", "Episode 2 - 42 min", "Episode 3 - 45 min"]
func convertToTimeInterval(from string: String) -> TimeInterval {
// logic here...
return timeInterval // Double
}
So using such a method, I want from the results:
arrayOfStrings.forEach { string in
print(convertToTimeInterval(from: string))
}
// Prints
1380 // timeinterval from "23 min"
2520 // timeinterval from "42 min"
2700 // timeinterval from "45 min"
- I have tried splitting from the the 'min' character but the problem is that it has to support multiple language format. So there is no way to know in advance the character to split from.
- I have also tried getting the
CharacterSet.separatedBybut the string can contains multiple decimal that have nothing to do with the time (e.g Episode 1)
Thanks for your help!
you could try this approach, where each string in the array is split into sections, and the minutes extracted and returned as a Double/TimeInterval.
This should (not tested) cater for multiple languages that uses
Western Arabic numerals