Error: "Type '*' does not conform to protocol '*'" using custom class and NSURLSession

Error: "Type '*' does not conform to protocol '*'" using custom class and NSURLSession

I know there are many questions and answers for this does not conform to protocolerror, but nothing matched and/or worked for my specific problem.

我真的不知道,我做错了什么。我想将我的代码分成 classes 这样我就可以将 "backend" 与 "frontend" (ViewController.swift).

分开

在这种情况下,我想要一个 class 在后台处理 API 调用,所以我创建了一个 ApiController.swift 文件,其中包含以下代码:

import Foundation


protocol OMDbApiControllerProtocol {
    func didReceiveOMDbResults(results: Dictionary<String, String>)
}

class OMDbApiController {

    var delegate: OMDbApiControllerProtocol?

    init(delegate: OMDbControllerProtocol?) {
        self.delegate = delegate
    }

    func searchOMDb(forSeries: String, season: String, episode: String) {

        let term = forSeries.stringByReplacingOccurrencesOfString(" ", withString: "+", options: .CaseInsensitiveSearch, range: nil)
        if let escapedTerm = term.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {

            let url = NSURL(string: "http://www.omdbapi.com/?s=\(escapedTerm)")
            println(url!)
            let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: {
                (data, response, error) in

                var jsonResults = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as? Dictionary<String, String>

                dispatch_async(dispatch_get_main_queue()) {
                   self.delegate?.didReceiveOMDbResults(jsonResults!)
                }

            })
            task.resume()
        }
    }

}

然后我"imported" ViewController.swift 文件中的协议:

class SeriesInfoViewController: UIViewController, UIScrollViewDelegate, OMDbApiControllerProtocol

这会产生错误: 类型 'SeriesInfoViewController' 不符合协议 'OMDbApiControllerProtocol'

I hope you understand my problem and please considerate, that this is my first asked question on Stack Overflow.

我认为您有点误解了协议的使用方式。通过 'importing' 协议,您是说您的 class 实际上符合该协议。在这种情况下,这意味着 SeriesInfoViewController 必须实现 didReceiveOMDbResults 才能真正符合协议。

因此,如果您实施该方法,您应该没问题。