我具有以下功能:
CourseSettingsNetManager.sharedInstance.GetCourseEnrollment(courseId: (course?.id)!, controler: self) {
enrollment, error in
if(enrollment != nil) {
self.getSlotId()
self.getSlotsPerCourse(variantId: (enrollment?.variantId)!)
} else if (error != nil) {
print("Log: " + String(describing: error))
}
}
GetCourseEnrollment的代码为:
func GetCourseEnrollment(courseId: Int, controler: UIViewController, completionHandler: @escaping (CourseEnrollment?, NSError?) -> ()) {
guard let url = try? Paths.sharedInstance.getCourseEnrollmentPath(courseId: String(courseId), userId: NetworkManager.sharedInstance.UserId()) else {
AlertManager.sharedInstance.alertMessage(message: "undefined_userid", controler: controler)
completionHandler(nil, NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "User ID not defined."]))
return
}
Alamofire.request(url, encoding: URLEncoding.default).responseJSON {
response in
switch response.result {
case .success :
if let result: AnyObject = response.result.value as AnyObject? {
if(response.response?.statusCode == 200){
let enrollment = CourseEnrollment(json: JSON(result))
completionHandler(enrollment, nil)
} else {
if let message = JSON(result)["message"].string {
print("Log: " + message)
}
completionHandler(nil, nil)
}
}
case .failure(let error):
completionHandler(nil, error as NSError?)
}
}
}
我在TableViewController中使用这些功能。除了当我返回使用NavigationController然后再次返回TableViewController并调用这些函数时,它的工作原理是完美的。由于某种原因,Alamofire不会返回响应,因此在调试时,它永远不会进入Alamofire请求的主体。