4

我试图将代码从 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
}
4

2 回答 2

9

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

  1. 你可以让它 return String?,这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您的正则表达式模式下(例如在这种情况下)不会失败,则只能使用最后一种模式。


顺便说一句,您可能会斩断戈尔迪安的结,只需使用replacingOccurrenceswith.regularExpression选项:

func formatSentence(_ sentence: String) -> String {
    return sentence.replacingOccurrences(of: "\\W+", with: "", options: .regularExpression)
}
于 2018-01-18T03:03:18.997 回答
-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
}
于 2018-01-18T03:00:19.083 回答