<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[A Swift misadventure - Medium]]></title>
        <description><![CDATA[A journey in the Swift programming language and its uncommon applications. - Medium]]></description>
        <link>https://medium.com/a-swift-misadventure?source=rss----7270030b88d6---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>A Swift misadventure - Medium</title>
            <link>https://medium.com/a-swift-misadventure?source=rss----7270030b88d6---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 24 Apr 2026 13:38:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/a-swift-misadventure" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Mastering the iOS Interview]]></title>
            <link>https://medium.com/a-swift-misadventure/mastering-the-ios-interview-98e200902fe9?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/98e200902fe9</guid>
            <category><![CDATA[interview-preparation]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[job-interview]]></category>
            <category><![CDATA[interview-questions]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Thu, 14 Jan 2021 15:25:24 GMT</pubDate>
            <atom:updated>2021-01-14T15:25:24.362Z</atom:updated>
            <content:encoded><![CDATA[<h4>A video course to learn how to master your next iOS job interview and get that dream job you’re after.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*Coxc536OKiJ768PrYPFj0Q.jpeg" /></figure><p>2019 and 2020 have been years of personal transformation. I focused on anything but tech. It was not exciting for me anymore. But in the last few months, I was able to get out of my comfort zone, ask for help to great friends and invest time and effort in building.</p><p>Now, I am finally ready to share my latest creation: <a href="https://www.udemy.com/course/mastering-the-ios-interview/"><em>Mastering the iOS Interview</em></a>. It’s a video course to learn how to master your next iOS job interview and get that dream job you’re after.</p><p><a href="https://www.udemy.com/course/mastering-the-ios-interview/">Mastering the iOS Interview</a></p><p>I hope this is the beginning of a series of video courses to help other software engineers get the jobs they like.</p><p>Let keep 🤞.</p><p>PS: feedback is alway welcome!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98e200902fe9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/mastering-the-ios-interview-98e200902fe9">Mastering the iOS Interview</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Install Multiple Versions of Xcode on the Same macOS Machine]]></title>
            <link>https://medium.com/a-swift-misadventure/how-to-install-multiple-versions-of-xcode-on-the-same-macos-machine-a2836387e57f?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/a2836387e57f</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[old-xcode]]></category>
            <category><![CDATA[version]]></category>
            <category><![CDATA[macos]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Wed, 11 Nov 2020 22:02:23 GMT</pubDate>
            <atom:updated>2020-11-11T22:02:22.958Z</atom:updated>
            <content:encoded><![CDATA[<p>As an iOS developer, you often have to coordinate and use a specific versions of Xcode as part of a team. Especially if the codebase gets older, bigger and more complex.</p><p>It becomes essential then to install multiple versions of Xcode on the same machine. Here it’s how to do it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Qtfw2Wy9-av5BIOiXKOBg.jpeg" /></figure><h3>Latest Xcode version</h3><p>The latest version of Xcode should generally be installed from the AppStore, since Apple does not provide a downloadable version. It will get updated by the AppStore when a new stable version comes out (automatically or manually depending on your AppStore settings).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eORMJQjDHNUfXcUFiuEzDg.png" /></figure><p>This Xcode version will just be named <strong>Xcode</strong> in your <strong>Applications</strong> folder.</p><h3>Older Xcode versions</h3><p>You can download old versions of Xcode and install them on the side of the latest one. So you can just choose which version you want to launch depending on your situation.</p><p>Open <a href="https://developer.apple.com/download/more/">https://developer.apple.com/download/more/</a> and login with your developer account. Then write <strong>Xcode</strong> in the left column’s search bar. You will now be able to dowload older and newer versions Xcode, including betas and RCs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XDr0DSZg_0Kfbmzs1zAAKw.png" /></figure><p>After downloading the archive, unpack it. Rename the extracted <strong>Xcode</strong> app something like <strong>Xcode_11.7</strong>. Then, move it into the <strong>Applications</strong> folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*WVqT7I94adLZitug8ebcKQ.png" /></figure><p>You are done. Good development!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2836387e57f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/how-to-install-multiple-versions-of-xcode-on-the-same-macos-machine-a2836387e57f">How to Install Multiple Versions of Xcode on the Same macOS Machine</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Expose Init Parameters and Dependencies in Swift and iOS Please]]></title>
            <link>https://medium.com/a-swift-misadventure/expose-init-parameters-and-dependencies-in-swift-and-ios-please-752497f06073?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/752497f06073</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[code]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Thu, 08 Oct 2020 16:27:46 GMT</pubDate>
            <atom:updated>2020-10-08T16:27:46.501Z</atom:updated>
            <content:encoded><![CDATA[<p>There is one simple concept to follow when developing Swift or iOS code. The init parameters and the dependencies should be accessible from the outside. Even if from the outside you don’t need them.</p><p>It’s good etiquette because it gives transparency. It’s good practice because it makes the component you created more scalable. It makes sense because it makes code easier to test.</p><p>When creating your future entity store the init parameter as a property with the same access control level of the entity itself. If your entity is public make it public. If internal keep it internal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cMqZuAw9rVcZopYBiRtVEw.png" /><figcaption>// Photo by Rowan Heuvel on Unsplash</figcaption></figure><p>One last thing. Please be consistent in the codebase. Don’t go for one logic in one place and a different one in another. Consistency makes understanding your codebase much easier. Especially when there are many team members.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=752497f06073" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/expose-init-parameters-and-dependencies-in-swift-and-ios-please-752497f06073">Expose Init Parameters and Dependencies in Swift and iOS Please</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Swift Action Delegate pattern Part 2]]></title>
            <link>https://medium.com/a-swift-misadventure/introducing-swift-action-delegate-pattern-part-2-904142fdbd6b?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/904142fdbd6b</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[delegation]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Wed, 26 Apr 2017 13:02:01 GMT</pubDate>
            <atom:updated>2017-04-26T13:06:59.416Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Here you find the </em><a href="https://medium.com/a-swift-misadventure/introducing-swift-action-delegate-pattern-c2e6a7d4880"><em>Part 1</em></a><em> of this article.</em></p><p>A while ago I wrote an article which introduced what I called <a href="https://medium.com/a-swift-misadventure/introducing-swift-action-delegate-pattern-c2e6a7d4880">Swift Action Delegate pattern</a>. It is an attempt to write less boilerplate code while implementing delegation for objects communication.</p><p>This was the core (maybe re-read the original article for more context):</p><pre>protocol DelegateAction {}<br>protocol ActionDelegate: class {<br>  func actionSender(_ sender: Any, didReceiveAction action: DelegateAction)<br>}</pre><p>Few ex-collegues gave me an interesting piece of feedback. The sender is Any type. This is a code smell and not following the Swift principles. We should send an actual type instead, avoiding casting.</p><p>Yesterday, I finally figured out how to solve this problem:</p><pre>protocol DelegateAction {}<br>protocol ActionDelegate: class {<br>  func actionSender(didReceiveAction action: DelegateAction)<br>}</pre><p>We just remove the sender from the function and pass it as part of the DelegateAction payload. If DelegateAction is an enum (as I generally suggest) it can simply be another parameter of its cases.</p><pre>final class RedViewController: UIViewController {</pre><pre>  enum Action: DelegateAction {<br>    case close(sender: RedViewController)<br>    case complete(sender: RedViewController, success: Bool)<br>  }</pre><pre>  weak var delegate: ActionDelegate?<br>    <br>  func pressButton(sender: Any) {<br>    delegate?.actionSender(didReceiveAction: Action.close(sender: self))<br>  }<br>}</pre><p>And the class which conforms to the ActionDelegate protocol, has access to the sender which is of the actual type, not a simple Any type.</p><pre>final class ParentViewController: UIViewController, ActionDelegate {</pre><pre>  func actionSender(didReceiveAction action: DelegateAction) {<br>    switch action {<br>    case RedViewController.Action.close(let sender):<br>      print(&quot;Close red. Sender: \(sender)&quot;)<br>    case RedViewController.Action.complete(let sender, let success):<br>      print(&quot;Complete red. Sender: \(sender). Success: \(success)&quot;)<br>    default:<br>      print(action)<br>    }<br>  }</pre><pre>}</pre><p>I think this makes the ActionDelegate pattern stronger than it used to be.</p><p>As always, I am curious to get your opinion and feedback.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.tenor.co%2Fembed%2F8049985&amp;url=https%3A%2F%2Fwww.tenor.co%2Fview%2Fthekloons-goodbye-bye-gif-8049985&amp;image=https%3A%2F%2Fmedia.tenor.co%2Fimages%2F9ef74e9ba36f1734cd925b9109ea6aef%2Ftenor.gif&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/2959f03e1af726f1d54021d0972932bb/href">https://medium.com/media/2959f03e1af726f1d54021d0972932bb/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=904142fdbd6b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/introducing-swift-action-delegate-pattern-part-2-904142fdbd6b">Introducing Swift Action Delegate pattern Part 2</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Swift protocol conforming values are treated as Value types by default?]]></title>
            <link>https://medium.com/a-swift-misadventure/why-swift-protocol-conforming-values-are-treated-as-value-types-by-default-9482c6809583?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/9482c6809583</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swift-3]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Mon, 27 Mar 2017 08:36:55 GMT</pubDate>
            <atom:updated>2017-03-27T08:37:00.281Z</atom:updated>
            <content:encoded><![CDATA[<h4>Why the compiler decides to ignore the real type of something that conforms to a protocol?</h4><p>Let’s say you have a protocol, which only describes an interface. It does not restrict the conformance to only instance types (no : class).</p><pre>protocol AlphaProtocol {<br>    var message: String? { get set }<br>}</pre><p>Why do the compiler treats the conforming value/object always as a Value type?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82c1ba42cb0db9642e5ae948b57c9ccb/href">https://medium.com/media/82c1ba42cb0db9642e5ae948b57c9ccb/href</a></iframe><p>If we modify alphaValue property, the compile will ignore the actual type of alphaValue. It will always treat the property as a value type (e.g. struct). So it will create a copy of alphaValue with a different message, then it will replace the old copy with the new one.</p><p>If you restrict the AlphaProtocol to classes only, then alphaValue will be correctly be treated as an Instance type. So if you now update the message in AlphaObject, it will just update the message in the current instance.</p><pre>protocol AlphaProtocol: class {<br>    var message: String? { get set }<br>}</pre><p>This behaviour has been particularly hard to understand, since it’s not intuitive at all.</p><p>But the real question is: is this behaviour correct? Is it intended to work that way? If so, why?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9482c6809583" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/why-swift-protocol-conforming-values-are-treated-as-value-types-by-default-9482c6809583">Why Swift protocol conforming values are treated as Value types by default?</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Swift Action Delegate pattern Part 1]]></title>
            <link>https://medium.com/a-swift-misadventure/introducing-swift-action-delegate-pattern-c2e6a7d4880?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/c2e6a7d4880</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[delegation]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Thu, 23 Mar 2017 17:25:57 GMT</pubDate>
            <atom:updated>2017-04-26T13:05:26.901Z</atom:updated>
            <content:encoded><![CDATA[<h4>Trying to improve the delegation pattern in the Swift and iOS ecosystem.</h4><p>Delegation is one of the core pattern on which most iOS SDKs are based on. First of all UIKit. Being familiar with them is core to developing a proper iOS app.</p><p>Delegation is solving one of the hardest problem in software development: communication and sharing of information between objects. It’s a complex problem with different opinionated solutions.</p><p>I have experimented different solutions which try to solve this problem, but I still think plain delegation is the way to go. It’s simple and robust. Yeah, it’s not trendy like reactive programming 🤐, but it works 🤷.</p><p>The problem with delegation is that it’s not immediate. It takes a moment to understand. And when you get familiar with it, it still involves writing a new delegate protocol for every context in which you need it.</p><p>Delegates are being used for two main reasons:</p><ul><li>A generic object that can be customised in the point of use. Think about a UICollectionView. It depend upon DataSource delegate methods to get the information it has to display.</li><li>An object which has to communicate with another object. Think about a ParentVC which has presented modally a ChildVC. After ChildVC has completed his goal, it has to communicate to ParentVC he wants to be dismissed.</li></ul><p>Let’s say we have a ParentVC which at different moment in time has to present RedVC and BlueVC. They both have to be dismissed at some point in the future. Maybe because the user just want to go back or because the user has selected some value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tOu3KEPle2lJ95GCkuwPBA.png" /></figure><p>In a classic scenario both the RedVC and BlueVC have to define a protocol which ParentVC has to conform to. This way ParentVC will be able to receive some information and eventually dismiss the child VCs.</p><p>This generates boilerplate code (which a lot of developers hate very much). It will generate boilerplate in each child VCs files, because protocols need to be defined. It will generate boilerplate code in the ParentVC because it has to conform to two differnent protocols.</p><p>Here a practical example (which you can run inside a Playground).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a155cd26a5441c17be438aed27f323e/href">https://medium.com/media/0a155cd26a5441c17be438aed27f323e/href</a></iframe><p>To avoid creating every time a new protocol, which adds very little value, let’s explore another avenue. Why can’t we create just one ActionDelegate protocol which is made for communication between objects? It would be enough to have a flexible way to pass context specific information and it should work everywhere.</p><p>The idea is very simple: an ActionDelegate protocol which receives a generic Action as parameter. The Action is any value conforming to the DelegateAction protocol. This way every object who wants to use the ActionDelegate pattern can define his own context specific actions.</p><pre>protocol DelegateAction {}<br>protocol ActionDelegate: class {<br>    func actionSender(_ sender: Any, didReceiveAction action: DelegateAction)<br>}</pre><p>Here it follows the previous example, but adopting the ActionDelegate pattern.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f2447e484196e3153ca46c0a44192cc1/href">https://medium.com/media/f2447e484196e3153ca46c0a44192cc1/href</a></iframe><p>The solution, in my opinion, is a (bit) simpler and more consistent delegation pattern. I think it could be a way to write a (bit) less boilerplate code, but especially to have a consistent and stable communication channel between objects.</p><p>There are questions that need to be answered. Like, is it a good idea to have a single method responsible to reply to messages from other objects? It’s something similar to prepareForSegue (🙈🙉🙊).</p><p>I am interested in your opinion. Please comment or test/fork the gists in a Playgorund and let me know a better solution.</p><p><em>Since I published this article, I got some feedback on some concepts to be improved. So, I wrote a follow up article: </em><a href="https://medium.com/a-swift-misadventure/introducing-swift-action-delegate-pattern-part-2-904142fdbd6b"><em>Introducing Swift Action Delegate pattern Part 2</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2e6a7d4880" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/introducing-swift-action-delegate-pattern-c2e6a7d4880">Introducing Swift Action Delegate pattern Part 1</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Xcode to write Swift code on a remote Linux machine]]></title>
            <link>https://medium.com/a-swift-misadventure/how-to-use-xcode-to-write-swift-code-on-a-remote-linux-machine-201ed8ca0124?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/201ed8ca0124</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Thu, 11 Aug 2016 10:23:04 GMT</pubDate>
            <atom:updated>2016-08-11T10:23:03.646Z</atom:updated>
            <content:encoded><![CDATA[<p>I have to write a lot of Swift code targeted to Linux only environment. It’s a robotic app, where no monitor is contemplated. I could connect remotely to it and code with emacs or vim. But I am so used to Xcode… So I decided to just keep using it, remotely.</p><p>The concept is simple. I am going to mount the Linux machine HDD as remote drive (via ssh) on my Mac.</p><p>This means being able to to use Xcode on my Mac, with all the nice tools I have there. I will only have to open the terminal on the Linux machine to compile and run.</p><h4>Step 1: Install SshFS</h4><p>On your Mac install OSXFuse and SshFS. You can download them both from <a href="https://osxfuse.github.io/">https://osxfuse.github.io/</a></p><h4>Step 2: Create a mount directory</h4><p>Somewhere on your Mac create a folder which will be where your remote drive will be mounted (where you will find the remote drive’s files).</p><pre>mkdir ~/Desktop/RemoteDrive</pre><h4>Step 3: Mount the remote drive</h4><p>You will have to mount the remote drive at every system startup.</p><pre>sshfs USER@MACHINE-HOSTNAME:/home/USER/ ~/Desktop/RemoteDrive</pre><h4>Step 4: Code</h4><p>Now you will be able to open the directory from Finder and use it as USB stick. Just open it, create your Xcode project and develop as usual.</p><h4>Step 5: Unmount the drive</h4><p>When you’re done, you should unmount the drive securely.</p><pre>fusermount -u ~/Desktop/RemoteDrive</pre><p>Simple and convenient. Yay!</p><p><em>To get my latest Swift misadventures </em><a href="https://medium.com/a-swift-misadventure"><em>subscribe to the publication</em></a><em>. Cheers.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=201ed8ca0124" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/how-to-use-xcode-to-write-swift-code-on-a-remote-linux-machine-201ed8ca0124">How to use Xcode to write Swift code on a remote Linux machine</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to setup your Raspberry Pi 2/3 with Ubuntu 16.04, without cables (headlessly)]]></title>
            <link>https://medium.com/a-swift-misadventure/how-to-setup-your-raspberry-pi-2-3-with-ubuntu-16-04-without-cables-headlessly-9e3eaad32c01?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/9e3eaad32c01</guid>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[headless]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Mon, 01 Aug 2016 17:03:13 GMT</pubDate>
            <atom:updated>2017-04-25T09:42:57.179Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*Kv8o7RNO6fcb49-HLrArPw.png" /></figure><p>I am working on a robotic project which has at its core a Raspberry Pi with Ubuntu minimal (no GUI) and Swift 3.0.</p><p>The problem with a vanilla Ubuntu is that it requires you to have a monitor, keyboard and mouse. All of this in inconvenient and I would love to just do everything by ssh-ing into the machine. Sadly, it’s not possible out of the box.</p><p>After some experimenting I’ve been able to achieve that. What follows is the tale of this mighty nerd.</p><h4>Step 1: Preparation</h4><p>Get your Raspberry Pi 2 or 3, an SD memory card with 8Gb or more memory (class 10 or more), a micro USB cable (for power), an ethernet cable and the Wifi USB dongle.</p><h4>Step 2: Flashing Ubuntu 16.04 server minimal on the SD card</h4><p>Download Ubuntu server classic from <a href="https://ubuntu-pi-flavour-maker.org/download/">here</a> or <a href="http://cdimage.ubuntu.com/ubuntu/releases/16.04/release/ubuntu-16.04.2-preinstalled-server-armhf+raspi2.img.xz">here</a>. This is a flavour of Ubuntu server, which have been stripped out of everything which is not strictly necessary, so it’s very light.</p><p>Now you have to flash the image on the SD card. On the web you will find a lot of multi step command line guides. But that’s nuts, since there’s a great apps, called <a href="http://etcher.io">Etcher.io</a>, that make the process incredibly simple, fast and safe.</p><p>Download Etcher app (for all platform), then select the Ubuntu image, the SD card (which you have to insert in your computer) and flash it. Few minutes and it will be done and verified.</p><p>Put the SD card into the RPi, you’re ready to rock!</p><h4>Step 3: Ssh-ing into your RPi</h4><p>After you’ve inserted the freshly backed SD card into your RPi connect the ethernet cable between the RPi and your router. Turn the RPi on by connecting the micro USB cable to the electricity or to your computer.</p><p>Wait few minutes, since the first boot takes longer than usual. Then open your router’s dashboard and look at the ethernet connected devices. You will see a device hostname <em>ubuntu-minimal</em>. Take a note of the IP of that device.</p><p>Open your terminal and type:</p><pre>ssh ubuntu@THE_IP_YOU_JUST_FOUND<br>(password ubuntu)</pre><p>You should be in!</p><p>You are now connected to the RPi via the network. The only problem is that you still need to keep the RPi connected to the network throughout the ethernet cable. But let’s address that.</p><h4>Step 4: Going wireless</h4><p>First we need to update the operating system:</p><pre>sudo apt-get update<br>sudo apt-get upgrade</pre><p>Install the wifi support:</p><pre>sudo apt-get install wireless-tools wpasupplicant</pre><p>Now reboot. It’s important to do it now, since my wireless interface changed name after this step. From a nice “wlan0” to a weird “wlx000f6005a699”.</p><pre>sudo reboot</pre><p>Attach the USB dongle to your RPi. Then, ssh again into it. Then, list the wireless network interfaces with:</p><pre>iwconfig</pre><p>Take note of the wireless interface name (e.g. <em>wlan0</em> or <em>wlx000f6005a699</em>).</p><p>Open the network interfaces configuration:</p><pre>sudo nano /etc/network/interfaces</pre><p>At the bottom of the file add (replacing the <em>wlan0 </em>with the name of your interface):</p><pre>allow-hotplug wlan0<br>iface wlan0 inet dhcp<br>wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf</pre><p>Now, open the wireless configuration file:</p><pre>sudo nano /etc/wpa_supplicant/wpa_supplicant.conf</pre><p>and add at the end of the file the informations about your wifi network:</p><pre>network={<br>    ssid=&quot;your-network-ssid-name&quot;<br>    psk=&quot;your-network-password&quot;<br>}</pre><p>Save and exit the editor. Remove the ethernet cable and then reboot:</p><pre>sudo reboot</pre><p>Now in about 30 seconds your RPi should be up an running. If you try again to ssh into the RPi (still looking for its IP in the router dashboard), you should just be able to connect to it via wifi!!</p><h4>Step 5: Ssh-ing into the RPi with a dynamic IP</h4><p>As you’ve already noticed, checking the the RPi’s IP every time you want to connect to it, it’s a bit of a pain in the ass.</p><p>So, let’s install <em>avahi</em> which will make you able to connect to the RPi via its hostname.</p><pre>sudo apt-get install avahi-daemon</pre><p>Now, after a reboot, you will be able to ssh into the machine:</p><pre>ssh ubuntu@ubuntu-minimal.local<br>(password ubuntu)</pre><h4>Step 6: Securing the ssh authentication</h4><p>I suggest you to secure your login with a rsa key authentication and enabling it only the port 22.</p><p>From your Mac laptop:</p><pre>ssh-copy-id ubuntu@ubuntu-minimal.local</pre><p>Now you should be able to ssh into RPi without password:</p><pre>ssh ubuntu@ubuntu-minimal.local</pre><h4>The-wise-guy-last-step: Backup your RPi</h4><p>It’s very easy to burn an SD card by writing to it too many times. Or irreversibly <em>fuckup</em> your configuration by doing what <em>sudoer</em> should not do. Cloning your SD card, as an image that you can flash on a new card when you need it, is the perfect backup strategy. <a href="https://medium.com/@ccarnino/backing-up-your-raspberry-pi-sd-card-on-mac-the-simple-way-398a630f899c#.fb2xyaos4">Here you find how</a>.</p><p>You are now ready to <a href="https://medium.com/@ccarnino/installing-swift-3-0-developer-preview-on-a-raspberry-pi-2-3-with-ubuntu-16-04-6b7ef220b3d7#.7jaftp69d">install Swift 3.0 developer preview</a> on your sexy Raspberry Pi Ubuntu box.</p><p><em>To get my latest Swift misadventures </em><a href="https://medium.com/a-swift-misadventure"><em>subscribe to the publication</em></a><em>. Cheers.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e3eaad32c01" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/how-to-setup-your-raspberry-pi-2-3-with-ubuntu-16-04-without-cables-headlessly-9e3eaad32c01">How to setup your Raspberry Pi 2/3 with Ubuntu 16.04, without cables (headlessly)</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Swift to control the Raspberry Pi GPIO pins and turn an LED on]]></title>
            <link>https://medium.com/a-swift-misadventure/using-swift-to-control-the-raspberry-pi-gpio-pins-and-turn-an-led-on-f31e33c3cb9a?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/f31e33c3cb9a</guid>
            <category><![CDATA[electronics]]></category>
            <category><![CDATA[gpio]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[raspbian]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Mon, 11 Jul 2016 10:06:35 GMT</pubDate>
            <atom:updated>2016-08-03T13:18:02.406Z</atom:updated>
            <content:encoded><![CDATA[<p>Here we are with a Raspberry Pi with <a href="https://medium.com/@ccarnino/384d8bb5aed4">Raspbian</a> and <a href="https://medium.com/@ccarnino/3e857fa995d9">Swift 3.0 installed</a>. Now we can actually do something with it. Like flashing an unsexy LED. But hey, we have to start from somewhere…</p><h4>Step 1: Understanding the GPIO pins numbers</h4><p>The core concept is that not all the pins in available on the board are GPIOs (General Purpose Input/Output). Some are power supply, others are ground connection and others are <em>other-stuff</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*QlSyHfcfNu4ePpNoNtKcZQ.jpeg" /></figure><p>The number inside the circle is the <em>pin number</em>. Some of these pins match a <em>GPIO pin</em>. For example the <em>pin 7</em> matches the <em>GPIO4 pin</em>.</p><p>We can control only the GPIO pins, not the other ones. The other ones are used to power up your circuit or to do <em>other-stuff</em>.</p><h4>Step 2: Connecting the circuit</h4><p>We want to create a simple circuit where an LED is turn on by the GPIO4 (pin 7). So we create a simple circuit like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E6qxWlKGrCFWZvG8tNZ2fQ.png" /><figcaption>LED + resistor, connected to ground to one side and to a GPIO pin on the other side</figcaption></figure><h4>Step 3: Making sure we can actually set a value on a GPIO pin</h4><p>We are installing a package that allow us to set values on a pin from command line. Just to make sure we’re actually able to turn the GPIO value high and low.</p><pre>sudo apt-get install wiringpi</pre><p>Now let’s set <em>pin 7</em> as output and turn it off, then on and then off again:</p><pre>gpio mode 7 out<br>gpio write 7 0<br>gpio write 7 1<br>gpio write 7 0</pre><p>It should work. If it doesn’t, well, it’s very likely you’ve made some wiring mistake in your circuit.</p><h4>Step 4: Using SwiftyGPIO library to control the GPIO pins</h4><p><a href="https://twitter.com/uraimo">Uraimo</a> has created a very nice <a href="https://github.com/uraimo/SwiftyGPIO">Swift wrapper</a> around the GPIO pins, so we can control them in our Swift app. There are few things that could be done better (e.g. more protocol oriented representation of the different GPIOs types, better representation of the GPIOs states and more immutability of a GPIO representation), but it’s still a good starting point.</p><p>Since the Swift Package Manager is still not working properly on Raspbian to date (July the 8th, 2016), we are going to clone the git repo and use the file directly within our code.</p><p>The important bit is to have all the sources in one folder, to be able to compile them all in one simple command.</p><pre>mkdir ~/ledtest<br>mkdir ~/ledtest/Sources<br>cd ~/ledtest<br>git clone <a href="https://github.com/uraimo/SwiftyGPIO.git">https://github.com/uraimo/SwiftyGPIO.git</a><br>cp SwiftyGPIO/Sources/* Sources/</pre><p>Let’s create our main:</p><pre>nano Sources/main.swift</pre><p>with this code:</p><pre>import Glibc</pre><pre>// Get the Pin where the LED will be attached to<br>let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi2)<br>guard let ledGPIO = gpios[.P4] else {<br>    fatalError(“It has not been possible to initialised the LED GPIO pin”)<br>}</pre><pre>// Set the GPIO to output<br>ledGPIO.direction = .OUT</pre><pre>// Turn on and off the led few times<br>ledGPIO.value = 1<br>sleep(1)<br>ledGPIO.value = 0<br>sleep(1)<br>ledGPIO.value = 1<br>sleep(1)<br>ledGPIO.value = 0</pre><p>Now compile and run as super user (it needs sudoer permissions to access the GPIO pins):</p><pre>swiftc Sources/*.swift<br>sudo ./main</pre><p>The LED should blink two times and then stop. Success!!</p><h4>Step 5: Cleanse our soul with some cleaner code</h4><p>I feel dirty in writing hacky scripts. I start regretting all my life decisions when I understand I wrote something that’s not even close to be acceptable good. So the following code is the v2 of the app.</p><p>But first I really suggest you to configure your Atom or SublimeText editor (on your main computer) to edit code remotely on your RPi, so you don’t have to use <em>nano</em> or <em>vim</em>. (Info for <a href="https://www.google.co.uk/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8#q=remote%20editing%20atom">Atom</a> and for <a href="https://www.google.co.uk/webhp?sourceid=chrome-instant&amp;ion=1&amp;espv=2&amp;ie=UTF-8#safe=off&amp;q=remote+editing+sublime+text">SublimeText</a>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05ed6214fa1463193c4b18e3b919fdc2/href">https://medium.com/media/05ed6214fa1463193c4b18e3b919fdc2/href</a></iframe><p>That should be it. Compile and run. The LED should keep blinking until you stop the execution.</p><p>This is the basic you need to know to start doing amazing things in electronics with a very nice programming language and all the things that comes with it.</p><p><em>To get my latest Swift misadventures </em><a href="https://medium.com/a-swift-misadventure"><em>subscribe to the publication</em></a><em>. Cheers.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f31e33c3cb9a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/using-swift-to-control-the-raspberry-pi-gpio-pins-and-turn-an-led-on-f31e33c3cb9a">Using Swift to control the Raspberry Pi GPIO pins and turn an LED on</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Installing Swift 3.0 preview on a Raspberry Pi 2/3 with Raspbian]]></title>
            <link>https://medium.com/a-swift-misadventure/installing-swift-3-0-preview-on-a-raspberry-pi-2-3-with-raspbian-3e857fa995d9?source=rss----7270030b88d6---4</link>
            <guid isPermaLink="false">https://medium.com/p/3e857fa995d9</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[raspbian]]></category>
            <category><![CDATA[gpio]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[electronics]]></category>
            <dc:creator><![CDATA[Claudio Carnino]]></dc:creator>
            <pubDate>Mon, 11 Jul 2016 10:06:07 GMT</pubDate>
            <atom:updated>2017-04-06T21:03:57.592Z</atom:updated>
            <content:encoded><![CDATA[<p>We’re <a href="https://medium.com/@ccarnino/384d8bb5aed4">halfway trough our journey</a> of controlling electric circuits and hardware with Swift and a Raspberry Pi. If you missed anything, <a href="https://medium.com/@ccarnino/384d8bb5aed4">here lie the answers to your questions</a>.</p><p>Ssh into your RPi:</p><pre>ssh pi@raspberrypi.local<br>(password raspberry)</pre><p>Open the apt sources file:</p><pre>sudo nano /etc/apt/sources.list</pre><p>Uncomment the last line:</p><pre>deb-src <a href="http://archive.raspbian.org/raspbian/">http://archive.raspbian.org/raspbian/</a> jessie main contrib non-free rpi</pre><p>Update your system and install some required packages:</p><pre>sudo apt-get update<br>sudo apt-get upgrade<br>sudo apt-get install clang git</pre><p>Installing Swift 3.0 build at root level. We’re going to use <a href="http://swift-arm.ddns.net/job/Swift-3.0-ARMv7-ubuntu1410/">Joe Bell’s incremental builds</a>:</p><pre>cd ~<br>wget <a href="http://47.186.11.166/job/Swift-3.0-ARMv7-ubuntu1410/29/artifact/swift-3.0-2016-11-03-armv7-ubuntu14.04.tar.gz">http://47.186.11.166/job/Swift-3.0-ARMv7-ubuntu1410/29/artifact/swift-3.0-2016-11-03-armv7-ubuntu14.04.tar.gz</a><br>sudo tar -xzpf swift-3.0-2016-11-03-armv7-ubuntu14.04.tar.gz -C /<br>rm swift-3.0-2016-11-03-armv7-ubuntu14.04.tar.gz</pre><p>You have installed Swift!! Now start screaming like Steve Ballmer in a moment of uncontrolled excitement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*hzIK3LF7lbasfRAI60fhFA.gif" /></figure><p>Let’s write our first Swift script to see that it’s actually working:</p><pre>mkdir swifttest<br>cd swifttest<br>nano main.swift</pre><p>Add this simple hello world (which ironically doesn’t say “hello world”):</p><pre>import Glibc<br>import Foundation</pre><pre>let currentDate = NSDate()<br>print(“My first Swift binary! Executed at \(currentDate)”)</pre><p>Compile and run:</p><pre>swiftc main.swift<br>./main</pre><p>Well done! You’re ready for the world. Now go and write beautiful swift code. <a href="https://medium.com/@ccarnino/f31e33c3cb9a">Or turn some LED on with Swift!</a></p><p><em>To get my latest Swift misadventures </em><a href="https://medium.com/a-swift-misadventure"><em>subscribe to the publication</em></a><em>. Cheers.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e857fa995d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/a-swift-misadventure/installing-swift-3-0-preview-on-a-raspberry-pi-2-3-with-raspbian-3e857fa995d9">Installing Swift 3.0 preview on a Raspberry Pi 2/3 with Raspbian</a> was originally published in <a href="https://medium.com/a-swift-misadventure">A Swift misadventure</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>