{"id":867,"date":"2014-06-19T23:05:05","date_gmt":"2014-06-20T05:05:05","guid":{"rendered":"http:\/\/jamesonquave.com\/blog\/?p=867"},"modified":"2014-10-30T21:03:21","modified_gmt":"2014-10-31T03:03:21","slug":"running-swift-scripts-from-the-command-line","status":"publish","type":"post","link":"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/","title":{"rendered":"Running Swift Scripts From The Command Line"},"content":{"rendered":"<h2>Swift Scripts From The Command Line<\/h2>\n<p><i>This post updated for Xcode 6.1<\/i><\/p>\n<p>One of the great features of swift is it&#8217;s ability to be used on the command line. It&#8217;s actually really easy to do, and I&#8217;ll show you how to make an executable Swift file in this tutorial.<\/p>\n<p>First let&#8217;s create a new Swift file. In any text editor, create a new file and save it as Hello.swift.<\/p>\n<p>In order to make this file executable we need to give it permissions. In the terminal app, navigate to your file using the cd command, and then type this:<\/p>\n<pre>\r\nchmod +x Hello.swift\r\n<\/pre>\n<p>Now open up your Swift file. We&#8217;re going to tell it to use xcrun to run our code.<br \/>\nFirst, let&#8217;s get a simple script going that prints a response to the console:<\/p>\n<pre class=\"brush: js;\">\r\n#!\/usr\/bin\/env xcrun swift\r\n\r\nprintln(\"Hello World!\")\r\n<\/pre>\n<p>This is great, but what about arguments? It&#8217;s very common for a script to take some arguments, do something with them, and then return a result.<br \/>\nLet&#8217;s say we wanted to make a command line utility that takes any number of numbers as arguments, and just adds them together. We&#8217;ll call it <b>SwiftSum<\/b>.<\/p>\n<p>A reasonable first step in building this script would be to simply print out the command line arguments. Similar to other languages, Swift automatically creates an argument count variable and argument value pointer.<\/p>\n<p>C_ARGC is the number of arguments passed in<br \/>\nC_ARGV is an array of the argument&#8217;s values<\/p>\n<p>We can loop through the arguments and print them fairly easily&#8230;<\/p>\n<pre class=\"brush: js;\">\r\n&#35;!\/usr\/bin\/env xcrun swift\r\nfor i in 1..&lt;C_ARGC {\r\n  let index = Int(i)\r\n  if let argStr = String.fromCString(C_ARGV[index]) {\r\n    println(argStr)\r\n  }\r\n}\r\n<\/pre>\n<pre>\r\nSwiftSum.swift 1 2 3\r\n<\/pre>\n<p>Outputs:<\/p>\n<pre>\r\n1\r\n2\r\n3\r\n<\/pre>\n<p>If we want to add these, we just need to attempt converting them to an Int, and if that succeeds add it to a total&#8230;<\/p>\n<pre class=\"brush: js;\">\r\n&#35;!\/usr\/bin\/env xcrun swift\r\n\r\nvar total : Int = 0\r\n\r\nfor i in 1..&lt;C_ARGC {\r\n  let index = Int(i)\r\n  if let argStr = String.fromCString(C_ARGV[index]) {\r\n    if let argInt = argStr.toInt() {\r\n      total += argInt\r\n    }\r\n  }\r\n}\r\n\r\nprintln(total)\r\n<\/pre>\n<pre>\r\n$ .\/SwiftSum.swift 1 2 3\r\n<\/pre>\n<p>Outputs:<\/p>\n<pre>\r\n6\r\n<\/pre>\n<p>In <a href='http:\/\/jamesonquave.com\/swiftebook'>my book<\/a> I talk more about using Swift from the command line, so I hope you choose to follow along. If not, make sure you subscribe to get the latest tutorials from me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swift Scripts From The Command Line This post updated for Xcode 6.1 One of the great features of swift is it&#8217;s ability to be used on the command line. It&#8217;s actually really easy to do, and I&#8217;ll show you how to make an executable Swift file in this tutorial. First let&#8217;s create a new Swift&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_links_to":"","_links_to_target":""},"categories":[24,10,5,32],"tags":[49,33],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Running Swift Scripts From The Command Line - Jameson Quave<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Running Swift Scripts From The Command Line - Jameson Quave\" \/>\n<meta property=\"og:description\" content=\"Swift Scripts From The Command Line This post updated for Xcode 6.1 One of the great features of swift is it&#8217;s ability to be used on the command line. It&#8217;s actually really easy to do, and I&#8217;ll show you how to make an executable Swift file in this tutorial. First let&#8217;s create a new Swift...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/\" \/>\n<meta property=\"og:site_name\" content=\"Jameson Quave\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-20T05:05:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-10-31T03:03:21+00:00\" \/>\n<meta name=\"author\" content=\"Jameson Quave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jameson Quave\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/\",\"url\":\"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/\",\"name\":\"Running Swift Scripts From The Command Line - Jameson Quave\",\"isPartOf\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\"},\"datePublished\":\"2014-06-20T05:05:05+00:00\",\"dateModified\":\"2014-10-31T03:03:21+00:00\",\"author\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\"},\"breadcrumb\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jamesonquave.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Running Swift Scripts From The Command Line\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\",\"url\":\"https:\/\/jamesonquave.com\/blog\/\",\"name\":\"Jameson Quave\",\"description\":\"Using computer technology to educate, and improve lives.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\",\"name\":\"Jameson Quave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"caption\":\"Jameson Quave\"},\"sameAs\":[\"http:\/\/jamesonquave.com\"],\"url\":\"https:\/\/jamesonquave.com\/blog\/author\/jquave\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Running Swift Scripts From The Command Line - Jameson Quave","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/","og_locale":"en_US","og_type":"article","og_title":"Running Swift Scripts From The Command Line - Jameson Quave","og_description":"Swift Scripts From The Command Line This post updated for Xcode 6.1 One of the great features of swift is it&#8217;s ability to be used on the command line. It&#8217;s actually really easy to do, and I&#8217;ll show you how to make an executable Swift file in this tutorial. First let&#8217;s create a new Swift...","og_url":"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/","og_site_name":"Jameson Quave","article_published_time":"2014-06-20T05:05:05+00:00","article_modified_time":"2014-10-31T03:03:21+00:00","author":"Jameson Quave","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jameson Quave","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/","url":"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/","name":"Running Swift Scripts From The Command Line - Jameson Quave","isPartOf":{"@id":"https:\/\/jamesonquave.com\/blog\/#website"},"datePublished":"2014-06-20T05:05:05+00:00","dateModified":"2014-10-31T03:03:21+00:00","author":{"@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc"},"breadcrumb":{"@id":"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jamesonquave.com\/blog\/running-swift-scripts-from-the-command-line\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jamesonquave.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Running Swift Scripts From The Command Line"}]},{"@type":"WebSite","@id":"https:\/\/jamesonquave.com\/blog\/#website","url":"https:\/\/jamesonquave.com\/blog\/","name":"Jameson Quave","description":"Using computer technology to educate, and improve lives.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc","name":"Jameson Quave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","caption":"Jameson Quave"},"sameAs":["http:\/\/jamesonquave.com"],"url":"https:\/\/jamesonquave.com\/blog\/author\/jquave\/"}]}},"_links":{"self":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/867"}],"collection":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/comments?post=867"}],"version-history":[{"count":5,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/867\/revisions"}],"predecessor-version":[{"id":1401,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/867\/revisions\/1401"}],"wp:attachment":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/media?parent=867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/categories?post=867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/tags?post=867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}