{"id":9182,"date":"2022-02-22T20:46:19","date_gmt":"2022-02-22T20:46:19","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=9182"},"modified":"2022-02-28T15:20:35","modified_gmt":"2022-02-28T15:20:35","slug":"creating-a-console-app-in-swift-on-linux","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-a-console-app-in-swift-on-linux\/","title":{"rendered":"Creating a console app. in Swift (on Linux)"},"content":{"rendered":"<p>The first thing we need to do is use the Swift CLI to generate the project etc. so run<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nswift package init --type executable\r\n<\/pre>\n<p>This will generate Sources folder, Tests, the Package.swift and even a README.md and .gitignore.<\/p>\n<p>Now in the previous post we created a simple <em>hello world<\/em> Vapor REST server, so let&#8217;s write the code to call that from our console app. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nimport Foundation\r\n#if canImport(FoundationNetworking)\r\nimport FoundationNetworking\r\n#endif\r\n\r\nlet session = URLSession.shared\r\nlet url = URL(string: &quot;http:\/\/192.168.1.1:8080&quot;)\r\n\r\nlet semaphore = DispatchSemaphore(value: 0)\r\n\r\nsession.dataTask(with: url!) { (data, response, error) in\r\n    let result = String.init(data: data!, encoding: .utf8)\r\n    print(result!)\r\n    semaphore.signal()\r\n}.resume()\r\n\r\nsemaphore.wait()\r\n<\/pre>\n<p>Let&#8217;s break down this code&#8230;<\/p>\n<p>The first bit of interest is the import. We need FoundationNetworking for the <em>URLSession.shared<\/em> within the Linux implementation (if I understand correctly) but not in the Mac OS version (I need to verify that). To allow us to only import FoundationNetworking if required, we use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n#if canImport(FoundationNetworking)\r\nimport FoundationNetworking\r\n#endif\r\n<\/pre>\n<p>As you&#8217;ve probably surmised <em>URLSession<\/em> is a shared resource, hence the <em>shared<\/em> singleton property. <\/p>\n<p>Next we need the URL, which is pretty explanatory. Now the URLSession calls are asynchronous, hence if we just let this code run without the semaphore we&#8217;ll exit the application before the response from the web service returns. <\/p>\n<p>The <em>resume<\/em> method is required to start the task (seems the wrong name to me, but there you go). See <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlsession\/1407613-datatask\" rel=\"noopener\" target=\"_blank\">dataTask<\/a>.<\/p>\n<p>Finally we wait on the signal from the semaphore. Within the actual <em>dataTask<\/em> we&#8217;re just going to assume there&#8217;s data and no error (ofcourse in a real world application you&#8217;ll want to add a guard etc. to check for such things and also check the optionals actual have some value before unwrapping). <\/p>\n<p>In this simple example we&#8217;ll simply assuming all worked, unwrap the data and print it to the console. Ofcourse the data will be encoded, so we&#8217;re need to create a string from the data and the required encoding as per the <em>String.init<\/em>.<\/p>\n<p>Now just run the command<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nswift run\r\n<\/pre>\n<p>If all went well you&#8217;ll see the root level response from the web server code we wrote in the last post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first thing we need to do is use the Swift CLI to generate the project etc. so run swift package init &#8211;type executable This will generate Sources folder, Tests, the Package.swift and even a README.md and .gitignore. Now in the previous post we created a simple hello world Vapor REST server, so let&#8217;s write [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9182","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9182"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9182\/revisions"}],"predecessor-version":[{"id":9210,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9182\/revisions\/9210"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}