{"id":4409,"date":"2021-08-28T15:40:56","date_gmt":"2021-08-28T20:40:56","guid":{"rendered":"https:\/\/dev.iachieved.it\/iachievedit\/?p=4409"},"modified":"2021-08-28T15:40:56","modified_gmt":"2021-08-28T20:40:56","slug":"swiftformat-decent-swift-syntax-and-sublime-text","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/swiftformat-decent-swift-syntax-and-sublime-text\/","title":{"rendered":"swiftformat, Decent Swift Syntax, and Sublime Text"},"content":{"rendered":"<p>If you&#8217;re writing Swift code for iOS you&#8217;re most likely going to be doing so in Xcode.  If you&#8217;re coding Swift to run <a href=\"https:\/\/dev.iachieved.it\/iachievedit\/swift-on-linux-in-2021\/\">on the server<\/a>, you might want to check out an editor like <a href=\"https:\/\/www.sublimetext.com\">Sublime Text<\/a>.  In this post we&#8217;ll show you how to use Sublime, the <a href=\"https:\/\/github.com\/colinta\/decent-swift-syntax\">Decent Swift Syntax<\/a> package and <a href=\"https:\/\/github.com\/nicklockwood\/SwiftFormat\">swiftformat<\/a> together to write some nicely formatted Swift code.<\/p>\n<h2>Sublime Text<\/h2>\n<p>Installing Sublime is as easy as heading over to the <a href=\"https:\/\/www.sublimetext.com\">Sublime Text<\/a> website and clicking the download button appropriate for your platform.  In this post we&#8217;ll be using macOS.<\/p>\n<p>Once you&#8217;ve installed Sublime Text create a new file with <strong>File &#8211; New<\/strong> and then go to its <strong>Tools<\/strong> menu and select <strong>Command Palette<\/strong>.  A search field will open.  Start typing &#8216;package&#8217; and select <strong>Package Control:  Install Package<\/strong>.<br \/>\n<a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2021\/08\/installPackage.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2021\/08\/installPackage.png\" alt=\"\" width=\"718\" height=\"286\" class=\"aligncenter size-full wp-image-4412\" \/><\/a><\/p>\n<p>The search field will change again to list available packages to install.  Find and install <strong>Decent Swift Syntax<\/strong>.  You&#8217;ll notice from here that files ending in <code>.swift<\/code> will be inferred as containing the Swift language, and Decent Swift Syntax will go to work.<\/p>\n<h2>swiftformat<\/h2>\n<p>The <a href=\"https:\/\/github.com\/colinta\/decent-swift-syntax\">Decent Swift Syntax<\/a> package relies on <a href=\"https:\/\/github.com\/colinta\/decent-swift-syntax\">swiftformat<\/a> to do the heavy lifting of reformatting your Swift code.  To install it you will want to use <a href=\"https:\/\/brew.sh\">brew<\/a>: <code>brew install swiftformat<\/code><\/p>\n<h2>Two Spaces and a Gotcha<\/h2>\n<p>With the end of the <a href=\"https:\/\/stackoverflow.blog\/2017\/06\/15\/developers-use-spaces-make-money-use-tabs\/\">spaces vs. tabs war<\/a>, the new battlefront formed was whether to indent two spaces or four.  Anyone with sense knows that the answer is two spaces, so at the top of your Swift file you can add <code>\/\/ swiftformat:options --indent 2<\/code> in order to tell <code>swiftformat<\/code> to format your code accordingly.<\/p>\n<p>Now here is an interesting gotcha we you may find when writing closures.  Let&#8217;s say our closure function signature is something like:<\/p>\n<pre class=\"toolbar-overlay:false tab-size:2 lang:swift decode:true \" >typealias ClosureSignature = (String, Int, Int) -> Void\n<\/pre>\n<p>And we&#8217;re writing some code that supplies the closure, like:<\/p>\n<pre class=\"toolbar-overlay:false tab-size:2 lang:swift decode:true \" >getData { data, status, error in\n  print(data)\n}<\/pre>\n<p>If this is all you wrote and saved your code <code>swiftformat<\/code> would happily replace <code>status<\/code> and <code>error<\/code> because they were <i>unused arguments<\/i>.  Out of habit I save files often so it came as a bit of a surprise when my arguments started disappearing.  Although <code>swiftformat<\/code> has an <code>stripunusedargs<\/code> option, it doesn&#8217;t appear to permit you to turn it off.<\/p>\n<div style=\"width: 640px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]-->\n<video class=\"wp-video-shortcode\" id=\"video-4409-1\" width=\"640\" height=\"360\" autoplay=\"1\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/quicktime\" src=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2021\/08\/swiftformat_eating_arguments.mov?_=1\" \/><a href=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2021\/08\/swiftformat_eating_arguments.mov\">https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2021\/08\/swiftformat_eating_arguments.mov<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re writing Swift code for iOS you&#8217;re most likely going to be doing so in Xcode. If you&#8217;re coding Swift to run on the server, you might want to check out an editor like Sublime Text. In this post we&#8217;ll show you how to use Sublime, the Decent Swift Syntax package and swiftformat together [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4409","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4409"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=4409"}],"version-history":[{"count":14,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4409\/revisions"}],"predecessor-version":[{"id":4427,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/4409\/revisions\/4427"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=4409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=4409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=4409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}