"Generic parameter 'Element' could not be inferred" swift 中的错误...?

"Generic parameter 'Element' could not be inferred" error in swift...?

我正在使用以下 class,它有提取字符串中所有电子邮件的方法,我是 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
    }   
}

因此,您的区块要求 NSTextCheckingResult!,但签名需要 NSTextCheckingResult?。如果将块更改为 usingBlock: { (result: NSTextCheckingResult?, _, _) in,编译器将静音。

虽然我不知道为什么编译器会给出该错误。