Your code is structured incorrectly.
URLSession
creates tasks that are run asynchronously. You set up a task, and either pass in a completion block, or set up a delegate.
The task.resume() call returns immediately, long before the network download is complete.
Once the task is complete, the system calls your completion handler (or your delegate, if you use the delegate style).
Beware that URLSessions' completion handlers and delegate calls are done on a background thread. If you do any UIKit calls in response to a task completing, you need to do it on the main thread.
As @keithbhunter says in his comment, you need to put the call to your completion handler inside the completion handler for your task. It's probably safest if you wrap that whole completion handler call in a call to the main thread:
func startConnection(completion: (NSArray, Int) -> Void) {
let url = URL(string: "http://www.example.com/path")
var request : URLRequest = URLRequest(url: url!)
request.httpMethod = "POST"
let postString = "a=(Int(teamInput.text!)!)"
request.httpBody = postString.data(using: .utf8)
let dataTask = URLSession.shared.dataTask(with: request) {
data,response,error in
print("anything")
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
self.teamResult = jsonResult
print(jsonResult)
//Use GCD to invoke the completion handler on the main thread
DispatchQueue.main.async() {
completion(NSArray(object: teamResult), Int(teamInput.text!)!)
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
dataTask.resume()
}
Note that your force-unwrapping of teamInput.text is very fragile, and will crash if teamInput.text is nil, or if it can't be converted to an Int. You'd be much better off to write your completion handler to take optionals for both the data and the int value you get back from teamInput.text:
func startConnection(completion: (NSArray?, Int?) -> Void) {
and call it passing in an optional:
let value: Int? = teamInput.text != nil ? Int(teamInput.text!) : nil
completion(NSArray(object: teamResult), value)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…