Jason O
Jason O

Reputation: 833

Handling errors from Swift 2.0 String object functions

I'm new to Swift 2.0 programming (coming from the world of C#), and I'm simply trying to write a function to parse some data from a string and return the result. The function itself works well, but as part of the process, the string functions may throw and error if the input string is improperly formatted and I would like to prepare a catch-all error handler for the function to deal with this. Here's the code for the original function without error handling:

// Parses the numeric value of the BATT return string
private func parseBatteeryLevel(inputStr : String) -> Int {
    if inputStr.hasPrefix("BATT") && inputStr.containsString("%") {
        let start = inputStr.startIndex.advancedBy(5)
        let end = inputStr.characters.indexOf("%")?.advancedBy(-1)

        if end != nil {
            return Int(inputStr.substringWithRange(start...end!))!
        }else{
            return 0
        }
    }else{
        printStatus("Return Value Parse Error: \"\(inputStr)\"")
        return 0
    }
}

As you can see, I did implement a basic sanity check for the strings that could be inputted but it is by no means exaustive. My primary concern is what happens if is passes the checks, but causes the advanceBy() or any of the internal string functions to fail?

I did try to wrap the code in a do-catch block as shown below,

private func parseBatteeryLevel(inputStr : String) -> Int {
    if inputStr.hasPrefix("BATT") && inputStr.containsString(" ") {
        do {
            let start = try inputStr.startIndex.advancedBy(5)
            let end = try inputStr.characters.indexOf("%")?.advancedBy(-1)

            if end != nil {
                return try Int(inputStr.substringWithRange(start...end!))!
            }else{
                return 0
            }
        } catch let error as NSError {
            printStatus(error.description)
        }
    }else{
        printStatus("Return Value Parse Error: \"\(inputStr)\"")
        return 0
    }
}

but then I get warnings, next to all the lines inside the do block, marked with the try keyword saying,

No calls to throwing functions occur within 'try' expression

Then, there is another warning next to the catch keyword saying,

'catch' block is unreachable because no errors are thrown in 'do' block

This doesn't make sense because the function clearly crashes the program if an invalid string is entered. This would be painfully simple to deal with in C# but what is the proper way to handle this in Swift 2.0?

Upvotes: 0

Views: 2149

Answers (1)

Vincent F
Vincent F

Reputation: 371

So the problem String startIndex and characters functions do not throw and exception that can be caught by the swift do try catch block. See The Swift Programming Language (Swift 2.1) - Error Handling documentation for more info.

So I rewrote the code to deal with parsing of the integer without crashing

func parseBatteeryLevel(inputStr : String) -> Int {
    if inputStr.hasPrefix("BATT") && inputStr.containsString("%") {
        let start = inputStr.startIndex.advancedBy(5)
        let indexOfPercent = inputStr.characters.indexOf("%")

        if (indexOfPercent != nil) {
            let end = indexOfPercent!.advancedBy(-1)
            let batteryLevel = inputStr.substringWithRange(start...end)

            if let level = Int(batteryLevel) {
                return level
            }
        }

        return 0

    } else {
        print("Return Value Parse Error: \"\(inputStr)\"")
        return 0
    }
}

Upvotes: 1

Related Questions