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