从 do-catch 语句返回字符串

Returning String from a do-catch statement

我试图将代码从 swift 2 翻译成 swift 4,但遇到了这个错误

Errors thrown from here are not handled

所以我这样做了,但现在它告诉我 return 一个字符串。知道怎么做吗?

func formatSentence(sentence:String) -> String
{
    do {
        let regex = try NSRegularExpression(pattern: "\W+", options: .caseInsensitive)
        let modifiedString = regex.stringByReplacingMatches(in: sentence, options: [], range: NSRange(location: 0,length: sentence.count), withTemplate: "")

    } catch {
        print(error)
    }

    //I tried adding it here the return modifiedString but gives me error
}

这是原始函数的样子

func formatSentence(sentence:String) -> String
{
    let regex = NSRegularExpression(pattern: "\W+", options: .caseInsensitive)//NSRegularExpression(pattern:"\W+", options: .CaseInsensitive, error: nil)
    let modifiedString = regex.stringByReplacingMatches(in: sentence, options: [], range: NSRange(location: 0,length: sentence.count), withTemplate: "")

    return modifiedString
}

在函数开头设置默认值,如下所示:

func formatSentence(sentence:String) -> String {
   var regex = ""
   var modifiedString = "" 

   do {
       regex = try NSRegularExpression(pattern: "\W+", options: .caseInsensitive)
       modifiedString = regex.stringByReplacingMatches(in: sentence, options: [], range: NSRange(location: 0,length: sentence.count), withTemplate: "")

   } catch {
       print(error)
   }
   return modifiedString
}

这取决于您要如何处理错误情况。有几个选项:

  1. 你可以做到returnString?,其中nil表示有错误:

    func formatSentence(_ sentence: String) -> String? {
        do {
            let regex = try NSRegularExpression(pattern: "\W+", options: .caseInsensitive)
            let range = NSRange(sentence.startIndex..., in: sentence)
            return regex.stringByReplacingMatches(in: sentence, range: range, withTemplate: "")
        } catch {
            print(error)
            return nil
        }
    }
    

    然后你会做类似的事情:

    guard let sentence = formatSentence(string) else { 
        // handle error here
        return
    }
    
    // use `sentence` here
    
  2. 您可以将您的函数定义为 throws 如果遇到一个错误:

    func formatSentence(_ sentence: String) throws -> String {
        let regex = try NSRegularExpression(pattern: "\W+", options: .caseInsensitive)
        let range = NSRange(sentence.startIndex..., in: sentence)
        return regex.stringByReplacingMatches(in: sentence, range: range, withTemplate: "")
    }
    

    然后你会在调用点发现错误:

    do {
        let sentence = try formatSentence(string)
    
        // use `sentence` here
    } catch {
        // handle error here
        print(error)
    }
    
  3. 或者,假设您知道您的模式是有效的,您可以使用 try! 知道它不会失败:

    func formatSentence(_ sentence: String) -> String {
        let regex = try! NSRegularExpression(pattern: "\W+", options: .caseInsensitive)
        let range = NSRange(sentence.startIndex..., in: sentence)
        return regex.stringByReplacingMatches(in: sentence, range: range, withTemplate: "")
    }
    

    然后你可以这样做:

    let sentence = formatSentence(string)
    

    只有当您以 100% 的信心知道 NSRegularExpression 不会因您的正则表达式模式(例如在这种情况下)而失败时,才使用最后一个模式。


顺便说一句,您可能会快刀斩乱麻,只需将 replacingOccurrences.regularExpression 选项一起使用即可:

func formatSentence(_ sentence: String) -> String {
    return sentence.replacingOccurrences(of: "\W+", with: "", options: .regularExpression)
}