从do-catch语句返回String

时间:2018-01-18 02:39:00

标签: swift do-catch

我试图将代码从swift 2转换为swift 4并遇到此错误

  

不处理从此处抛出的错误

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

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
}

2 个答案:

答案 0 :(得分:6)

这取决于您希望如何处理错误情况。有几个选择:

  1. 您可以将其返回String?,其中nil表示存在错误:

    func formatSentence(_ sentence: String) -> String? {
        do {
            let regex = try NSRegularExpression(pattern: "\\W+", options: .caseInsensitive)
            let range = NSRange(sentence.startIndex ..< sentence.endIndex, 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 ..< sentence.endIndex, 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 ..< sentence.endIndex, in: sentence)
        return regex.stringByReplacingMatches(in: sentence, range: range, withTemplate: "")
    }
    

    然后你可以这样做:

    let sentence = formatSentence(string)
    

    如果您100%放心地知道NSRegularExpression在您的正则表达式模式下(例如在这种情况下)不能失败,那么您只使用最后一个模式。

  4. 顺便说一下,你可能会切断Gordian结,只需使用replacingOccurrences选项.regularExpression

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

答案 1 :(得分:-1)

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

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
}