在命令行工具上使用Swift URLSession示例代码

时间:2017-09-20 18:39:03

标签: swift nsurlsession foundation

我试图找出最简单的方法,从命令行在Swift 4中发出HTTP请求。我从URLSession programming guide复制了这段代码,并添加了几个打印语句。我无法弄清楚为什么print("Testing URLSession") let sessionWithoutADelegate = URLSession(configuration: URLSessionConfiguration.default) if let url = URL(string: "https://www.example.com/") { print("Encoded url \(url)") (sessionWithoutADelegate.dataTask(with: url) { (data, response, error) in print("Executing dataTask") if let error = error { print("Error: \(error)") } else if let response = response, let data = data, let string = String(data: data, encoding: .utf8) { print("Response: \(response)") print("DATA:\n\(string)\nEND DATA\n") } }).resume() } 没有执行。

GET

目标是从REST API中检索数据,但我甚至无法使一个简单的executeAsync()请求正常工作......

1 个答案:

答案 0 :(得分:3)

我终于找到了如何使用public class test2{ public static class Node<T>{ public T data; public Node<T> next; public Node(T data, Node<T> next) { this.data = data; this.next = next; } } static Node[] array = new Node[10]; public static void add(String word){ int position = hashFunction(word); if(array[position] == null){ array[position] = new Node(word, null); }else{ new Node(word, array[position]); } } public static int hashFunction(String a){ int sum = 1; for(int i = 0; i<a.length(); i++){ char b = a.charAt(i); int value = (int) b; sum *= value; } return sum % array.length; } public static void main(String[] args) { add("abc"); add("acb"); add("bca"); add("bac"); add("cba"); System.out.println(array[4].next); } }

使其工作
CFRunLoop