我有这段代码:
MainViewControler:
func errorLoginMessage(txt: String, title: String){
let alertController = UIAlertController(title: title, message: txt, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok".localized(), style: .cancel, handler: { (action: UIAlertAction!) in
exit(0)
}))
self.present(alertController, animated: true, completion: nil)
}
和函数AppSystem.swift中的文件:
func startUpdate(){
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
let cms = ServerConnect()
cms.getJsonProducts(completion: { (data) in
switch data {
case .succes(let data):
self.saveJsonFileToTheDiskProducts(downloadData: data)
case .error(let error):
self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
print("")
break
}
dispatchGroup.leave()
})
}
我有这个错误:
self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
:类型的价值' AppSystem'没有会员' errorLoginMessage'
有谁知道如何修复它?
答案 0 :(得分:0)
由于提醒不属于您的AppSystem
课程,因此您需要将消息传递给MainViewController
,以便在出现故障时显示消息。
您可以使用Delegates
,Notifiations
或Completion blocks
,这是completion block
的示例:
func startUpdate(completion:@escaping(Bool)->()){
// No need of DispatchGroup as pointed by Anbu.Karthik, as with completion block you are making this function work asynchronously only.
//let dispatchGroup = DispatchGroup()
//dispatchGroup.enter()
let cms = ServerConnect()
cms.getJsonProducts(completion: { (data) in
switch data {
case .succes(let data):
self.saveJsonFileToTheDiskProducts(downloadData: data)
completion(true)
case .error(let error):
completion(false)
//self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
print("")
break
}
//dispatchGroup.leave()
})
}
在MainViewController
中将其称为
startUpdate { (success) in
if !success {
//Show alert
self.errorLoginMessage(txt: "MainView - Error 101: Configuration files can not be created. \(error)", title: "Blad".localized())
}
}