1

this function is supposed to just get a substring but I can't seem to quite get it.

I've tried pretty much all combinations of types but nothing seems to work

Code Causing the error:

let final = temp.substring(with: 9..<11)

all the functions code:

func decodeInput(update: String) -> String {
    var temp = String(describing: update)
    
    
    let final = temp.substring(with: 9..<11)
    
    return String(temp)
}
Rshelver
  • 49
  • 5

1 Answers1

1

This is because String collection is not indexed by integer. You need to use String.Index instead. You should also make sure your string has at least 11 characters and use the final instead of temp when returning the result. Btw substring with range is deprecated you should use subscript:

func decodeInput(update: String) -> String? {
    guard update.count > 10 else { return nil }
    let start = update.index(update.startIndex, offsetBy: 9)
    let end = update.index(start, offsetBy: 2)
    return String(update[start..<end])
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571