我正在使用以下类,它有方法来提取字符串中的所有电子邮件,我是swift的新手,它给我一个错误。有人可以解释一下为什么会出现这个错误..? 感谢
import UIKit
import Foundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
if let results = extractEmailFromString("+abc@gmail.com heyyyyy cool +def@gmail.com") {
print(results)
}
}
func extractEmailFromString(string:NSString) -> [String]? {
let pattern = "(\\+[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\\.[a-zA-Z0-9._-]+)"
let regexp = try! NSRegularExpression(pattern: pattern,
options: [.CaseInsensitive])
var results = [String]()
regexp.enumerateMatchesInString(string as String, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length:string.length), usingBlock: { (result: NSTextCheckingResult!, _, _) in
results.append(string.substringWithRange(result.range))
})
return results
}
}
答案 0 :(得分:1)
因此,您的广告块要求NSTextCheckingResult!
,但签名需要NSTextCheckingResult?
。如果将块更改为usingBlock: { (result: NSTextCheckingResult?, _, _) in
,则会使编译器静音。
我不知道为什么编译器会给出错误。