Jekyll2020-01-22T21:48:36+01:00https://blog.pigonahill.com/feed.xmlPig on a Hill ProductionsiOS App and Swift DevelopmentJames BarrowApple Watch - A Passive App Platform2016-11-03T00:00:00+01:002017-05-21T15:57:37+02:00https://blog.pigonahill.com/2016/apple-watch-a-passive-app-platform<p>I first started writing this article before WWDC 2016, then I stopped. For starters watchOS 3 was announced and I wondered if a lot of my initial points would be resolved by the speed increase it sounded like we’d get there. And then I just got busy and didn’t manage to update my post. Such is life. But we’ve had watchOS 3 for a while now, and we’ve gotten the new Apple Watches, Series 1 & 2, with the improved S2 SoC. While we did get the speed increases from watchOS 3 we were promised (in the new and old hardware) looking back, I think a lot of my points still stand. So here we go, take 2 of this post.</p>
<p>So let’s face it watchOS 1 & 2 were a good start for the platform, but they were way too slow. watchOS 1 had limitations due to the fact that the main part of each Watch app was still running on our iPhones, and where watchOS 2 fixed that point the limitations that Apple had in the OS on the amount of available memory and resources was too stringent <sup id="fnref:1"><a href="#fn:1" class="footnote">1</a></sup>. With watchOS 3, Apple fixed that though, with the addition of the dock and keeping more apps in memory, apps became a LOT more responsive when opening (if they’re in your dock), but in my mind they’re still not fast enough.</p>
<p>Apple have said to developers since the watch first came out, interaction on the watch should be kept to a minimum. So much so as to be only a few seconds <sup id="fnref:2"><a href="#fn:2" class="footnote">2</a></sup>. In that respect, opening an app and waiting for it to update is still too slow. I know with watchOS 3, the majority of that lies on the shoulders of us developers <sup id="fnref:3"><a href="#fn:3" class="footnote">3</a></sup>, but it still stands that it’s still too slow for the types of interactions we generally have with the watch. That is to say, trying to have an interaction with an app, like it’s an app on our iPhone or iPad.</p>
<p>Where I feel the Apple Watch excels is in passive activities. When I say passive activities, I mean things that the watch just <em>does</em>, without input from the user. In my mind such activities range from counting steps and taking heart rate readings over the course of the day to receiving notifications and viewing complications on the Watch face.</p>
<!-- READMORE -->
<p>A little bit of self smartness would be really great when it comes to this type passive activities. When I go out for a run, my watch should be able to tell that I’ve started exercising and when I’ve stopped, so it can then ask me “Do you want to save this workout?” without me even having to tell it I started one. This is exactly the type of passive activities that Apple needs to improve upon, and in turn, allow us developers more ways to give our users this type of functionality.</p>
<p>But I think this could be taken even further, when it comes to Apple (and developers) looking at all 4 of its OSes together. Apple have made great improvements with watchOS 3, iOS 10 and macOS 10.12 in regards to continuity <sup id="fnref:4"><a href="#fn:4" class="footnote">4</a></sup>. You can now have your Mac login automatically if you have your watch on (and unlocked), you can use Apple Pay on your Mac via Safari using your iPhone’s Touch ID sensor, handoff is still something I use constantly with Safari and supporting apps across all my devices, and even though it’s not perfect the shared clipboard is really handy. But why can I not walk away from my Mac and continue listening to my music on my phone? What about raising my watch, and it knowing I’m listening to music, so presenting me with the “Now Playing” screen? And if I leave home without my phone, why can’t my watch tap me on the wrist to let me know that I should go back and grab it? <sup id="fnref:5"><a href="#fn:5" class="footnote">5</a></sup></p>
<p>I think we’ve seen the beginning of some of this smartness already. For example features like proactive Siri, be it with app suggestions, smart calendar reminders or simply working out the best time to backup our device when it’s most likely to be plugged in and not being used. Also, more notably lately, with the (now delayed, but hopefully still coming soon) AirPods, which not only sync their pairing between devices (via iCloud), but will smartly change how they work depending on how you use them and what device you’re currently using.</p>
<p>All these things are promising and it’s also extremely possible that these things, or similar, are already in the pipeline, but it surely would be great to see improvements coming in the next OS updates. I know it’s still 2016, but bring on WWDC 2017.</p>
<div class="footnotes">
<ol>
<li id="fn:1">
<p>Apple even said so on the live episode of The Talk Show with John Gruber and his special guests of Phil Schiller and Craig Federighi (relevant timecode 01:04:05): <a href="http://daringfireball.net/thetalkshow/2016/06/17/ep-158" target="_blank">http://daringfireball.net/thetalkshow/2016/06/17/ep-158/</a> <a href="#fnref:1" class="reversefootnote">↩</a></p>
</li>
<li id="fn:2">
<p>See the WWDC session here (relevant timecode 01:23): <a href="https://developer.apple.com/videos/play/wwdc2016/227/?time=83" target="_blank">https://developer.apple.com/videos/play/wwdc2016/227/</a> <a href="#fnref:2" class="reversefootnote">↩</a></p>
</li>
<li id="fn:3">
<p>Background updates on watchOS 3: <a href="https://developer.apple.com/videos/play/wwdc2016/218/" target="_blank">https://developer.apple.com/videos/play/wwdc2016/218/</a> <a href="#fnref:3" class="reversefootnote">↩</a></p>
</li>
<li id="fn:4">
<p>I deliberately left tvOS 10 out of this list, as I feel there’s still no really good continuity features with tvOS and the other OSes. <a href="#fnref:4" class="reversefootnote">↩</a></p>
</li>
<li id="fn:5">
<p>I could go on and on here. There’s so much that could be done with all Apple’s devices and OSes, including the Apple TV and Siri. <a href="#fnref:5" class="reversefootnote">↩</a></p>
</li>
</ol>
</div>James BarrowI first started writing this article before WWDC 2016, then I stopped. For starters watchOS 3 was announced and I wondered if a lot of my initial points would be resolved by the speed increase it sounded like we’d get there. And then I just got busy and didn’t manage to update my post. Such is life. But we’ve had watchOS 3 for a while now, and we’ve gotten the new Apple Watches, Series 1 & 2, with the improved S2 SoC. While we did get the speed increases from watchOS 3 we were promised (in the new and old hardware) looking back, I think a lot of my points still stand. So here we go, take 2 of this post.Puck 3.02016-09-12T00:00:00+02:002017-05-21T15:57:37+02:00https://blog.pigonahill.com/2016/puck-3-0<p>Puck 3.0 is finally here!</p>
<p>It’s taken a while now of me messing around and technologies I was going to use changing, but I finally managed to finish and release Puck 3.0 for the SHL 2016/2017 season.</p>
<p>So, what’s new? Well I did a major redesign of the app around a year ago. It brings Puck more inline with how iOS looks generally these days. A little bit cleaner and more direct to get the content to you. Secondly, there’s now video highlights in the app! You can now view video highlights for games, right from the app, or you can stream these to your AppleTV.</p>
<p>There’s also been a few other little changes, like being able to see the SHL arena website for a game that is currently live or has been played, allowing you to see play by plays right from the app. There is also now an app for you AppleWatch owners out there that allows you to see live games, standings and news summaries.</p>
<p>All in all I’m very pleased with this new update and looking forward for people to get hold of it.</p>
<p>Puck 3.0 is a free update to all existing users, and can be downloaded here:
<a href="https://itunes.apple.com/se/app/puck/id571254467">https://itunes.apple.com/se/app/puck/id571254467</a></p>
<p>I’ve got some ideas for extra features going forward, but if you have any suggestions, please let me know at <a href="mailto:james@pigonahill.com?subject=Puck 3">james@pigonahill.com</a>.</p>
<p>Click below for screen shots of Puck 3.0!</p>
<!-- READMORE -->
<div class="iOS-screen-shot">
<img class="iOS-screen-shot" src="/assets/puck/3-0/iOS/Screen-Shot-1.png" alt="Puck 3.0 Games" />
<img class="iOS-screen-shot" src="/assets/puck/3-0/iOS/Screen-Shot-2.png" alt="Puck 3.0 Game Details" />
<img class="iOS-screen-shot" src="/assets/puck/3-0/iOS/Screen-Shot-3.png" alt="Puck 3.0 Standings" />
<img class="iOS-screen-shot" src="/assets/puck/3-0/iOS/Screen-Shot-4.png" alt="Puck 3.0 News" />
<img class="iOS-screen-shot" src="/assets/puck/3-0/iOS/Screen-Shot-5.png" alt="Puck 3.0 Videos" />
<img class="iOS-screen-shot" src="/assets/puck/3-0/iOS/Screen-Shot-6.png" alt="Puck 3.0 Teams" />
</div>
<div class="watchOS-screen-shot">
<img class="watchOS-screen-shot" src="/assets/puck/3-0/watchOS/Screen-Shot-1.png" alt="Puck 3.0 Watch Live Games" />
<img class="watchOS-screen-shot" src="/assets/puck/3-0/watchOS/Screen-Shot-2.png" alt="Puck 3.0 Watch Live Games Details" />
<img class="watchOS-screen-shot" src="/assets/puck/3-0/watchOS/Screen-Shot-3.png" alt="Puck 3.0 Watch Standings" />
<img class="watchOS-screen-shot" src="/assets/puck/3-0/watchOS/Screen-Shot-4.png" alt="Puck 3.0 Watch News" />
<img class="watchOS-screen-shot" src="/assets/puck/3-0/watchOS/Screen-Shot-5.png" alt="Puck 3.0 Watch News Article" />
</div>James BarrowPuck 3.0 is finally here!WatchKit and watchOS 1 Apps2015-08-10T00:00:00+02:002017-05-21T15:57:37+02:00https://blog.pigonahill.com/2015/watchkit-and-watchos-1-apps<p>So over the last few weeks I’ve been on a bit of holiday. Some time away from work is always nice, some time to relax, and yet, I continue coding. 😛</p>
<p>However, I finally got myself an Apple Watch, as I managed to get my hands on one when I was over in the UK (I wanted to see what the colours really looked like) and they also came out in Sweden. So obviously I’ve been trying to make watch apps for my apps, namely Puck and Dog Bins. I thought it might be useful to post some of my findings from playing around with WatchKit in watchOS 1.</p>
<!-- READMORE -->
<h3 id="only-send-what-you-need">Only send what you need</h3>
<p>You want to limit the amount of data you are handling in your watch extension, either from the main app or from the internet. You don’t want to be sending an array or objects (probably dictionaries) full of key-values that the watch will never need or use.</p>
<p>What I did in these cases was, with my custom objects create a <code class="highlighter-rouge">simple() -> [String: AnyObject]</code> function. This would return a dictionary with just the basic key-values I wanted. You could even go further on that to have a <code class="highlighter-rouge">simple(keys: [String]) -> [String: AnyObject]</code> function. This would allow you to specify the keys you wanted from the custom object. Then you’d simply loop though the passed in keys and return the key-values in a dictionary.</p>
<p>This is a good practice at the moment, as both the extension and main app are running on the same device. However, when we get watchOS 2 later this year, the watch extension will be running on the watch and so this becomes even more important.</p>
<h3 id="do-not-use-openparentapplication_reply-for-network-operations">Do NOT use openParentApplication(_:reply:) for network operations</h3>
<p>This is exactly how I started using <code class="highlighter-rouge">openParentApplication(_:reply:)</code> but I soon realised this was adding extra steps that were causing issues. <code class="highlighter-rouge">openParentApplication(_:reply:)</code> is a synchronous call. This means that by the end of the function, you have to call the <code class="highlighter-rouge">reply()</code> callback, and if you don’t the the OS will complain at you. So if you kicked off an asynchronous task (which they should pretty much always be) you would get to the end of <code class="highlighter-rouge">openParentApplication(_:reply:)</code> and probably call <code class="highlighter-rouge">reply()</code> before you ever get any data back from your request.</p>
<p>This was the source of many issues I was having. I knew why it was doing it, but I couldn’t find a way around it. I even started playing with <code class="highlighter-rouge">dispatch_semaphore</code> etc before I realised, this thing that should be simple, I was making very complicated.</p>
<p><strong>Note:</strong> Ok, so sometimes you do need to hand off network request to the main app, but my point is, do it very sparingly.</p>
<p>Watch extension → Internet → watch extension<br />
is always going to be better than<br />
Watch extension → main app → internet → main app → watch extension.</p>
<h3 id="run-network-request-in-the-watch-extension">Run network request in the Watch extension</h3>
<p>Why not? You can even think of your watch extension as being a completely different app, in fact, you <em>should</em> think of it that way. It can still share the same code with you main iOS app with Frameworks obviously. And this is actually what they were introduced to do with iOS 8 and extensions (today, action, share, keyboard, etc). But again, this is another point that will become more obvious when you start to think about watchOS 2, as then your watch extension will be running on the watch itself. Give it it’s independence, let it explore the world on it’s own.</p>
<p>In fact, I managed to make the Puck 3 watch extension run completely independently from the main app if it had to. The only limitation is watch OS 1, but when watch OS 2 comes out and the extension is run on the watch, the phone could probably be off and it’d still work (on wifi only of course 😉).</p>
<h3 id="so-when-should-i-use-openparentapplication_reply">So when should I use openParentApplication(_:reply:)?</h3>
<p>In watch OS 1, very little, though there will be some instances when you need it. Getting a users location, getting locally saved data or transferring a locally saved image/file are probably the only reasons in watch OS 1 you’d need to use it. (watchOS 2 is a different matter, but we’ve got a whole framework to help us out there. Thank you Watch Connectivity.)</p>
<h3 id="share-as-much-data-as-possible">Share as much data as possible</h3>
<p>What I’m referring to here is shared containers. In Puck 3 I am using Core Data in the main iOS app, however the watch extension just uses the basic raw JSON from my server. I even use the same code in my framework called by both apps. However the main iOS app calls 2 functions, the watch extension just calls 1.</p>
<p>What I’ve done is made a function called <code class="highlighter-rouge">updateData(complete:)</code> which will get the JSON from my server, saves it to the shared container and also returns it in the <code class="highlighter-rouge">complete()</code> callback. This is what the watch extension uses.</p>
<p>The second called <code class="highlighter-rouge">updateDataForCoreData(complete:)</code> will first call <code class="highlighter-rouge">updateData(complete:)</code>, but then it will parse the data it has received into Core Data. This, as you might have guessed is what the main iOS app uses.</p>
<p>Apart from reusing code and not having duplicates, this allows me to make sure that the data saved into the shared container is always up to date. It doesn’t matter it the request was made from the watch extension or main iOS app, <code class="highlighter-rouge">updateData(complete:)</code> is always called, and so the JSON response is always saved to the shared container.</p>
<p>I even went a little further, going along with what I previously said about “only send what you need”. I only save certain sections to the shared container, as that is all that the watch extension needs. For example fixtures are not shown in the watch app, so there’s no point to save the raw JSON for 360+ games if I’m already saving them to Core Data for the only thing using them.</p>
<h3 id="the-error-the-uiapplicationdelegate-in-the-iphone-app-never-called-reply-in--uiapplicationdelegate-applicationhandlewatchkitextensionrequestreply-doesnt-always-mean-what-it-says">The error <code class="highlighter-rouge">The UIApplicationDelegate in the iPhone App never called reply() in -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]</code> doesn’t always mean what it says.</h3>
<p>I noticed there was a few times that I would call <code class="highlighter-rouge">reply()</code> in <code class="highlighter-rouge">openParentApplication(_:reply:)</code>, but I would still get this error. It seems that this is the <em>generic</em> error that we get given for watch extensions, and sadly, it’s all the feedback we get.</p>
<p>An example is that you can only return certain native classes in the <code class="highlighter-rouge">reply()</code> dictionary, NSData, NSString, NSNumber, NSDate, NSArray and NSDictionary. So if you reply with another object, or custom object, it will fail.</p>
<p>The error is not correct, but <code class="highlighter-rouge">reply()</code> never gets called, not because you didn’t call it, but because the iOS can’t parse the dictionary to send, and so crashes, so the watch extension complains it never receives a response.</p>
<h3 id="test-on-a-physical-device">Test on a physical device</h3>
<p>Now this is probably the most annoying though hopefully the most obvious point. Anyone who has been making iOS apps for a while knows that however nice the simulator can be, it also does perform very differently at times to actual hardware. This is even more the case with watch apps on watchOS 1.</p>
<p>Seeing as all we got with watchOS 1 was basically a second screen for the iPhone simulator (hell it’s even in the same menu are AirPlay to an AppleTV) there are some things that just aren’t the same. I found a few times that stating certain processes in my watch extension when the main app was never open on the physical device gave some stage results, something that you can’t reproduce with the simulator. There’s even features you just can’t test properly, like had off, at all in the simulator.</p>
<h3 id="conclusion">Conclusion</h3>
<p>So those are just some of the things I’ve run into while making watch apps in the last week or so. There are parts of this post of course that will become redundant when watchOS 2 comes out later this year. But there are also some ideas that can be used with the new APIs we’re getting, even if we don’t use <code class="highlighter-rouge">openParentApplication(_:reply:)</code> anymore.</p>James BarrowSo over the last few weeks I’ve been on a bit of holiday. Some time away from work is always nice, some time to relax, and yet, I continue coding. 😛State of Play, Mid 20152015-05-22T00:00:00+02:002017-05-21T15:57:37+02:00https://blog.pigonahill.com/2015/state-of-play-mid-2015<p>So I’ve been quiet for a little while about my own apps. I keep getting back to them every now and again, but the main thing is that simply, I just haven’t had the time. Let’s face it, none of my apps are “big” apps. They don’t make me any money that I could live off of, maybe enough for my developer licence and a beer.</p>
<p>Though that doesn’t matter to me. I don’t make my apps to make loads of money (however that wouldn’t be something I’d be against 😉). I make my apps to satisfy needed I have that I can’t find some other existing app to fill that need, and mainly, I make my apps to learn new things. Going though my current apps on the store:</p>
<ul>
<li>Pixel Pals was made for my dissertation at University.</li>
<li>Colour Cubes was made in about a week after seeing a very basic version of a similar game on my mates BlackBerry, and I wanted to see if I could remake it myself.</li>
<li>Dog Bins was made when my step-mum suggested it would be a good idea for the app, and I took it as a change to learn how to make an app with cloud feature, so I made it using <a href="http://parse.com" target="_blank">Parse</a> for the backend.</li>
<li>Puck was made because when I moved to Sweden, I got into Ice Hockey, and I wasn’t amazingly impressed by the apps available at the time, so I made my own. It also gave me the chance to learn Python and setup my server, along with push notifications and all that fun stuff.</li>
</ul>
<!-- READMORE -->
<p>Every app I’ve made, I’ve learnt something new on, and that’s sort of a conscious decision. So even if the app doesn’t make any money, I know I’ve learned something, something that will helpfully help one day when I make that app that might do well. You never know. I’ve made quite a few little apps when I’ve been learning new tech (UIDocument, CoreData, CoreLocation, Beacons, Sockets, etc) that will never see the light of day, but have been fundamental in things that have happened after them.</p>
<p>But that’s getting away from wanted I want to say here. I’m going to quickly run though each of my apps in the app store with a quick description of where they are and what I see happening with them in the future. I’m not going to say when anything will be done, because honestly, I have no idea when I’ll find the time do do any of what I want to do, but hopefully, one day. 😄</p>
<p>If you want to jump to any particular app that you’re interested in, I’ve added some quick links below. Otherwise, just read away.</p>
<ul>
<li><a href="#pixel-pals">Pixel Pals</a></li>
<li><a href="#colour-cubes">Colour Cubes</a></li>
<li><a href="#dog-bins">Dog Bins</a></li>
<li><a href="#puck">Puck</a></li>
</ul>
<h2 id="pixel-pals">Pixel Pals</h2>
<p>Pixel Pals is still the app that sells, consistently. Not a lot, but there’s always at least 1/2 sales a week, which always makes me happy. Especially as it’s an app that hasn’t gotten a proper update since the end of Match 2012<sup id="fnref:1"><a href="#fn:1" class="footnote">1</a></sup>. I have loads of plans for PP, the issue is time, though mainly logic.</p>
<p>The logic for the original game was, how can I say this… poor. It was my first ever app, and it was a crazy app for my first one. Kid’s make your first apps simple. DO NOT try and make something like Pixel Pals. I learnt a lot, and in quite a small space of time, but with any code that’s more than 8 months older, or more, it makes me want to cry when I look at how I did some things. So one of the fundamental features in a 2.0 version is to re-do the underlying logic.</p>
<p>The original version had issues with this, you could change the time zone and your creature would die, you could put your creature to sleep, close the app, and he would just sleep indefinitely, some weird sleeping beauty scenario and so on. There were some funny ones, that were planned, but sort of worked, for example if you feed your pet continuously (as there was no logic for your pet to tell you it was full, it’d just keep eating and eating) the next time it would goto the loo, it would… go to the <strong>loo</strong>. A screen full of poo.</p>
<p>However annoying or funny these were, the base issue was the fact the app can be closed. This is not an issue the original physical toys ever had. They’d always be on, always be ticking though the game loop, no worries. But an iOS app, users can close it, it can be closed by the operating system, lots of things could happen that disrupt this, so I started going about solving this… then I got in a muddle and haven’t touched it in about a year and a half.</p>
<p>One day I will get my head around it, one day I will not be thinking about a million other things to be able to get my head around it, and one day, I’ll probably get a few of my friends to double check my logic, just to make sure I’ve not completely messed it up. But for now it stays on the list of apps I <em>really</em> want to update, but just need to find the time and motivation to do so.</p>
<p>Any feature I’ve previously talked about for PP 2.0 are still completely on the table. More pets, more mini games, etc, just maybe with a few tweaks after 2+ years thinking about it in the back of my mind.</p>
<h2 id="colour-cubes">Colour Cubes</h2>
<p>Colour Cubes was always a fun little thing I did to test myself, that ended up becoming something I released. If you look at it now, it looks like pants. Though, the logic is pretty solid. I’ve been thinking lately about seeing if I can clean it up, revamp the whole design to something much cleaner, and maybe add a few features.</p>
<p>I had, a little while ago, an idea of how to make CC multiplayer, something I’m still interesting at coming back to. I’ve have a good few pages in a notebook from a year or so back that lays out how it could be done, logic wise, in some detail. Again, this being quite an old project now, its really needs a re-write (probably in Swift as well) but I feel it has the possibility of having something done to it at some point.</p>
<p>There is another thing with CC, because it’s based on what is basically a square of squares, it’s made me very interested to see if I could make it work on the Apple Watch. Obviously the higher levels of difficulty wouldn’t work as the hit boxes for each cube would be too small, but maybe. Either way I think I’m going to wait on that particular point until, at least, after WWDC 2015. Let’s see what more we get, as developers, for the Apple Watch this year, then we’ll see.</p>
<h2 id="dog-bins">Dog Bins</h2>
<p>Dog Bins was my first foray into working with a cloud based backend system. I learned a lot that I now take for granted from it, async processes probably being a big one. However, after the last big update, it fell prey to my lack of time. There’s enhancements I want to make to it, fixes that are needed and other things that could be improved… but time.</p>
<p>Dog Bins out of all my apps falls into it’s own category of being a free app. It has ads in it yes, and in app purchase to remove those ads, however, they make nothing. It has a decent user base, and the community has nearly logged 300 dog bins, which is awesome. However in the last year it has had 200 installs, though iAds have made $0.70 and in app purchases came to $0.71. That’s after Apple takes there stuff but before any tax. In short terms, it’s nothing, and sadly that’s something that bumps it down the list of priorities.</p>
<h2 id="puck">Puck</h2>
<p>Puck was the app where I learned how to code in Python on a server (along with setting up and maintaining that server, to a degree) and dealing with push notifications. I was my test case when Swift came out last year at WWDC 2014 to dive into Apple’s new programming language. And that was also what made it instantly clear, that Swift was not ready when it was released to remake a whole app in it. Now however with Swift 1.2, I feel like it is and so I’ve gone back to what I started on with re-writing Puck in Swift all those months ago.</p>
<p>However, because of the nature of how much Swift has changed, and with how much I’ve learned since then on how you should do things in Swift compared to Obj-C, it needs to be pretty much scrapped and started again.</p>
<p>Puck has the extra component of being a 2 sided application, as it also has the backend that needs constant updating and maintenance as well. With the latest version of Puck, I’ve started looking into redoing my backend for the 3rd time, as, like with 2.0 last year, I’ve started going though the app and wanting to add this or that, and realised my backend data isn’t optimised to do those tasks. This of course makes the process longer, but I hope, in the long run makes the whole application and service that much better.</p>
<p>Again, Puck is another perfect candidate for an Apple Watch application, more so than Colour Cubes, as what we have in WatchKit is more than enough for what I need for Puck, but without that new main app backing it up, it has to wait.</p>
<p>I do feel Puck is the app that I expect an update being released for soonest, and I do now have the summer break before the new season begins to get this done in. But we will have to see. Like in all of my apps, I have lots ideas I want to add or improve on existing features, it’s just finding the time to do so.</p>
<h2 id="so-to-sum-up">So to sum up…</h2>
<p>Time, time, time… I think that pretty much sums it up. I’ve got lot of ideas, and even though 4 apps doesn’t sound a lot, when there’s just one of you, 4 apps becomes a lot to go though. You can say that I’ve kind of shot myself in the foot by leaving some of them for a while, and so making the workload more, but sadly that’s just the way it is.</p>
<p>As always, if I have any more updates as time goes on, I will try and post them here, but I hope this answers any questions, even though it might not answer them in the way you wanted.</p>
<div class="footnotes">
<ol>
<li id="fn:1">
<p>I don’t count the update in October 2013, as it was really just a fix for an issue that arose with iOS 7. <a href="#fnref:1" class="reversefootnote">↩</a></p>
</li>
</ol>
</div>James BarrowSo I’ve been quiet for a little while about my own apps. I keep getting back to them every now and again, but the main thing is that simply, I just haven’t had the time. Let’s face it, none of my apps are “big” apps. They don’t make me any money that I could live off of, maybe enough for my developer licence and a beer.Static Site Generators2015-03-19T00:00:00+01:002017-05-21T15:57:37+02:00https://blog.pigonahill.com/2015/static-site-generators<p><strong>PRE-POST NOTE:</strong> The origins of this post really starts in another post. I was having issues with Apache and my Wordpress setup, and those issues made me move to Nginx and then to using a static site generator. To read more of those details that got me to this point, read here: <a href="/2015/apache-woes-hello-nginx/">Apache Woes, Hello Nginx!</a></p>
<p>I have been using Wordpress since I started my site, I think it must have been back in 2010-2011, and it’s done all I’ve wanted it to do, and more. But it’s always felt too much than I needed. I’ve tried to dive into making my own themes a couple of times, but swiftly given up, just because the amount of <em>stuff</em> there was, it overwhelmed me. So after the problems I was having with my server (see pre-post note), I decided to move to a static site.</p>
<p>What is a static site? Static site generators are programs that take templates and content you make and render the content into separate, compiled files. This means, unlike a dynamic site, the content is already created and ready to serve to the requesting web browser, unlike a dynamic site, like Wordpress, that renders the content and creates the web page (in Wordpress’ case using PHP) when and every time it is requested (though obviously facing systems can and are used, but still).</p>
<!-- READMORE -->
<p>This instantly gives me several benefits when using a static site, the most prominent in my eyes is that we get an enormous speed boost. I have never used my blog in a big way, I’ve never managed to keep up a good posting schedule, and it’s always been just me posting. So all of the <em>mass</em> features that Wordpress has, multiple users, massive plugin expandability with a few clicks, I’ve never needed.</p>
<p>It was mentioned to me by a friend<sup id="fnref:1"><a href="#fn:1" class="footnote">1</a></sup> that he used a static site generator for his blog. He uses <a href="https://middlemanapp.com">Middleman</a>, but there were others such as <a href="http://jekyllrb.com">Jekyll</a> and then from there I also found <a href="http://octopress.org">Octopress</a> (which is built on top of Jekyll).</p>
<p>For your site, you can choose which you feel is best, but after a bit of searching around, reading comments and looking into the separate docs, I decided to go with Middleman. It was mainly for 3 reasons;</p>
<ol>
<li>It was in Ruby, which I haven’t really used before<sup id="fnref:2"><a href="#fn:2" class="footnote">2</a></sup>, so bring on something new!</li>
<li>I liked the site, the documentation seemed simple to understand and to the point and there wasn’t an excess to <em>stuff</em> that I didn’t need to bog it down,</li>
<li>My friend Matt used it. I think it’s important to use things that have been recommended to you by people who’s judgement you trust (and as a side point, who can help you if it isn’t working or you break something 😉).</li>
</ol>
<p>So I started setting up my middleman-blog site, and after a while of tinkering, adding this, moving that, (pestering Matt when it something didn’t work,) I have my new blog up and running. It was taking me a while to get used to using Ruby and then Haml as my templating markup language, but I was learning, and getting there. And the main point, it was fun and simple. Nothing ever happened that made me throw my arms up and walk away from my computer.</p>
<p>Of course, there were a few issues I had (like my code syntax css file refusing to generate on build, I had to save the <code class="highlighter-rouge">.css</code> file manually myself) etc, but overall, things were going well.</p>
<h3 id="exporting-blog-posts-from-wordpress">Exporting Blog Posts from Wordpress</h3>
<p>With my new Middleman blog setup, now I needed content. Well I had that, but it was all in Wordpress, wrapped up in the MySQL database. Luckily I am not the first to make the switch and <a href="https://github.com/mdb">Mike Ball</a> has made a great little Ruby Gem called <a href="https://github.com/mdb/wp2middleman">wp2middleman</a> that takes the <code class="highlighter-rouge">.xml</code> file you can export from Wordpress and turns it into markdown files<sup id="fnref:3"><a href="#fn:3" class="footnote">3</a></sup>. After that, it’s just a matter or sorting the post files into the organisational system you’ve chosen for you blog, and off you go.</p>
<p><strong>Note:</strong> There are obviously some things you need to keep an eye out for. For example, Wordpress does love to add certain <code class="highlighter-rouge"><p></code> and <code class="highlighter-rouge"><div></code> tags along with ids and classes. Noramally this happens when you’ve told your posts content to formatter in a certain way (entered, justified, etc). Also, there’s the <code class="highlighter-rouge"><!--more--></code> line where you want to break you post summary on the main page. Though these should be easily fixed with something like SublimeText’s (or whatever editor you’re using) find and replace feature, it’s something to look out for.</p>
<h3 id="disqus-comments">Disqus Comments</h3>
<p>Comments were one of the last things to move over, mainly because I was expecting them to be a painful experience to move over, but I was quite wrong there. Now I don’t get a lot of comments on my blog, but I have a few and I didn’t want to start from scratch. So I looked around a bit for what other people use. I wasn’t feeling like using something such as Facebook or Google+, those commenting systems always seem a bit cluttered for my tastes (and constantly trying to drag you back to the sites respectively).</p>
<p>I then found a very basic, but obviously powerful system called <a href="http://pooleapp.com">Poole</a> which at first seemed exactly what I wanted, but after a deeper looked into it was a great but very basic system. I wasn’t looking for something I had to build up from scratch, at least, not at this point in time. I just wanted to have something I could dump at the bottom of my posts, import my old Wordpress comments to and carry on with my life.</p>
<p><a href="https://disqus.com">Disqus</a> was the answer to this. It’s a simple commenting system, closer to that of Facebook or Google+ than Poole, but it doesn’t feel as <em>loud</em> on the page. Also, it has the ability (via a <a href="https://wordpress.org/plugins/disqus-comment-system/">Wordpress plugin</a>) to export comments from your Wordpress blog. All boxes ticked, and after that, it was a simple few lines to implement.</p>
<p><code class="highlighter-rouge">Gemfile</code></p>
<div class="language-ruby highlighter-rouge"><pre class="highlight"><code><span class="n">gem</span> <span class="s2">"middleman-disqus"</span>
</code></pre>
</div>
<p><code class="highlighter-rouge">config.rb</code></p>
<div class="language-ruby highlighter-rouge"><pre class="highlight"><code><span class="c1"># Disqus Comments</span>
<span class="n">activate</span> <span class="ss">:disqus</span> <span class="k">do</span> <span class="o">|</span><span class="n">d</span><span class="o">|</span>
<span class="n">d</span><span class="p">.</span><span class="nf">shortname</span> <span class="o">=</span> <span class="s1">'short_name'</span>
<span class="k">end</span>
</code></pre>
</div>
<p>And then in your article layout file, <code class="highlighter-rouge">article.ham</code> for me.</p>
<div class="language-haml highlighter-rouge"><pre class="highlight"><code><span class="p">=</span> <span class="n">disqus</span>
<span class="p">=</span> <span class="n">disqus_count</span>
</code></pre>
</div>
<p>And you’re done. There are a few extra things to do in Disqus, but you can find them in the <a href="https://disqus.com/admin/create/">admin panel</a> on the Disqus site.</p>
<h3 id="wrap-up">Wrap-up</h3>
<p>Overall I’m currently in love with static sites. They’re so fast, and now I’ve got my foot in the door and feel I understand the basics, I can really see some amazing places I can go with it. That along with the fact my blog is now amazingly fast compared to how it was when running via Wordpress, I can’t wait to base some more sites off of it.</p>
<p>I am not condemning Wordpress, or dynamic sites in general. In some cases (and there’s a lot of them) it’s exactly what’s needed. But for me and for this, Middleman and a static site works a charm. I just have to think of some other ways to use it now.</p>
<div class="footnotes">
<ol>
<li id="fn:1">
<p>My good friend Matt is normally the guy who helps me fix the issues I get myself into when it comes to my server. I only wish I can repay his help when it comes to iOS development as much as he helps me with Server stuff. Check out his blog here: <a href="https://mattprice.me">https://mattprice.me</a> <a href="#fnref:1" class="reversefootnote">↩</a></p>
</li>
<li id="fn:2">
<p>Up until now, when I’ve stayed away from iOS, Obj-C and Swift, it’s been to do things in Python, but it’s always nice to have a change. <a href="#fnref:2" class="reversefootnote">↩</a></p>
</li>
<li id="fn:3">
<p>it also seems to convert some other items like custom menu items etc, if you you’ve added that type of thing to your Wordpress blog, but they’re not hard to filter out. <a href="#fnref:3" class="reversefootnote">↩</a></p>
</li>
</ol>
</div>James BarrowPRE-POST NOTE: The origins of this post really starts in another post. I was having issues with Apache and my Wordpress setup, and those issues made me move to Nginx and then to using a static site generator. To read more of those details that got me to this point, read here: Apache Woes, Hello Nginx!Apache Woes, Hello Nginx!2015-03-18T00:00:00+01:002017-05-21T15:57:37+02:00https://blog.pigonahill.com/2015/apache-woes-hello-nginx<p>A couple of weeks ago I started getting emails from Jetpack<sup id="fnref:1"><a href="#fn:1" class="footnote">1</a></sup> telling me that my Wordpress blog was down. I was too busy at the time, but I came back to look into it around a week later. I was amazed to see that what the issue was that MySQL wasn’t running on my server. It took a while to work out what was wrong, but the long and short of it is I ran out of memory.</p>
<p>Now my server isn’t the most powerful one out there. It’s a 2GB Linode VPS<sup id="fnref:2"><a href="#fn:2" class="footnote">2</a></sup> but that should be more than powerful enough to run a simple Wordpress site. A deeper look into it showed me that Apache was using 1.99GB/2.0GB! So there’s the culprit.</p>
<!-- READMORE -->
<p>Apache is what I’ve used since the beginning, since I first got a VPS about 3 years ago. But now it’s stabbed me in the back using up all of my memory. So I scrapped it. 😛 Goodbye Apache, hello Nginx.</p>
<p>Nginx is a lightweight web server, and I could probably write a whole post of it in itself (actually, that’s sort of what this has become). After installing, setting it up and then removing Apache, I went from my web server using up 1.99GB to using around 6-8MB. Now that, that is a massive improvement. Right, so lets get things running again. I setup PHP-FastCGI so my Wordpress site can work and I left it alone.</p>
<p>20 minutes later and Jetpack emails me saying that my blog is down again. Back to square 1. 😟 What’s going on now? Apparently PHP-FastCGI is doing the same thing that Apache was, though it’s capping itself to ~240MB. However it’s still getting to that cap and not releasing anything to be able to keep going. I did a bit of research and tried optimising (like I did with Apache before moving to Nginx) but nothing seems to work. 20 minutes after every time I restarted PHP-FastCGI, my Wordpress site would go down, pretty much on the dot.</p>
<p>So, I feel we’ve found the issue. Wordpress. For whatever reason it’s just being a fat memory hog. And after being so happy in optimising my memory usage when swapping to Nginx, I’m not going to let my blog do the same thing.</p>
<p>I ended up moving my whole blog to a static site (which I’ve written about here <a href="/2015/static-site-generators/">Static Site Generators</a>) and that sorted out my issues. I am now using between 180MB and 220MB for standard usage. Which makes me extremely happy compared to to the over 1.99GB previously.</p>
<p>Setting up Nginx was simple, you install it was <code class="highlighter-rouge">sudo apt-get install nginx</code> and then you and then start it with <code class="highlighter-rouge">sudo service nginx start</code>. And hey presto, you’re pretty much up and running. Linode has a load of really nice tutorials on it, though I’ll mainly link this one, which details the basics (I’m sure you can find more specific ones for your own distributions with a simple search) <a href="https://www.linode.com/docs/websites/nginx/websites-with-nginx-on-debian-7-wheezy" target="_blank">Websites with Nginx on Debian 7 (Wheezy)</a>.</p>
<p>Unlike Apache, Nginx does require you to restart the web server every time you change something in either the main config file or one of your sites individual config files, but that takes less than a second using <code class="highlighter-rouge">sudo service nginx restart</code>, so it’s not really an issue, just something to note.</p>
<p>One final note about PHP-FastCGI. If you have anything that requires PHP (for me, once I’d removed Wordpress it was just a token parsing script for APNs) then you will need to install and configure FastCGI. It is a pain in the arse, as there are so many conflicting sources out there on how to get it to work, all slightly different from each other. I can write another post, if people want it, about configuring a Nginx site to send PHP to FastCGI if people request it, but for now I will offer up the link that saved my hide and finally got it all working. <a href="http://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm/" target="_blank">Solving “502 Bad Gateway” with nginx & php-fpm</a>.</p>
<p>In conclusion, Nginx is my new best friend when it comes to web servers. It’s easy to setup, it’s not impossible to understand the config file (unlike Apache where I always got lost instantly) and it takes up little to no memory when running a set of simple sites<sup id="fnref:3"><a href="#fn:3" class="footnote">3</a></sup>. If you’re looking into getting a VPS and are wondering if you go for this widely talk about server Apache, I’d say wait, do you <em>need</em> it? If the answer is anything but “they’re paying me to use it”, then you should use Nginx.</p>
<div class="footnotes">
<ol>
<li id="fn:1">
<p>Jetpack is a tool built into Wordpress that allows access to a range of tools, one of which being a status tool that emails the site’s admin if it detects the site being down. <a href="#fnref:1" class="reversefootnote">↩</a></p>
</li>
<li id="fn:2">
<p>If you feel like using Linode, which I highly recommend as a VPS provider, then check them out here with my referral link: <a href="https://www.linode.com/?r=398f3b1ce56e745028c920f81e56d1cbb13f57bf" target="_blank">Linode</a>. <a href="#fnref:2" class="reversefootnote">↩</a></p>
</li>
<li id="fn:3">
<p>In my case, I’m running 4-5 low traffic sites, and some JSON feeds for some of my iOS apps to access. <a href="#fnref:3" class="reversefootnote">↩</a></p>
</li>
</ol>
</div>James BarrowA couple of weeks ago I started getting emails from Jetpack1 telling me that my Wordpress blog was down. I was too busy at the time, but I came back to look into it around a week later. I was amazed to see that what the issue was that MySQL wasn’t running on my server. It took a while to work out what was wrong, but the long and short of it is I ran out of memory. Jetpack is a tool built into Wordpress that allows access to a range of tools, one of which being a status tool that emails the site’s admin if it detects the site being down. ↩Swift, Optionals and nil2014-06-07T00:00:00+02:002017-05-23T00:59:06+02:00https://blog.pigonahill.com/2014/swift-optionals-and-nil<p>Optionals are something that I had never heard of before Swift came out. I have coded in quite a few languages, but as far as I’m aware, I hadn’t come across this thing. But it seems to be everywhere in Swift, so what is it? It took me a little while to sit down and work that out, but I now feel like I’ve wrapped my head around it.</p>
<p><code class="highlighter-rouge">nil</code> is something I use on a daily basis, but it’s not the same in Swift. It looks like it is, but under the hood, it really isn’t. Below I’m going to talk about how Swift deals with optionals, this new way of thinking of <code class="highlighter-rouge">nil</code> and why I’ve come to realise and really love this new way of looking at things.</p>
<!-- READMORE -->
<p>In Obj-C if we create a variable, let’s say its an NSString, we’d create it with something like the following:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="kt">NSString</span> <span class="n">myString</span> <span class="o">=</span> <span class="kc">nil</span><span class="p">;</span>
<span class="cm">/* some code */</span>
<span class="n">myString</span> <span class="o">=</span> <span class="s">@"Hello World"</span><span class="p">;</span>
</code></pre>
</div>
<p>This string has no value on creation, so we set it, or should I say the pointer, to <code class="highlighter-rouge">nil</code> then somewhere else we set it to the string <code class="highlighter-rouge">Hello World</code>. This is something I use every day in lots of different places.</p>
<p>So in Swift you could assume that we would write the following:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myString</span><span class="p">:</span> <span class="kt">String</span> <span class="o">=</span> <span class="kc">nil</span>
</code></pre>
</div>
<p>This will produce a compile error before we even got to trying to assign anything to our variable later on. Why? Well a variable in Swift can not be set to <code class="highlighter-rouge">nil</code> as <code class="highlighter-rouge">nil</code> is not the same thing. But we’ll come back to that in a second.</p>
<p>So to make this work we need to make myString into an optional. For this the syntax is very simple addition of a question mark <code class="highlighter-rouge">?</code> after the type declaration.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myString</span><span class="p">:</span> <span class="kt">String</span><span class="p">?</span> <span class="o">=</span> <span class="kc">nil</span>
<span class="cm">/* some code */</span>
<span class="n">myString</span> <span class="o">=</span> <span class="s">"Hello World"</span>
</code></pre>
</div>
<p>Now we get no compile time errors and we can then go and set our variable to “Hello World” somewhere else in our code.</p>
<p>So why? Why would we want to have the ability to not be able to set a variable to <code class="highlighter-rouge">nil</code> and to have to add an extra character to make it even compile?</p>
<p>Well we have to look at how the two languages treat <code class="highlighter-rouge">nil</code>. To quote from the iBook that Apple realised “The Swift Programming Guide”,</p>
<blockquote>
<p>In Objective-C, <code class="highlighter-rouge">nil</code> is a pointer to a non-existant object. In Swift <code class="highlighter-rouge">nil</code> is not a pointer - it is the absence of a value of a certain type.</p>
</blockquote>
<p>The important bit to note here is that <code class="highlighter-rouge">nil</code> in Swift is nothing to do with a pointer, it’s the absence of a value altogether. If you were able to set <code class="highlighter-rouge">nil</code> to any variable at any time, you would have the ability to call something on <code class="highlighter-rouge">nil</code> which would crash your code instantly. So to fix that we have optionals, which make everything safe.</p>
<p>If a variable can’t be set as <code class="highlighter-rouge">nil</code>, then you always know if you call something on it, it won’t crash!</p>
<p>I just said that if you have a variable you can set to <code class="highlighter-rouge">nil</code> then you could call something on it and cause a crash. So couldn’t we just call something on an optional, and so crash out code in the same way. Well Swift has already though of that for us. To be able to access an optional you have to check it contains a value before you use it, otherwise we get a compile time error.</p>
<p>This means calling something like the following will not compile:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myCar</span><span class="p">:</span> <span class="kt">Car</span><span class="p">?</span> <span class="o">=</span> <span class="kc">nil</span>
<span class="n">myCar</span><span class="o">.</span><span class="nf">fillTank</span><span class="p">()</span>
</code></pre>
</div>
<p>To fix this we can wrap it in an if statement that checks to see if <code class="highlighter-rouge">myCar</code> has a value assigned to it.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myCar</span><span class="p">:</span> <span class="kt">Car</span><span class="p">?</span> <span class="o">=</span> <span class="kc">nil</span>
<span class="k">if</span> <span class="n">myCar</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="n">myCar</span><span class="o">!.</span><span class="nf">fillTank</span><span class="p">()</span>
<span class="p">}</span>
</code></pre>
</div>
<p>This is great but what’s the exclamation mark <code class="highlighter-rouge">!</code> Optional variables wrap up their values, which means when we want to access them again we have to unwrap them. That’s what the exclamation mark <code class="highlighter-rouge">!</code> is doing, it’s telling the compiler, “Please unwrap this variable. I would like to use it.” and the compiler unwraps the variable and passes it back. This is fine in our 1 line example here, but what happens when you want to do a lot of things on myCar? Even 10 lines of code, having to type <code class="highlighter-rouge">!</code> to unwrap a variable every time would get a little annoying. And we know, from the way Swift can imply types and other places, that Swift was built with inference in mind. So what about trying this?</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myCar</span><span class="p">:</span> <span class="kt">Car</span><span class="p">?</span> <span class="o">=</span> <span class="kc">nil</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">car</span> <span class="o">=</span> <span class="n">myCar</span> <span class="p">{</span>
<span class="n">car</span><span class="o">.</span><span class="nf">fillTank</span><span class="p">()</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Here we’ve managed to do a few things, we’ve created a new constant reference of <code class="highlighter-rouge">myCar</code> called <code class="highlighter-rouge">car</code>, we’ve done this in the logic of the if statement and we no longer have to unwrap anything. Swift and the compiler is cleaver (if you hadn’t already picked that up), it knows that we’ve created a constant <code class="highlighter-rouge">car</code> and assigned <code class="highlighter-rouge">myCar</code> to it. But we’ve done it in an if statement, so it knows it’s being requested to check if <code class="highlighter-rouge">car</code> actually gets populated with a value. It automatically unwraps <code class="highlighter-rouge">myCar</code>, and if it finds a value, sets it to <code class="highlighter-rouge">car</code> and goes into the statements scope. If it finds nothing, it just continues. This allows us to have a unwrapped version of <code class="highlighter-rouge">myCar</code> in <code class="highlighter-rouge">car</code>, so anything we want to perform, we can perform on car within the scope of the if statement. ARC being awesome as well, as soon as we leave that scope <code class="highlighter-rouge">car</code> is cleaned up and we can carry on using <code class="highlighter-rouge">myCar</code>.</p>
<p>But, to use a phase I’ve heard a lot watching the WWDC session videos on Swift, “we can do better”. This is still 3 lines of code, to check and call the function on <code class="highlighter-rouge">myCar</code> if it doesn’t contain <code class="highlighter-rouge">nil</code>. Wouldn’t it be amazing if we could do this in 1 line?</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myCar</span><span class="p">:</span> <span class="kt">Car</span><span class="p">?</span> <span class="o">=</span> <span class="kc">nil</span>
<span class="n">myCar</span><span class="p">?</span><span class="o">.</span><span class="nf">fillTank</span><span class="p">()</span>
</code></pre>
</div>
<p>What’s happening here now? Well, we’re taking our instance of <code class="highlighter-rouge">myCar</code>, then we have a question mark <code class="highlighter-rouge">?</code> and then we try to <code class="highlighter-rouge">fillTank()</code>. In this case the question mark <code class="highlighter-rouge">?</code> is not telling us we want to use an optional, as we did that in the declaration. Here we’re using it to check <code class="highlighter-rouge">myCar</code> has an actual value. You can think of us actually using it to ask a question. “Does <code class="highlighter-rouge">myCar</code> exist?” If it doesn’t, our code breaks out and continues on its merry way. If it does have a value assigned to it, it calls the next item in the chain and so on. This is called <em>optional chaining</em>.</p>
<p>These are of course simple examples, and as such it doesn’t look that impressive. Ok, let’s make something a little bit bigger. Imagine we have a garage, that garage has customer who in turn has a car. A car has a thing that need to be done on it and each thing is either complete or it isn’t. In code we might represent this with <code class="highlighter-rouge">Garage</code>, <code class="highlighter-rouge">Customer</code>, <code class="highlighter-rouge">Car</code> and <code class="highlighter-rouge">Task</code> objects. Where a task has a <code class="highlighter-rouge">Bool</code> that represents if the task is compete or not. Now if we were to set this in code with if statements it’d look like this.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myGarage</span><span class="p">:</span> <span class="kt">Garage</span><span class="p">?</span> <span class="o">=</span> <span class="kt">Garage</span><span class="p">()</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">garage</span> <span class="o">=</span> <span class="n">myGarage</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">customer</span> <span class="o">=</span> <span class="n">garage</span><span class="o">.</span><span class="n">customer</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">car</span> <span class="o">=</span> <span class="n">customer</span><span class="o">.</span><span class="n">car</span> <span class="p">{</span>
<span class="k">if</span> <span class="k">let</span> <span class="nv">task</span> <span class="o">=</span> <span class="n">car</span><span class="o">.</span><span class="n">task</span> <span class="p">{</span>
<span class="n">task</span><span class="o">.</span><span class="n">completed</span> <span class="o">=</span> <span class="kc">true</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>
<p>What a MESS! Yes, this will compile and run, and if a car doesn’t actually have any tasks, then it won’t go into try to set <code class="highlighter-rouge">true</code> to <code class="highlighter-rouge">nil</code> and the code will carry on its way. But that’s 3 nested if statements spanning 9 Ïlines of code! Not exactly concise. Let’s do that again with optional chaining.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myGarage</span><span class="p">:</span> <span class="kt">Garage</span><span class="p">?</span> <span class="o">=</span> <span class="kt">Garage</span><span class="p">()</span>
<span class="n">myGarage</span><span class="p">?</span><span class="o">.</span><span class="n">customer</span><span class="p">?</span><span class="o">.</span><span class="n">car</span><span class="p">?</span><span class="o">.</span><span class="n">task</span><span class="p">?</span><span class="o">.</span><span class="n">completed</span> <span class="o">=</span> <span class="kc">true</span>
</code></pre>
</div>
<p>1 line. And this compiles, and will work in the exact way the nested if statement did. And actually, that’s not even a very complicated example, but I hope now you can see the point and how powerful this feature can be.</p>
<p>Swift is a language that wants to help you, and it does. We can make things that could before time multiple lines of code, work in just a few, if not one. It’s reasons like this that are making me love Swift, and I can’t wait until later this year when it gets released to the world and I can start writing full apps in it.</p>
<p>If you haven’t downloaded the book “The Swift Programming Language” from the iBooks store, you really should. That along with the Apple documentation and WWDC sessions are all great places to find more information about Swift.</p>
<p>For any of the sessions from WWDC 2014 find them at: <a herf="https://developer.apple.com/videos/wwdc/2014/" target="_blank">developer.apple.com/videos/wwdc/2014/</a></p>
<p>Also check out the excellent book that Apple released that goes though the Swift language in a lot of detail: <a herf="https://itunes.apple.com/se/book/swift-programming-language/id881256329?l=en&mt=11" target="_blank">Swift Programming Language</a></p>James BarrowOptionals are something that I had never heard of before Swift came out. I have coded in quite a few languages, but as far as I’m aware, I hadn’t come across this thing. But it seems to be everywhere in Swift, so what is it? It took me a little while to sit down and work that out, but I now feel like I’ve wrapped my head around it.Swift, The Basics2014-06-06T00:00:00+02:002017-05-23T00:59:06+02:00https://blog.pigonahill.com/2014/swift-the-basics<p>There are some awesome things happening when you look at Swift as a basic language. Swift is not another language built off of C, it’s a brand new language. This means that there are no performance or historical drawbacks to Swift, as it doesn’t rely on an underling langue.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">name</span> <span class="o">=</span> <span class="s">"James"</span>
<span class="k">var</span> <span class="nv">greeting</span> <span class="o">=</span> <span class="s">"Hello "</span>
<span class="n">greeting</span> <span class="o">+=</span> <span class="n">name</span>
<span class="nf">println</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">greeting</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="o">>></span> <span class="kt">Hello</span> <span class="kt">James</span>
</code></pre>
</div>
<p>So in this simple code snippet you can instantly see some differences compared to the normal Obj-C code you might be used to. For starters their aren’t any semicolons <code class="highlighter-rouge">;</code>. You just don’t need semicolons in Swift. You can use them if you want, but they’re not required (unless in specific circumstances).</p>
<!-- READMORE -->
<h3 id="var-and-let">var and let</h3>
<p>You might have also noticed that there are two different versions of string, <code class="highlighter-rouge">var</code> and <code class="highlighter-rouge">let</code>. A <code class="highlighter-rouge">var</code> is the representation of a variable and mutable, where as <code class="highlighter-rouge">let</code> is the representation of a constant and so immutable.</p>
<p>Put simply <code class="highlighter-rouge">let</code> can only have its value set when it is created. Where as <code class="highlighter-rouge">var</code> allows you to change the value after the item has been created. This can been seen in the code snippet above where we concatenated the <code class="highlighter-rouge">Hello</code> string to the <code class="highlighter-rouge">name</code> constant and assign it to the greeting variable.</p>
<p>Apple has gone to a lot of effort to make Swift a very safe language. This means that if you do something wrong, more often than not you will get a compile time error. Where as in Obj-C we could make these errors, compile and run and then get the app crashing and have to go back and find where we went wrong.</p>
<h3 id="type-declaration">Type Declaration</h3>
<p>The other thing we have there is how we stated that the variables were strings… oh wait, we didn’t. Swift is a type-inferance language. This means that <code class="highlighter-rouge">name</code> knows that it should be of the type <code class="highlighter-rouge">String</code> due to the fact we assign it a string. We can declare the type if we wanted to and then we’d get the following:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myString</span><span class="p">:</span> <span class="kt">String</span> <span class="o">=</span> <span class="s">"Hello World"</span>
</code></pre>
</div>
<h3 id="types">Types</h3>
<p>Swift gives us the standard types that you would expect:</p>
<ul>
<li><code class="highlighter-rouge">String</code></li>
<li><code class="highlighter-rouge">Bool</code></li>
<li><code class="highlighter-rouge">Int</code></li>
<li><code class="highlighter-rouge">UInt</code></li>
<li><code class="highlighter-rouge">Double</code></li>
<li><code class="highlighter-rouge">Float</code></li>
<li><code class="highlighter-rouge">Array</code></li>
<li><code class="highlighter-rouge">Dictionary</code></li>
</ul>
<p><code class="highlighter-rouge">String</code> and <code class="highlighter-rouge">Bool</code> are pretty self-explanatory, but <code class="highlighter-rouge">Int</code> and <code class="highlighter-rouge">UInt</code> might need a little more explanation. These work very much like NSInteger and NSUInteger, on a 32-bit systems they are <code class="highlighter-rouge">Int32</code> or <code class="highlighter-rouge">UInt32</code> and on 64-bit systems they are <code class="highlighter-rouge">Int64</code> or <code class="highlighter-rouge">UInt64</code> respectively. As you might have guessed from my explanation there, you can also define an integer in 8, 16, 32 and 64 bit forms. It’s probably worth mentioning that Apple suggests that you use <code class="highlighter-rouge">Int</code>, even when you know you won’t use any negatives values.</p>
<p><code class="highlighter-rouge">Double</code> and <code class="highlighter-rouge">Float</code> are the 64-bit and 32-bit representations of floating point numbers respectively, and don’t need much more or an explanation than that.</p>
<p><code class="highlighter-rouge">Array</code> and <code class="highlighter-rouge">Dictionary</code> are created in a visually very similar way. They use square brackets <code class="highlighter-rouge">[]</code> to encapsulate the values and key-values.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">var</span> <span class="nv">myArray</span> <span class="o">=</span> <span class="p">[</span><span class="s">"Hello"</span><span class="p">,</span> <span class="s">"Bonjour"</span><span class="p">,</span> <span class="s">"Hej"</span><span class="p">]</span>
<span class="k">var</span> <span class="nv">myDictionary</span> <span class="o">=</span> <span class="p">[</span><span class="s">"Dog"</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="s">"Snake"</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s">"Ant"</span><span class="p">:</span> <span class="mi">6</span><span class="p">]</span>
</code></pre>
</div>
<p>Adding items to an <code class="highlighter-rouge">Array</code> is a simple as using <code class="highlighter-rouge">+=</code> and adding to a <code class="highlighter-rouge">Dictionary</code> is just adding the new key in <code class="highlighter-rouge">[]</code> and then assigning the value.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="n">myArray</span> <span class="o">+=</span> <span class="s">"Hallo"</span>
<span class="n">myDictionary</span><span class="p">[</span><span class="s">"Kangaroo"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
</code></pre>
</div>
<p>And lastly retrieving values can be done by the follow:</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">greeting</span> <span class="o">=</span> <span class="n">myArray</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="nf">println</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">greeting</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="o">>></span> <span class="kt">Hej</span>
<span class="k">let</span> <span class="nv">legs</span> <span class="o">=</span> <span class="n">myDictionary</span><span class="p">[</span><span class="s">"Dog"</span><span class="p">]</span>
<span class="nf">println</span><span class="p">(</span><span class="s">"</span><span class="se">\(</span><span class="n">legs</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
<span class="o">>></span> <span class="mi">4</span>
</code></pre>
</div>
<p>These are simple and logical, but it’s worth mentioning as it’s useful to know how to do the simple things.</p>
<h3 id="if-and-switch-statements">If and Switch Statements</h3>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">inProcess</span> <span class="o">=</span> <span class="kt">True</span>
<span class="k">if</span> <span class="n">inProcess</span> <span class="o">==</span> <span class="kt">True</span> <span class="p">{</span>
<span class="nf">println</span><span class="p">(</span><span class="s">"In Process"</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>There might be an obvious omission to if statements in Swift, you don’t have to have parentheses <code class="highlighter-rouge">()</code>. Like the semicolon at the end of every line, you can continue to do so if you want to, but there’s no real need.</p>
<div class="language-swift highlighter-rouge"><pre class="highlight"><code><span class="k">let</span> <span class="nv">myNumber</span> <span class="o">=</span> <span class="mi">5</span>
<span class="k">switch</span> <span class="n">myNumber</span> <span class="p">{</span>
<span class="k">case</span> <span class="mi">0</span><span class="p">:</span>
<span class="nf">println</span><span class="p">(</span><span class="s">"Zero"</span><span class="p">)</span>
<span class="k">case</span> <span class="mi">1</span><span class="o">...</span><span class="mi">10</span><span class="p">:</span>
<span class="nf">println</span><span class="p">(</span><span class="s">"Top ten!"</span><span class="p">)</span>
<span class="k">case</span> <span class="mi">11</span><span class="o">..</span><span class="mi">100</span><span class="p">:</span>
<span class="nf">println</span><span class="p">(</span><span class="s">"Eleven to ninety nine"</span><span class="p">)</span>
<span class="k">default</span><span class="p">:</span>
<span class="nf">println</span><span class="p">(</span><span class="s">"One hundred or more..."</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
</div>
<p>Again there’s an obvious omission to this switch statement, no break case at the end of every case. Again it’s just one of those things you just don’t need to do in Swift. Switches are actually amazingly powerful, you can pass in nearly any type of constant to case check against making they very adaptable to most circumstances.</p>
<p>In Obj-C I use switches a lot, due to looking up some speed tests once on if statements vs. switch in Obj-C. Switches won hands down, but the thing that annoyed me over the years is the fact they could get big and messy, fast. With not needing to add break in there anymore, that 1 line omission should help clean them up a lot, but that tied into the fact you can perform almost any logic in a case means I think I’ll be using switches even more that I did before in Obj-C.</p>
<h3 id="and-on-and-on">…and on and on…</h3>
<p>I could go on here more and more but I have decided to leave it here. I don’t want to write a detailed how to and why, as Apple has already done that very well in the “The Swift Programming Language” book they released on iBooks. So for more details and abilities of these features in Swift, I encourage you to take at the Apple documentation and references below. They go into so much more detail I could in this post, so go check them out.</p>
<p>Next time I’m going to go into a few of the features I’ve enjoyed and liked being added in Swift that weren’t in Obj-C, and lets see if we can get a bit more focus in these posts.</p>
<p>For any of the sessions from WWDC 2014 find them at: <a herf="https://developer.apple.com/videos/wwdc/2014/" target="_blank">developer.apple.com/videos/wwdc/2014/</a></p>
<p>Also check out the excellent book that Apple released that goes though the Swift language in a lot of detail: <a herf="https://itunes.apple.com/se/book/swift-programming-language/id881256329?l=en&mt=11" target="_blank">Swift Programming Language</a></p>James BarrowThere are some awesome things happening when you look at Swift as a basic language. Swift is not another language built off of C, it’s a brand new language. This means that there are no performance or historical drawbacks to Swift, as it doesn’t rely on an underling langue.A swift look at Swift2014-06-06T00:00:00+02:002017-05-23T00:59:06+02:00https://blog.pigonahill.com/2014/a-swift-look-at-swift<p>On Monday of this week Apple introduced the world to Swift. The new programming language to, <em>eventually</em>, replace Objective-C. There’s a lot of major improvements that Swift brings and I will never be able to go through everything, but I’m going to try to go though the “main” points or just as much, the points that excite me the most. If there’s anything that I’ve missed that you won’t to hear about, post it in the comments and I’ll try to look into it.</p>
<p>Swift has made me very excited. It’s the most excited I’ve got about coding since I first started learning Obj-C. It’s a language that seems to make an awful lot of sense when you look though it. It is a language that in its simplest form can be very easy to learn. However it can be harder for beginners to coding when you get to some of the more advanced topics, a lot sooner than, say, Obj-C would.</p>
<p>With that in mind, the majority of what I’m going to write about will assume that you have some previous coding knowledge, mainly in Obj-C. If you’re a complete beginner, some guides for you will be coming out by someone (I might try some myself) when Swift comes out properly later this year.</p>
<!-- READMORE -->
<p>I said Swift will <em>eventually</em> replace Objective-C. This is <strong>not</strong> something that is going to happen overnight. Far from it. Apple has even said in their WWDC sessions that they are not expecting Swift to be an instant replacement to Obj-C, nor should it be thought of in that way. So you don’t have to worry about re-writing all of your Obj-C apps, they’ll continue to work just as they always have. What Apple has done those is given us as developers the tools to easily use Swift and Obj-C classes and frameworks seamlessly together, with little or no hassle.</p>
<p>So what do you need to know when your writing stuff in Swift? Well first of all Apple has spent a lot of time and effort being able to switch between Obj-C and Swift amazingly easily. So much so that when you’re working in a Swift file and you lookup a class or element from Obj-C if converts the header into Swift so you don’t have to translate it yourself in your head. Obviously it works the opposite way as well and is a great tool to be able to sink yourself into the new language instantly with very little constraints.</p>
<p>This is not to say you can convert you code from Obj-C, this is just to say you can see what your declarations would look like in Swift, allowing you to lookup method calls or variables just like you would if you were looking them up in an Obj-C header file. If you want to look into this particular feature in more detail, check out session 407 - Swift Interoperability in Depth in the WWDC 2014 videos.</p>
<p>As everyone is a 4 day old Swift programmer I am bound to make a few mistakes here and there, so please forgive me if you spot any of these. I will make edits as I go if they are required.</p>
<p>I’ve split this post up into a series of post as after writing about my first point, I’ve already filled up 1 posts worth already. Below I have listed the areas I’ll go into and I will add links to the relevant posts as and when I write them.</p>
<ul>
<li><a href="/2014/swift-the-basics/">The Basics</a></li>
<li><a href="/2014/swift-optionals-and-nil/">Optionals and nil</a></li>
<li>Functions and Closures</li>
<li>Extensions</li>
<li>Enumerations</li>
<li>Generics</li>
</ul>
<p>It’s the little bits that make the difference</p>
<p>For any of the sessions from WWDC 2014 find them at: <a herf="https://developer.apple.com/videos/wwdc/2014/" target="_blank">developer.apple.com/videos/wwdc/2014/</a></p>
<p>Also check out the excellent book that Apple released that goes though the Swift language in a lot of detail: <a herf="https://itunes.apple.com/se/book/swift-programming-language/id881256329?l=en&mt=11" target="_blank">Swift Programming Language</a></p>James BarrowOn Monday of this week Apple introduced the world to Swift. The new programming language to, eventually, replace Objective-C. There’s a lot of major improvements that Swift brings and I will never be able to go through everything, but I’m going to try to go though the “main” points or just as much, the points that excite me the most. If there’s anything that I’ve missed that you won’t to hear about, post it in the comments and I’ll try to look into it.Dog Bins 2.02014-04-17T00:00:00+02:002017-05-23T00:59:06+02:00https://blog.pigonahill.com/2014/dog-bins-2-0<p>Dog Bins 2.0 is here!</p>
<p>So it’s been a long time coming but I finally made some time out of my work hours a couple of weeks ago to blast through the final bits I had to do with updating Dog Bins. This morning it got passed through the review process with Apple and is currently being “processed for the App Store”. So hopefully by the time you read this, you’ll be able to update, if you iOS 7 device hasn’t automatically done it for you that is.</p>
<p>Dog Bins 2.0 is totally rebuilt, pretty much from the ground up. DB was my first app that felt with external sources for getting and saving information. So it had quite a few rookie errors in it. They’re the type of things you don’t realise you did something badly until you’ve gone away, learnt how to actually done it, come back and look at you old code and cringe. It happens a lot in the computing world, mainly due to the fact you are constantly learning something new as people are constantly creating new things and ways of doing these things.</p>
<!-- READMORE -->
<p>The update includes a much needed design cleanse, simpler, more to the point and with the flat elegance that iOS 7 brought us last year. It also seriously improves the speed issues 1.X was plagued by as well as optimisations. All this has come with a little drawback of DB 2.0 being only available for iOS 7+. This is mainly due to the amount of new features available in iOS 7 that just aren’t there in previous versions, along with having to basically make 2 designs for the app to fit into the old look. For the first time since iPhone OS 3.2 we’ve had some of the biggest changes in iOS and sadly that means cutting support for older devices. So to all you iPhone 3GS owners out there, I’m sorry, but maybe it’s time for an upgrade.</p>
<div class="iOS-screen-shot">
<img class="iOS-screen-shot" src="/assets/dog-bins/4_1.png" alt="Dog Bins Map" />
<img class="iOS-screen-shot" src="/assets/dog-bins/4_2.png" alt="Dog Bin Detail" />
<img class="iOS-screen-shot" src="/assets/dog-bins/4_3.png" alt="Dog Bins Settings" />
<img class="iOS-screen-shot" src="/assets/dog-bins/4_4.png" alt="Dog Bins User Profile" />
<img class="iOS-screen-shot" src="/assets/dog-bins/4_5.png" alt="Dog Bins Map Key" />
</div>James BarrowDog Bins 2.0 is here!