All you need is to iterate through your roman string characters in a reverse way and map those characters values. Start with maxValue equal to zero, switch the letters value, save it as the maxValue and if the value is equal to maxValue add it otherwise subtract the actual value. You can also use regex (strict or not) to validate and throw an error in case it fails. Try like this:
Xcode 9.x • Swift 4.x
Note: for Swift 3 version or earlier check the edit history.
extension String {
enum RomanParsingError: Error {
case invalidNumber
}
func romanNumeralValue() throws -> Int {
guard range(of: "^(?=[MDCLXVI])M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$", options: .regularExpression) != nil else {
throw RomanParsingError.invalidNumber
}
var result = 0
var maxValue = 0
uppercased().reversed().forEach {
let value: Int
switch $0 {
case "M":
value = 1000
case "D":
value = 500
case "C":
value = 100
case "L":
value = 50
case "X":
value = 10
case "V":
value = 5
case "I":
value = 1
default:
value = 0
}
maxValue = max(value, maxValue)
result += value == maxValue ? value : -value
}
return result
}
}
usage:
do {
let decimal = try "MCMLXXVIII".romanNumeralValue()
print(decimal) // 1978
} catch {
print(error)
}
do {
let decimal = try "IIIV".romanNumeralValue()
print(decimal)
} catch {
print(error) // "invalidNumber\n"
}