gpt4 book ai didi

ios - 错误 : "Type ' *' does not conform to protocol ' *'" using custom class and NSURLSession

转载 作者:行者123 更新时间:2023-11-29 01:54:46 25 4
gpt4 key购买 nike

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.

我真的不知道,我做错了什么。我想将我的代码划分为类,以便我可以将我的“后端”与我的“前端”分开(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 文件中“导入”协议(protocol):

class SeriesInfoViewController: UIViewController, UIScrollViewDelegate, OMDbApiControllerProtocol

这会产生错误:类型“SeriesInfoViewController”不符合协议(protocol)“OMDbApiControllerProtocol”

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

最佳答案

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

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

关于ios - 错误 : "Type ' *' does not conform to protocol ' *'" using custom class and NSURLSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31004836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com