Upload files with completion handler

import Cocoa

class uploadToServer: NSObject {
   
    func upload() {
        let config = URLSessionConfiguration.default
        let url = URL(string: "http://localhost:8042/instances")
        let request = NSMutableURLRequest(url: url!)
        request.httpMethod = "POST"
        request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
        let folder = Bundle.main.path(forResource: "dicom", ofType: "")!         
        for i in 1...10 {  
            let session = URLSession(configuration: config)
            let data = NSData(contentsOfFile: folder + "/" + String(i) + ".dcm")
            let task = session.uploadTask(with: request as URLRequest, from: data as Data?){
                data, response, error -> Void in
                    if error != nil {  
                        print("error = \(error!)")
                    } else {
                        print(i)
                    }  // end if
                }  // end task
            task.resume()
        }  // end for
    }  // end func
    
}  // end class