简体   繁体   English

符合Swift中的协议

[英]Conforming to Protocols in Swift

I am new to Swift development and am struggling while trying to do some tutorials (I know, they are step by step, but apparently I must have messed up somewhere along the line and now I can't figure out for the life of my why it isn't working. I am not very good at debugging and lack a solid understanding of the foundation of the language. If anyone could help point me in the right direction I would appreciate it. 我是Swift开发的新手,正在尝试做一些教程时很挣扎(我知道,它们是一步一步来的,但是显然我一定已经搞砸了,现在我无法弄清楚为什么要这么做了我不是很擅长调试,并且对语言的基础缺乏扎实的理解,如果有人可以帮助我指出正确的方向,我将不胜感激。

I've tried commenting things out to try and get it as "bare bones" as possible while still being able to "conform" but I am pretty stuck at the moment. 我尝试注释掉某些东西,以使其尽可能“裸露”,同时仍然能够“整合”,但目前我还很固执。

Here is the code: 这是代码:

Error received on this class: 此类收到错误:

final class ForecastAPIClient : APIClient {

    let configuration: NSURLSessionConfiguration
    lazy var session : NSURLSession = {
        return NSURLSession(configuration: self.configuration)
    }()

    private let token : String


    init(config: NSURLSessionConfiguration, APIKey: String){
        self.configuration = config
        self.token = APIKey
    }

    convenience init(APIKey: String){
        self.init(config: NSURLSessionConfiguration.defaultSessionConfiguration(), APIKey: APIKey)
    }

    func fetchCurrentWeather(coordinate: Coordinate, completion : APIResult<CurrentWeather> -> Void){
        let request = Forecast.Current(token: self.token, coordinate: coordinate).request
    }

}

trying to conform to this protocol: 尝试遵守此协议:

protocol APIClient{

    var configuration : NSURLSessionConfiguration { get }
    var session : NSURLSession { get }

    init(withConfig: NSURLSessionConfiguration)

    func JSONTaskWithRequest(request: NSURLRequest, completion: JSONTaskCompletion) -> JSONTask

    func fetch<T>(request: NSURLRequest, parse: JSON -> T?, completion: APIResult<T> -> Void)

}

Sorry if this is already answered.. I searched and read through some other posts, but I still wasn't able to figure it out. 抱歉,如果已经回答了。.我搜索并阅读了其他一些帖子,但仍然无法弄清楚。

Thanks in advance! 提前致谢!

It looks like your class isn't implementing two of the methods from the protocol: 看来您的课程没有实现协议中的两个方法:

func JSONTaskWithRequest(request: NSURLRequest, completion: JSONTaskCompletion) -> JSONTask

and

func fetch<T>(request: NSURLRequest, parse: JSON -> T?, completion: APIResult<T> -> Void)

Also, your class has an initializer with this signature: 另外,您的类具有带有此签名的初始化程序:

init(config: NSURLSessionConfiguration, APIKey: String)

When the protocol requires: 协议要求时:

init(withConfig: NSURLSessionConfiguration)

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

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