Using NSURLSession from a Swift command line program

后端 未结 3 1114
星月不相逢
星月不相逢 2020-12-08 01:17

I\'m trying to test a little proof-of-concept command line app prior to integrating it into a larger app. What I\'m trying to do is download some data using NSURLSession usi

3条回答
  •  无人及你
    2020-12-08 01:20

    Try this

    let sema = DispatchSemaphore( value: 0)
    
    let url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_November_2010-1a.jpg")!;
    
    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
      print("after image is downloaded");
      sema.signal(); // signals the process to continue
    };
    
    task.resume();
    sema.wait(); // sets the process to wait
    

提交回复
热议问题