Ios 在所有应用程序中写入一次get请求-swift
我正在尝试为http get请求编写一个协议,并在我的视图控制器中使用它。其想法是防止在每个VC中编写请求,并在应用程序中编写一次。我用get函数编写了一个协议,并为它编写了一个扩展,以使用Alamofire实现get请求Ios 在所有应用程序中写入一次get请求-swift,ios,swift,Ios,Swift,我正在尝试为http get请求编写一个协议,并在我的视图控制器中使用它。其想法是防止在每个VC中编写请求,并在应用程序中编写一次。我用get函数编写了一个协议,并为它编写了一个扩展,以使用Alamofire实现get请求 protocol Irequest { func getRequest(url: String, completion: (JSON) -> ()) func postRequest(url: String, param: Parameters, com
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。我如何使用它?更一般地说,我怎么能在所有应用程序中都有一个get请求功能
class X : UIViewController , Irequest
一,。
协议的get方法签名为!=分机已经签名了。一个人没有“逃跑”,一个人有
==>方法不匹配,因此不存在ergo get
二,。
post根本没有实现
==>缺少ergo方法
从你已经知道的关于2的评论来看,修复1,你应该很好:)实际上,你可以把这个函数放在全局范围内,不是吗?错误的发生是因为没有实现
postRequest
。我想UIViewController
的扩展比协议更合适。@vadian你说得对。我在协议中评论了postRequest,但仍然说它不符合协议