我正在尝试为http get请求编写协议,并在我的视图控制器中使用它。这个想法是为了防止在每个VC中写入请求并在应用程序中写入一次。我用get函数编写了一个协议,并为它编写了一个扩展,以使用Alamofire实现get请求。
protocol Irequest
{
func getRequest(url: String, completion: (JSON) -> ())
func postRequest(url: String, param: Parameters, completion: (JSON) -> ())
}
extension Irequest
{
func getRequest(url: String, completion:@escaping (JSON) -> ())
{
Alamofire.request(url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!, method: .post)
.responseJSON { response in
debugPrint(response)
if let Json = response.result.value
{
let j = JSON(Json)
completion(j)
}
else
{
completion(JSON("{}"))
}
}
}
}
现在,当我想在课堂上使用它时,它说X类不符合协议Irequest。我怎么用呢?在更一般的术语中,我如何在所有应用程序中都有一个获取请求功能?
class X : UIViewController , Irequest
答案 0 :(得分:1)
1。 协议的get方法签名是!=扩展的get签名。一个没有@escaping,一个没有。
==>方法不匹配,ergo get不存在
2。 邮件根本没有实施
==>错误的方法
从你已经知道的评论2中解决问题1,你应该做得很好:)