Xcode 7,swift: Type of expression ambiguous without more context

let regex = NSRegularExpression(pattern: mailPattern,options: .CaseInsensitive, error: nil)

这个错误字面意思是有歧义,那就使用全称吧

NSRegularExpressionOptions.CaseInsensitive 



更新xcode7以后的swift2.0已经不使用error:&NSError这种形式来处理错误了; Cocoa框架里面所有这个形式的函数都会在swift2.0中标记为 Throws ,意思是会抛出错误,要使用新的错误处理机制处理;


第一种方法,使用do-try-catch 处理错误

do{
    let regex = try NSRegularExpression(pattern: "((((((", options: NSRegularExpressionOptions.CaseInsensitive  )
}
catch
{
    print(error)
}

第二种方法,如果你认为你的代码不会有任何错误  使用try!告诉编译器

 let regex = try! NSRegularExpression(pattern: "((((((", options: NSRegularExpressionOptions.CaseInsensitive )

注意噢 , 如果使用try! 时出错应用会直接崩溃

温馨提示:答案为网友推荐,仅供参考