我正在研究REST API管理器.这是一个错误,我无法解决它.我得到的错误在下面突出显示.
import Foundation import Alamofire import SwiftyJSON class RestApiManager { var resources: JSON = [ "resources": [ "resourceA": [] ] ] let apiUrl: String let apiUsername: String let apiPassword: String init(apiUrl: String,apiUsername: String,apiPassword: String) { self.apiUrl = apiUrl self.apiUsername = apiUsername self.apiPassword = apiPassword getApiResourceA() { responSEObject,error in let resourceA = JSON(responSEObject!) self.resources["resources"]["resourceA"] = resourceA } } func collectDataFromApi(completionHandler: (responSEObject: NSDictionary?,error: NSError?) -> ()) { prepareHttpRequest(completionHandler) } func prepareHttpRequest(completionHandler: (responSEObject: NSDictionary?,error: NSError?) -> ()) { let alamofireRequest = Alamofire.request(.GET,"\(self.apiUrl)") alamofireRequest.authenticate(user: self.apiUsername,password: self.apiPassword) alamofireRequest.responseJSON { request,response,responSEObject,error in completionHandler(responSEObject: responSEObject as? NSDictionary,error: error) } } func getAllResources() -> JSON { return self.resources } func getApiResourceA(completion: (responSEObject: NSDictionary?,error: NSError?) -> ()) { collectDataFromApi() { responSEObject,error in completion(responSEObject: responSEObject,error: error) } } }
当我打电话给这个班级获取资源时:
override func viewDidLoad() { super.viewDidLoad() if record != nil { let url = record?.url let username = record?.username let password = record?.password let restApiManager = RestApiManager(apiUrl: url!,apiUsername: username!,apiPassword: password!) // This line seems buggy let delay = 10.0 * Double(NSEC_PER_SEC) let time = dispatch_time(DISPATCH_TIME_NOW,Int64(delay)) dispatch_after(time,dispatch_get_main_queue()) { let Resources = restApiManager.getAllResources() let ResourceA = Resources["resources"]["resourceA"] } } }
我评论的线打印:
Thread 1: breakpoint 2.1
我需要修复错误的建议.任何建议都非常感谢