'In Xcode, code that works in Playground does not execute properly in the CommandLineTool project
I wrote a simple code that makes a GET request to the githubAPI as shown below.
import Foundation
let url = URL(string: "https://api.github.com/users/(伏字)")!
let get = URLSession.shared.dataTask(with: url,completionHandler: { data, response,error in
if let error = error {
print(error.localizedDescription)
return
}
guard let data = data,
let response = response as? HTTPURLResponse else {
print("data or response is nil")
return
}
if response.statusCode == 200 {
print(String(data: data, encoding: .utf8))
} else {
print("statusCode: \(response.statusCode)")
}
}
)
get.resume()
When this code is executed in Playground, the value successfully obtained is returned, but when CommandLineTool is selected when creating a Project, it is executed without error but returns no value. (Even if it is not able to GET, something should be Printed, but it only shows "program ended with exit code: 0".) Solution please.
Is there something I have to do only when I choose CommandLineTool???
Xcode 13.3
macOS 12.3.1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
