繁体   English   中英

错误:使用自定义类和NSURLSession的“类型'*'不符合协议'*'”

[英]Error: “Type '*' does not conform to protocol '*'” using custom class and NSURLSession

我知道有很多问题和答案does not conform to protocol错误does not conform to protocol ,但是没有任何匹配和/或解决我的特定问题。

我真的不知道我在做什么错。 我想将代码分成多个类,以便可以将“后端”与“前端”(ViewController.swift)分开。

在这种情况下,我需要一个在后台处理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()
        }
    }

}

然后,将协议“导入”到ViewController.swift文件中:

class SeriesInfoViewController: UIViewController, UIScrollViewDelegate, OMDbApiControllerProtocol

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

希望您能理解我的问题,并请您慎重考虑,这是我对堆栈溢出的第一个询问。

我认为您有点误解了应如何使用您的协议。 通过“导入”协议,您表示您的类实际上符合该协议。 在这种情况下,这意味着SeriesInfoViewController必须实现didReceiveOMDbResults才能真正符合协议。

因此,如果实现该方法,则应该没问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM