错误:使用自定义类和 NSURL "Type '*' does not conform to protocol '*'"



我知道这个does not conform to protocol错误有很多问题和答案,但没有任何问题与我的特定问题相匹配和/或有效。

我真的不知道,我做错了什么。我想把我的代码分成类,这样我就可以把我的"后端"one_answers"前端"(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"

我希望你能理解我的问题,请考虑周到,这是我在Stack Overflow上的第一个问题。

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

所以,如果你实现了这个方法,你应该是好的。

相关内容

最新更新