我试图在ViewController类中调用在视图模型类中编写的一个函数。但是不幸的是,该函数没有被调用。我没有弄错我在哪里。如果有人帮助我,那会很棒。谢谢!
//ViewController Class
import UIKit
class getDetailsViewController: UIViewController {
var getviewmodel: getDetaisViewModel?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func getDetailsAction(_ sender: Any) {
getviewmodel?.getdetailscall()
}
}
//ViewModel Class
import Foundation
import Alamofire
import SwiftyJSON
class getDetaisViewModel{
var url: URL!
func getdetailscall(){
let headers:HTTPHeaders = ["Authorization":"application/json"]
print(headers)
url = URL(string: Constants.apiForGetDetails)
print(url)
let vc = getDetailsViewController.init(nibName: "getDetailsViewController", bundle: nil)
getWebService.requestService(url: url, method: .post, headers: headers, showLoaderFlag: true, viewController: vc, completion: { response in
guard response["code"].int != 503 else {
print("No internet connection")
return
}
guard response != .null else {
return
}
})
}
}
答案 0 :(得分:2)
因为您尚未初始化getviewmodel
对象
更改
var getviewmodel: getDetaisViewModel?
到
var getviewmodel = getDetaisViewModel()
或通过getviewmodel
方法初始化viewDidLoad
对象
override func viewDidLoad() {
super.viewDidLoad()
self.getviewmodel = getDetaisViewModel()
}
将您的班级名称更改为GetDetaisViewModel