<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-2918646278142295649</atom:id><lastBuildDate>Sun, 23 Feb 2025 06:51:45 +0000</lastBuildDate><category>CodeProject</category><category>C#</category><category>Swift</category><category>Attribute</category><category>Control</category><category>CustomAttribute</category><category>Fluent Programming</category><category>WPF</category><category>.NET 2.0</category><category>Fluent</category><category>NUnit</category><category>TDD</category><category>lambda</category><category>&quot;XCode 6&quot;</category><category>C++</category><category>Dependency Property</category><category>DyanmoDB</category><category>Entity Framework</category><category>Entity Framework DbContext SQLServerCE C# NUnit CodeProject SqlCeConnectionFactory databaseDirectory</category><category>OS X</category><category>User Control</category><category>aws</category><category>intelliJ</category><category>js</category><category>&quot;Apple TV&quot;</category><category>&quot;Computer humour&quot; joke UDP</category><category>&quot;Jamerson Quave&quot;</category><category>&quot;aws lambda&quot;</category><category>&quot;vs code&quot;</category><category>7</category><category>ARC</category><category>C# &quot;command line&quot; argument parsing attributes reflection TDD WPF</category><category>C++ C++11 CodeProject</category><category>C++ Unit Testing templates SFINAE TMP sizeof &quot;compilation failure&quot; COM ATL STL CAdapt</category><category>C++11</category><category>Code First</category><category>CodeProject C++ C++11  constexpr range-for</category><category>Computer humour joke</category><category>Data Context</category><category>DataContext</category><category>DbContext</category><category>DbContext SQLServerCE C# NUnit CodeProject SqlCeConnectionFactory databaseDirectory</category><category>DbContext SQLServerCE C# NUnit SqlCeConnectionFactory</category><category>Dilbert</category><category>EF</category><category>ES6</category><category>EventCommand</category><category>EventToCommand</category><category>Fluent Functor</category><category>Graph</category><category>IEnumerable</category><category>IEnumerator</category><category>IPad2</category><category>Intergration Test</category><category>JavaScript</category><category>KeyRemap4MacBook</category><category>MVVM</category><category>Mocha</category><category>NFS C# .NET Server &quot;Open Source&quot;</category><category>Objective-C</category><category>PNG</category><category>Pattern</category><category>Promises</category><category>SKNode</category><category>SizeChanged</category><category>Spirit</category><category>SpriteKit</category><category>Swift 2 defer #swiftlang Apple iOS</category><category>Swift Objective-C iOS</category><category>Swift Objective-C iOS &quot;iOS 8&quot; CodeProject OSX subclass designated convenience initializer super &quot;super class&quot; base &quot;base class&quot;</category><category>TreeView</category><category>Twitter &quot;London Surrey Cycle Classic&quot; &quot;Mark Cavendish&quot; #CycleClasic #testevent Twittating Spectwitter Spectwiting</category><category>UIWindow</category><category>Unit Test</category><category>Unit of work</category><category>Updated</category><category>Visual Studio 2005</category><category>Windows</category><category>Windows 8 iMac 21.5 Boot Camp</category><category>Windows to OS X</category><category>Xcode &quot;Xcode 5&quot; &quot;Xcode 6&quot; &quot;Sprite Kit&quot; iOS Swift Game generated landscape portait sks GameScene.sks SKScene size CodeProject &quot;iOS 8&quot;</category><category>actionscript</category><category>adobe</category><category>air</category><category>app icon</category><category>bitmap</category><category>capture</category><category>cover</category><category>debugging</category><category>design-time</category><category>encapsulation</category><category>enumeration</category><category>enumerator</category><category>event</category><category>event to command</category><category>event-to-command pattern</category><category>flex</category><category>flex sdk</category><category>git OneDrive iCloud remote clone backup GitHub</category><category>iOS</category><category>iOS Swift &quot;Sprite Kit&quot; &quot;Hungry Bunny&quot; Keychain NSCoding</category><category>icon</category><category>interoperability</category><category>ios 7</category><category>keyboard</category><category>lambda capture</category><category>lambda-local</category><category>lvalue</category><category>lvalue reference</category><category>node</category><category>node.js</category><category>persist</category><category>reference</category><category>remapping</category><category>resizable</category><category>sdk iOS</category><category>start-up</category><category>swift &quot;optional chaining&quot; Dictionary</category><category>swift enum protocol</category><category>swift let var value reference type container</category><category>tactile</category><category>tvOS</category><category>tvOS iOS swift &quot;Apple TV&quot; OAuth OAuth2 iCloud KV Key-Value Photone OneDrive</category><category>unowned</category><category>weak</category><title>C#, C++, Windows &amp;amp; other ramblings</title><description></description><link>http://petebarber.blogspot.com/</link><managingEditor>noreply@blogger.com (Pete Barber)</managingEditor><generator>Blogger</generator><openSearch:totalResults>48</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-149937250221971746</guid><pubDate>Wed, 03 Jan 2018 21:13:00 +0000</pubDate><atom:updated>2018-01-03T13:14:24.406-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">iOS</category><category domain="http://www.blogger.com/atom/ns#">resizable</category><category domain="http://www.blogger.com/atom/ns#">Swift</category><category domain="http://www.blogger.com/atom/ns#">UIWindow</category><title>Resizable UIWindow on iOS</title><description>I came across a Todo item in my inbox (I often email myself things todo) whilst tidying it up following Christmas, entitled &quot;Make a resizable UIWindow&quot;. I can remember sending this to myself but not what I was reading or watching that prompted me to do it other than I thought it must be possible to play around with the default style of main UIWindow that an iOS App displays most of its content in.&lt;br /&gt;
&lt;br /&gt;
Normally, when running an iOS App the main UIWindow is black and takes up the entire devices screen. It is possible to have more than one UIWindow but the usual case for this is when content is being displayed on a secondary display, e.g. AirPlay. On MacOS it is common to have many instances of UIWindow with these being displayed on the Mac&#39;s desktop. It&#39;s normal to think of an iOS device as having a desktop as when using an App (or multiple if using Multi-tasking) then the desktop/background is completely obscured.&lt;br /&gt;
&lt;br /&gt;
None the less, UIWindow is a subclass of UIView. This inherently means it has a Frame, i.e. a position in it&#39;s parent space plus a width &amp;amp; height. I wondered if these could be altered - they can!&lt;br /&gt;
&lt;br /&gt;
I wrote a very simple single page App which contains a UIView that by default has the screen dimensions and is coloured blue, on top this, roughly in the centre is a red rectangle (a child UIView).&lt;br /&gt;
&lt;br /&gt;
There are a set of 3 gesture recognisers (pinch, rotate &amp;amp; pan) that can be applied to either the Red Rect, the View or the underlying Window. Which one they&#39;re applied too is set using the Picker control hosted by the UIView.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsGD0XGUe25DGRakPHHG7HOLOPW8K5EGwstJADUVpUBAX13z-Il8hqoQxipLRnOR20epg8UZm02vvGu9PhmaN6PhnQVazLAcQrjV3EyNtG-6DYw5lNdLzTrRUFYKIxRyO1Zi-qBQL-k5o/s1600/Screen+Shot+2018-01-03+at+20.27.41.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1334&quot; data-original-width=&quot;750&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsGD0XGUe25DGRakPHHG7HOLOPW8K5EGwstJADUVpUBAX13z-Il8hqoQxipLRnOR20epg8UZm02vvGu9PhmaN6PhnQVazLAcQrjV3EyNtG-6DYw5lNdLzTrRUFYKIxRyO1Zi-qBQL-k5o/s320/Screen+Shot+2018-01-03+at+20.27.41.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The next picture shows all 3 components resized, rotated and moved around. Note how the Status Bar has almost disappeared due the black text colour now being on a black background (though the green battery indicator is still present) as no UIWindow is there.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWjYB_9TUGTSUA0uTkuY93CA4VyMGm8aVlV3UKTr4LCRbbmNGwjBjkMrV1DUPLUlbE1KzceN1DVRFzinZzkuWH7dF0bQR2dC7gpSPsw6u9H7sTNZi1vHanILPnxJa8cZDeF2W9Xtlj54A/s1600/Screen+Shot+2018-01-03+at+20.28.57.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1334&quot; data-original-width=&quot;750&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWjYB_9TUGTSUA0uTkuY93CA4VyMGm8aVlV3UKTr4LCRbbmNGwjBjkMrV1DUPLUlbE1KzceN1DVRFzinZzkuWH7dF0bQR2dC7gpSPsw6u9H7sTNZi1vHanILPnxJa8cZDeF2W9Xtlj54A/s320/Screen+Shot+2018-01-03+at+20.28.57.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
By dragging the Window over the Status Bar it&#39;s revealed again.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhr8PEwxGIQrd1wwPPK6To6YLk_E1PjJ0uqi1yl3TegReaN3ERxrU917LQE-Ic4pcvdEI_KpgIkLuHA6tRGFeiaP6YR7JGo9W95vCRZaCbAc0745ogPV03RqEJ7f5sHvpYfUEPFPGXPrd8/s1600/Screen+Shot+2018-01-03+at+20.29.08.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1334&quot; data-original-width=&quot;750&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhr8PEwxGIQrd1wwPPK6To6YLk_E1PjJ0uqi1yl3TegReaN3ERxrU917LQE-Ic4pcvdEI_KpgIkLuHA6tRGFeiaP6YR7JGo9W95vCRZaCbAc0745ogPV03RqEJ7f5sHvpYfUEPFPGXPrd8/s320/Screen+Shot+2018-01-03+at+20.29.08.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, the Red Rectangle has been completely detached from the it&#39;s parent UIView. At this point touch events do not seem to be sent to it any more.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVnXAVYpHkiXNFGPTfMERsxXTSTjw-qJs96B4SK5grFBxwY0WI9I8lrC7wEaBeSwDi6ZwZ_r-4sYr2Z-C4-t6i-ttc3zgswfX0fHYEZJkE30s2t8e6DFLlB1WmPzmDOsweXm3mrOkh6y8/s1600/Screen+Shot+2018-01-03+at+20.29.38.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1334&quot; data-original-width=&quot;750&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVnXAVYpHkiXNFGPTfMERsxXTSTjw-qJs96B4SK5grFBxwY0WI9I8lrC7wEaBeSwDi6ZwZ_r-4sYr2Z-C4-t6i-ttc3zgswfX0fHYEZJkE30s2t8e6DFLlB1WmPzmDOsweXm3mrOkh6y8/s320/Screen+Shot+2018-01-03+at+20.29.38.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Taking this further my moving (via the pan gesture) the View from off the Window this too stops receiving input. At this one nothing can be done with the App.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCG2jZyYrkId-dV6-5v-uEEMIYNnZmpDNzgdHvM62CvvR8744chLR8jN4ZGRNl-xA0TDW5KU0S6AvqZ5h8bSKXc6E6a-BH5Rdkaot7R5H-gPEr-xpKigoU1TW8mFpbc36oLntkeqUw68E/s1600/Screen+Shot+2018-01-03+at+20.30.05.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1334&quot; data-original-width=&quot;750&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCG2jZyYrkId-dV6-5v-uEEMIYNnZmpDNzgdHvM62CvvR8744chLR8jN4ZGRNl-xA0TDW5KU0S6AvqZ5h8bSKXc6E6a-BH5Rdkaot7R5H-gPEr-xpKigoU1TW8mFpbc36oLntkeqUw68E/s320/Screen+Shot+2018-01-03+at+20.30.05.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
That&#39;s not completely true. The App can be rotated. This has some bizarre results probably not helped by there being no constraints.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieHH9jb04UT_IOOAnV0NvS0ZkCAVOjCMKbqU4TjmmgghOOz9TkTk0LeZqX_xMA1UVI3PbzwgDKlG_pPlm1zDkBpd2OLodJ8EBWfOJe24hPa2-YS2dCw9vizYgqdM7EZLUQPLiDwzwD1fg/s1600/Screen+Shot+2018-01-03+at+20.39.24.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;750&quot; data-original-width=&quot;1334&quot; height=&quot;179&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieHH9jb04UT_IOOAnV0NvS0ZkCAVOjCMKbqU4TjmmgghOOz9TkTk0LeZqX_xMA1UVI3PbzwgDKlG_pPlm1zDkBpd2OLodJ8EBWfOJe24hPa2-YS2dCw9vizYgqdM7EZLUQPLiDwzwD1fg/s320/Screen+Shot+2018-01-03+at+20.39.24.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
The code for the App is available on &lt;a href=&quot;https://github.com/petebarber/Resizable&quot;&gt;GitHub&lt;/a&gt; though the code for ViewController is below&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/petebarber/adc769463c54cdba6d6463eaa1eccef4.js&quot;&gt;&lt;/script&gt;

&lt;br /&gt;
The only slightly strange things happening here are:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The re-assignment of the Gesture Recognizers to the selected UIView&lt;/li&gt;
&lt;li&gt;For each of the methods invoked by the Gesture Recognizers how they &#39;reset&#39; the main parameter modified by the gesture after each invocation.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
If you run the project, as the there are multiple Gesture Recognizers but multiple simultaneous Gesture Recognition support hasn&#39;t been enabled then in particular pinch &amp;amp; rotate must be discreet gestures. Also, rotation can be fiddly to get working especially if UI element has been pinched to a small size.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I&#39;m not really aware of any practical application of this. It might be interesting to try have 2 UIWindows displayed at the same time though I&#39;m not sure this is possible and could easily be achieved with 2 UIViews. Perhaps to have 2 sub-apps each in its own UIWindow which can be swapped between.&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2018/01/resizable-uiwindow-on-ios.html</link><author>noreply@blogger.com (Pete Barber)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsGD0XGUe25DGRakPHHG7HOLOPW8K5EGwstJADUVpUBAX13z-Il8hqoQxipLRnOR20epg8UZm02vvGu9PhmaN6PhnQVazLAcQrjV3EyNtG-6DYw5lNdLzTrRUFYKIxRyO1Zi-qBQL-k5o/s72-c/Screen+Shot+2018-01-03+at+20.27.41.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-7558337230794925525</guid><pubDate>Sat, 02 Sep 2017 09:24:00 +0000</pubDate><atom:updated>2017-09-02T07:26:05.628-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">aws</category><category domain="http://www.blogger.com/atom/ns#">DyanmoDB</category><category domain="http://www.blogger.com/atom/ns#">ES6</category><category domain="http://www.blogger.com/atom/ns#">JavaScript</category><category domain="http://www.blogger.com/atom/ns#">js</category><category domain="http://www.blogger.com/atom/ns#">lambda</category><category domain="http://www.blogger.com/atom/ns#">Mocha</category><category domain="http://www.blogger.com/atom/ns#">Promises</category><title>Unit Testing with Mocha, a local instance of dynamoDB &amp; Promises</title><description>I&#39;m writing the backend for my current iOS App in Javascript using node.js, AWS Lambda along with DynamoDB.&lt;br /&gt;
&lt;br /&gt;
My AWS Lambda code is mostly AWS Lambda agnostic except for the initial handler methods, this makes them fairly testable outside of AWS. However, they depend on the DynamoDB. It&#39;s quite easy to write Unit Tests that run against a live version of DynamoDB but I wanted to run against a local instance, ideally an in-memory instance so that it would be quick (not that running against a real instance is that slow) and so that I could have a clean dB each time.&lt;br /&gt;
&lt;br /&gt;
NOTES&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;As these tests are running against a dB it might be more accurate to call them Integration Tests implemented using a Unit Testing framework but I&#39;ll refer to them as Unit Tests (UTs).&lt;/li&gt;
&lt;li&gt;This is running on MacOS&lt;/li&gt;
&lt;li&gt;I don&#39;t Unit Test the actual AWS Lambda function. Instead I export the underlying functions &amp;amp; objects that the AWS Lambda uses and Unit Test these.&lt;/li&gt;
&lt;li&gt;I&#39;m a JavaScript, Node, AWS n00b so this if you spot something wrong or that&#39;d bad please comment.&lt;/li&gt;
&lt;li&gt;I don&#39;t like the callback pyramid so I use Promises where I can. I would use async and await but the latest version of Node that AWS Lambda supports doesn&#39;t support them :-(&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
In order to run this locally you&#39;ll need:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://nodejs.org/en/&quot;&gt;Node&lt;/a&gt; installed locally&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://mochajs.org/#more-information&quot;&gt;Mocha&lt;/a&gt; installed&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/intro-dynamodb-local.html&quot;&gt;DynamoDB installed locally&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
My goal was to have a clean dB for each individual Unit Test. The simplest way to achieve this I thought was to create an in-memory of dynamoDB and destroy it after each Unit Test.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/petebarber/ea4f0e41cbd7d764993db842e1afd4e5.js&quot;&gt;&lt;/script&gt;&lt;br /&gt;
This uses the child_process npm package to create an instance before each test, store the handle to the process in local-ish variable and following the tasks just kill it. The important points here are that the &#39;-inMemory&#39; option is used meaning that when the dB instance is killed and another re-started everything is effectively wiped without having to do everything.&lt;/div&gt;
&lt;br /&gt;
The problem I had with this approach is that in addition to creating the dB each time I also needed to create a table. Whilst the documentation for local dynamoDB says that one of the &lt;a href=&quot;http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/dynamolocal-vs-dynamoservice.html&quot;&gt;differences&lt;/a&gt; between the AWS hosted &amp;amp; the local versions is that CreateTable completes immediately it seems that the function does indeed complete immediately the table isn&#39;t immediately available. This meant the UT using the table often failed with:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;1) awsLambdaToTest The querying an empty db for a user Returns{}:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;ResourceNotFoundException: Cannot do operations on a non-existent table&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I&#39;m going to jump ahead and show the completed Unit Test file and explain the various things I had to do in order to get it working. This shows the tests.&lt;br /&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/petebarber/1bef78b9a6e98c9132a248a422922948.js&quot;&gt;&lt;/script&gt;

&lt;br /&gt;
&lt;h3&gt;
before()/after() - creating/destroying dynamoDB&lt;/h3&gt;
&lt;br /&gt;
Rather than attempting to create &amp;amp; destroy the dB for each Unit Test I settled with creating it once per &amp;nbsp;Unit Test file. This is handled in the begin() &amp;amp; after() functions. Here the local instance of dynamoDB is spawned using the child_process package and reference to the process retained. This is then used to kill it afterwards. The important point to note here is the use of the sleep package &amp;amp; function.&lt;br /&gt;
&lt;br /&gt;
I found when I had multiple test files, each with their own begin() &amp;amp; after() functions that did the same as these, even though kill had purported to have killed the processed (I checked the killed flag) it seemed the process hadn&#39;t died immediately. This meant that the before() function in the next set of tests would succesfully connect to the dying instance of dynamoDB. Then later when any operation was performed it would just hang until Mocha timed-out the Unit Test/before handler. I tried various ways to detect that the process was really dead but none worked so settled for a sleep.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
beforeEach()/afterEach() - creating/destroying the table&lt;/h3&gt;
&lt;div&gt;
Where possible I use Promises. Mocha handles &lt;a href=&quot;https://mochajs.org/#asynchronous-code&quot;&gt;promises quite simply&lt;/a&gt; for both hooks (the before*/after* functions) and Unit Tests. The key is to make sure to return the final promise (or pass in the done parameter &amp;amp; call it - though I don&#39;t use this mechanism).&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Looking at the beforeEach() function createTable() is called which returns a promise (from the AWS.Request type that aws-sdk.DynamoDB.createTable() returns. This promise is then chained too by the synchronous waitFor method. This polls the dB for state of the table. The returned promise will not complete until the table has been created and waitFor has completed.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I am not convinced that waitFor is needed. According to the &lt;a href=&quot;http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html&quot;&gt;AWS vs Local DynamoDB guide&lt;/a&gt;&amp;nbsp;for local instances tables are created immediately. I added this check as occasionally I was getting resources errors like the one earlier. However, I think the cause for that was because I forgot the &lt;i&gt;return&lt;/i&gt;&amp;nbsp;statement before the call to createTable() meaning no Promise was returned to Mocha so it thought the beforeEach() function had completed. I have removed this since in my real Unit Tests and they all seem to work.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
Unit Tests&lt;/h3&gt;
&lt;div&gt;
That&#39;s really it. The hard part wasn&#39;t writing the UTs but getting a local instance of DynamoDB running with the table that the functions to test used in the correct state. Again, due to the functions being tested usually returning promises themselves it is necessary to return Promise. The assertion(s) are made synchronously in a &lt;i&gt;then&lt;/i&gt;&amp;nbsp;continuation chained to Promise returned from the function being tested and Promise from the whole chain returned.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
If an assertion returns false then even though it&#39;s within a continuation Mocha detects this and the test fails. If the function under test throws then Mocha also catches this and the test fails.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
And finally&lt;/h3&gt;
&lt;h4&gt;
timeout&lt;/h4&gt;
&lt;div&gt;
There&#39;s also the &lt;i&gt;this.timeout(5000);&lt;/i&gt;&amp;nbsp;at the top of the tests for this file. By default Mocha has 2 second timeout for all tests and hooks. By having the 1 second sleep for starting the dB it&#39;s possible for the hook to timeout then causing everything else to fail. The 5 second timeout protects against this.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;
localhost&lt;/h4&gt;
&lt;div&gt;
When creating the local instance it uses the default port of 8000 and is accessible via the localhost hostname alias. This is used to access the dB when creating the reference to it in before(). A local aws.config is also constructed in each of the functions that access the dB in the actual code under test below.&lt;/div&gt;
&lt;br /&gt;
&lt;h4&gt;
The actual code being tested&lt;/h4&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/petebarber/fa0b96226bf51f0602f1348c6cb06960.js&quot;&gt;&lt;/script&gt;</description><link>http://petebarber.blogspot.com/2017/09/unit-testing-with-mocha-local-instance.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-5743982245980367426</guid><pubDate>Tue, 03 Jan 2017 18:28:00 +0000</pubDate><atom:updated>2017-01-03T13:10:55.111-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">&quot;aws lambda&quot;</category><category domain="http://www.blogger.com/atom/ns#">&quot;vs code&quot;</category><category domain="http://www.blogger.com/atom/ns#">aws</category><category domain="http://www.blogger.com/atom/ns#">debugging</category><category domain="http://www.blogger.com/atom/ns#">DyanmoDB</category><category domain="http://www.blogger.com/atom/ns#">js</category><category domain="http://www.blogger.com/atom/ns#">lambda</category><category domain="http://www.blogger.com/atom/ns#">lambda-local</category><category domain="http://www.blogger.com/atom/ns#">node</category><category domain="http://www.blogger.com/atom/ns#">node.js</category><title>Debugging AWS Lambda functions locally using VS Code and lambda-local</title><description>I&#39;ve just started using &lt;a href=&quot;https://aws.amazon.com/lambda/&quot;&gt;AWS Lambda&lt;/a&gt;&amp;nbsp;with&amp;nbsp;&lt;a href=&quot;https://nodejs.org/&quot;&gt;node.js&lt;/a&gt;. I was able to develop these locally using the &lt;a href=&quot;https://www.npmjs.com/package/lambda-local&quot;&gt;lambda-local&lt;/a&gt; npm package, e.g. with node.js installed (via brew) and lambda-local installed (using npm) then the following &quot;hello, world&quot; example is run as follows:&lt;br /&gt;
&lt;br /&gt;
hellolambda.js&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&#39;use strict&#39;;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal; min-height: 14px;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;console.log(&#39;Loading function&#39;);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal; min-height: 14px;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;exports.handler = (event, context, callback) =&amp;gt; {&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; console.log(&#39;Received event:&#39;, JSON.stringify(event, null, 2));&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; console.log(&#39;value1 =&#39;, event.key1);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; console.log(&#39;value2 =&#39;, event.key2);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; console.log(&#39;value3 =&#39;, event.key3);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; callback(null, event.key1);&amp;nbsp; // Echo back the first key value&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; //callback(&#39;Something went wrong&#39;);&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;};&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
defaultevent.js&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;module.exports =&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&quot;key1&quot;: &quot;hello&quot;,&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&quot;key2&quot;: &quot;lambda&quot;,&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&quot;key3&quot;: &quot;node&quot;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;};&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
/usr/local/bin/lambda-local -l hellolambda.js -e default event.js&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;Loading function&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: Logs&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: ------&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: START RequestId: d683128b-ac14-93c3-b2c1-5541f3bb3fda&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;Received event: {&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &quot;key1&quot;: &quot;hello&quot;,&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &quot;key2&quot;: &quot;lambda&quot;,&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &quot;key3&quot;: &quot;node&quot;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;value1 = hello&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;value2 = lambda&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;value3 = node&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: END&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: Message&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: ------&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: hello&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: -----&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color: rgba(0, 0, 0, 0.901961); color: #28fe14; font-family: &#39;Andale Mono&#39;; font-size: 12px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #34bc26; font-variant-ligatures: no-common-ligatures;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;font-variant-ligatures: no-common-ligatures;&quot;&gt;: lambda-local successfully complete.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Rather than use bash and vi (I&#39;m running on MacOS) I wanted to use some sort of IDE. &lt;a href=&quot;https://code.visualstudio.com/&quot;&gt;VS Code&lt;/a&gt;&amp;nbsp;seemed ideal as it&#39;s free and it also has builtin node.js debugging. Using it for editing is very simple, just open the folder containing the source. In this case ~/tmp/hellolambda&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_QM5nl4DlJWRk-gKqaE0vgS9zSeJ93SJWTdrAqEBRMMOQGYn5Ork4FZyUsdh5gJ3JhNZuQb21x7e4KM1ekYIzBVhIhes0pXx_yN7wrYORX5HdUhssHaRf4MuJJeQVA_Ny6RN7IZr9ayo/s1600/Untitled.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;476&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_QM5nl4DlJWRk-gKqaE0vgS9zSeJ93SJWTdrAqEBRMMOQGYn5Ork4FZyUsdh5gJ3JhNZuQb21x7e4KM1ekYIzBVhIhes0pXx_yN7wrYORX5HdUhssHaRf4MuJJeQVA_Ny6RN7IZr9ayo/s640/Untitled.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
However, switching to the debugging section and creating the default launch configuration where VS Code will launch node with the specify file as the program doesn&#39;t do much good.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJSXNjEKkzGwZPOz3Xe8iKY9DVppUrYRbr5_ZsxJcNqF-onTjjrOv_mwTytTXKZMKtWS59coWxBoYsc9xYmxV-Qj_LSydRVKCwlxL9kS_NcGobUz_SMR6j7gf7wTy91ZPszV1Wupm9rls/s1600/Untitled+2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;428&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJSXNjEKkzGwZPOz3Xe8iKY9DVppUrYRbr5_ZsxJcNqF-onTjjrOv_mwTytTXKZMKtWS59coWxBoYsc9xYmxV-Qj_LSydRVKCwlxL9kS_NcGobUz_SMR6j7gf7wTy91ZPszV1Wupm9rls/s640/Untitled+2.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
This is because when running a lambda locally using local-lambda the program that node needs to run is the local lambda environment that local-lambda creates and for it to launch the lambda function.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
This can be simply configured by specifying the local-lambda script as the program (it&#39;s a node script) and then passing the lambda script and the event data as arguments using the args key (which isn&#39;t included when using the VS Code option to add a configuration). The original example above can be launched using the following configuration.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid6jP4V507f2TslNWHYF4P_xjDLXq7lEnxuoJaZDaq_Z7u8E-JAer8gm9_WHm6W4lOCl1RnSJWxjQbZv9p1o-ZPW3eA4T6X9XDRRy_g65p0aiT09gR8Jcqf9ryQ_dhE8iUQwbVztJlsso/s1600/Untitled.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;428&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid6jP4V507f2TslNWHYF4P_xjDLXq7lEnxuoJaZDaq_Z7u8E-JAer8gm9_WHm6W4lOCl1RnSJWxjQbZv9p1o-ZPW3eA4T6X9XDRRy_g65p0aiT09gR8Jcqf9ryQ_dhE8iUQwbVztJlsso/s640/Untitled.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
In the output window at the bottom the results of executing the lambda are shown. Breakpoints can be set and hit.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
It&#39;s important that each command line argument, i.e. the option and the value are specified separately. Even though &#39;-l&#39; and its value are a pair they are separate command line arguments (2 in total) where &quot;-l ${workspaceRoot}/hellolambda.js&quot; is a single argument.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;i&gt;NOTE: The lambdas I&#39;m writing are also using the &lt;a href=&quot;https://aws.amazon.com/dynamodb/&quot;&gt;AWS DynamoDB&lt;/a&gt;. Using a &lt;a href=&quot;http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html&quot;&gt;local instance&lt;/a&gt; of DynamoDB along with installing the AWS SDK via npm I&#39;ve been able to successfully invoke local lambdas that have used the local instance of the DB.&lt;/i&gt;&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2017/01/debugging-aws-lambda-functions-locally.html</link><author>noreply@blogger.com (Pete Barber)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_QM5nl4DlJWRk-gKqaE0vgS9zSeJ93SJWTdrAqEBRMMOQGYn5Ork4FZyUsdh5gJ3JhNZuQb21x7e4KM1ekYIzBVhIhes0pXx_yN7wrYORX5HdUhssHaRf4MuJJeQVA_Ny6RN7IZr9ayo/s72-c/Untitled.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-3994362208542752114</guid><pubDate>Mon, 01 Feb 2016 21:05:00 +0000</pubDate><atom:updated>2016-02-02T01:46:49.465-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Swift 2 defer #swiftlang Apple iOS</category><title>Swift&#39;s defer statement is funkier than I thought</title><description>Swift 2.0 introduced the defer keyword. I&#39;ve used this a little but only in a simple way, basically when I wanted to make sure some code would be executed regardless of where control left the function, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; resetAfterError() throws&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;font-family: menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2;&quot;&gt;defer&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #703daa;&quot;&gt;selectedIndex&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt; = &lt;/span&gt;&lt;span style=&quot;color: #272ad8; font-variant-ligatures: no-common-ligatures;&quot;&gt;0&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #4f8187; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;isError&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt; = &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;false&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; if /* condition */&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; // Do stuff&lt;br /&gt;
&amp;nbsp; &amp;nbsp; return&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; if /* other condition */&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; // Do other stuff&lt;br /&gt;
&amp;nbsp; &amp;nbsp; return&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; // Do default stuff&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;br /&gt;
In my usage to date there has always been some code that should always be executed prior to the function&#39;s exit and additionally only one piece of code. Therefore I&#39;ve always put the defer statement at the top of the function so when reading it&#39;s pretty obvious.&lt;br /&gt;
&lt;br /&gt;
I was aware that if there were multiple defer statements then they&#39;d be executed in reverse order but what I&#39;d not given any thought to before was what happens if the defer statement isn&#39;t reached. In fact I&#39;d just assumed it was more of a declaration that this code should always be executed on function exit and as I put mine right at the start of the function this was effectively the case.&lt;br /&gt;
&lt;br /&gt;
However, for some functions (probably most) you don&#39;t want this. You only want the deferred code executing if some else as happened. This is shown simply in &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508&quot;&gt;The Swift Programming Language&lt;/a&gt; book example:&lt;br /&gt;
&lt;br /&gt;
&lt;ol class=&quot;code-lines&quot; style=&quot;border: 0px; counter-reset: li 0; font-family: Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.6em; list-style: none; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;&lt;span class=&quot;kt&quot; style=&quot;border: 0px; color: #aa3391; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;processFile&lt;/span&gt;(&lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;filename&lt;/span&gt;: &lt;span class=&quot;n&quot; style=&quot;border: 0px; color: #5c2699; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;String&lt;/span&gt;) &lt;span class=&quot;kt&quot; style=&quot;border: 0px; color: #aa3391; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;throws&lt;/span&gt; {&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;    &lt;span class=&quot;kt&quot; style=&quot;border: 0px; color: #aa3391; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;exists&lt;/span&gt;(&lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;filename&lt;/span&gt;) {&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;        &lt;span class=&quot;kt&quot; style=&quot;border: 0px; color: #aa3391; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;file&lt;/span&gt; = &lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;open&lt;/span&gt;(&lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;filename&lt;/span&gt;)&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;        &lt;span class=&quot;kt&quot; style=&quot;border: 0px; color: #aa3391; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;defer&lt;/span&gt; {&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;            &lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;close&lt;/span&gt;(&lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;file&lt;/span&gt;)&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;        }&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;        &lt;span class=&quot;kt&quot; style=&quot;border: 0px; color: #aa3391; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;kt&quot; style=&quot;border: 0px; color: #aa3391; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;line&lt;/span&gt; = &lt;span class=&quot;kt&quot; style=&quot;border: 0px; color: #aa3391; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;file&lt;/span&gt;.&lt;span class=&quot;vc&quot; style=&quot;border: 0px; color: #3f6e74; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;readline&lt;/span&gt;() {&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;            &lt;span class=&quot;c&quot; style=&quot;border: 0px; color: #008312; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;// Work with the file.&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -13px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;        }&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -17px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;        &lt;span class=&quot;c&quot; style=&quot;border: 0px; color: #008312; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;// close(file) is called here, at the end of the scope.&lt;/span&gt;&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -17px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;    }&lt;/code&gt;&lt;/li&gt;
&lt;li style=&quot;border-left-color: transparent; border-left-style: solid; border-width: 0px 0px 0px 18px; list-style-type: none; margin: 0px; outline: 0px; padding: 0px 10px 0px 0px; text-indent: -17px; vertical-align: baseline; white-space: pre-wrap;&quot;&gt;&lt;code class=&quot;code-voice&quot; style=&quot;border: 0px; font-family: Menlo, monospace; font-size: 0.85em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-wrap: break-word;&quot;&gt;}&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
In this if the file is not opened then the deferred code should not be executed. Another very important usage is:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
extension&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #703daa; font-variant-ligatures: no-common-ligatures;&quot;&gt;NSLock&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; synchronized&amp;lt;T&amp;gt;(&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;@noescape&lt;/span&gt; closure: () &lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;throws&lt;/span&gt; -&amp;gt; T) &lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;rethrows&lt;/span&gt; -&amp;gt; &lt;span style=&quot;color: #703daa; font-variant-ligatures: no-common-ligatures;&quot;&gt;T&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #703daa;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;{&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;&amp;nbsp;   &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;self&lt;/span&gt;.&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;lock&lt;/span&gt;()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #bb2ca2;&quot;&gt;defer&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; &amp;nbsp; {&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;self&lt;/span&gt;.&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;unlock&lt;/span&gt;()&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;&amp;nbsp;   &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;try&lt;/span&gt; closure()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;&amp;nbsp; &lt;/span&gt;}&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;br /&gt;
If the lock is never obtained then it should never be unlocked. In this case this shouldn&#39;t have as the self.lock() will not return until it obtains the lock but if that line were replaced with self.&lt;br /&gt;
&lt;br /&gt;
This is how defer works. If the defer statement is never reached and/or encountered then the deferred code block will never be executed. This includes branches (if-statements etc.). The following example:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;enum&lt;/span&gt; WhenToReturn&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; case&lt;/span&gt; After0&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; case&lt;/span&gt; After1&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; case&lt;/span&gt; After2&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; deferTest(whenToReturn: &lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;WhenToReturn&lt;/span&gt;, shouldBranch: &lt;span style=&quot;color: #703daa; font-variant-ligatures: no-common-ligatures;&quot;&gt;Bool&lt;/span&gt;)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #3d1d81;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #d12f1b;&quot;&gt;&quot;Defer Test - whenToReturn:&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;\&lt;/span&gt;&lt;span style=&quot;color: #d12f1b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;whenToReturn&lt;/span&gt;&lt;span style=&quot;color: #d12f1b;&quot;&gt;), shouldBranch:&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;\&lt;/span&gt;&lt;span style=&quot;color: #d12f1b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;shouldBranch&lt;/span&gt;&lt;span style=&quot;color: #d12f1b;&quot;&gt;)&quot;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; defer&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #3d1d81;&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: #d12f1b;&quot;&gt;&quot;defer 0&quot;&lt;/span&gt;)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; }&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; print&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #d12f1b; font-variant-ligatures: no-common-ligatures;&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; if&lt;/span&gt; whenToReturn &lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;WhenToReturn&lt;/span&gt;.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;After0&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; &amp;nbsp; return&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; }&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; defer&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; print&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;(&lt;/span&gt;&quot;defer 1&quot;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; }&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; print&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #d12f1b; font-variant-ligatures: no-common-ligatures;&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; if&lt;/span&gt; whenToReturn &lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;WhenToReturn&lt;/span&gt;.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;After1&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; &amp;nbsp; return&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; }&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; if&lt;/span&gt; shouldBranch&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; &amp;nbsp; defer&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; &amp;nbsp; {&lt;/div&gt;
&lt;div style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;&quot;shouldBranch&quot;&lt;span style=&quot;color: black;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; }&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; defer&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;&amp;nbsp; &amp;nbsp; print&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;(&lt;/span&gt;&quot;defer 2&quot;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; }&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; print&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #d12f1b; font-variant-ligatures: no-common-ligatures;&quot;&gt;&quot;3&quot;&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;deferTest&lt;/span&gt;(&lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;WhenToReturn&lt;/span&gt;.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;After0&lt;/span&gt;, shouldBranch: &lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;false&lt;/span&gt;)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;deferTest&lt;/span&gt;(&lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;WhenToReturn&lt;/span&gt;.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;After1&lt;/span&gt;, shouldBranch: &lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;true&lt;/span&gt;)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;deferTest&lt;/span&gt;(&lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;WhenToReturn&lt;/span&gt;.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;After2&lt;/span&gt;, shouldBranch: &lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;false&lt;/span&gt;)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;deferTest&lt;/span&gt;(&lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;WhenToReturn&lt;/span&gt;.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;After2&lt;/span&gt;, shouldBranch: &lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;true&lt;/span&gt;)&lt;/div&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;Defer Test - whenToReturn:After0, shouldBranch:false&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;Defer Test - whenToReturn:After1, shouldBranch:true&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;1&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 1&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;Defer Test - whenToReturn:After2, shouldBranch:false&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;1&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;3&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 2&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 1&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;Defer Test - whenToReturn:After2, shouldBranch:true&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;1&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;shouldBranch&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;3&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 2&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 1&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;defer 0&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
&lt;div style=&quot;font-family: menlo; font-size: 11px;&quot;&gt;
&lt;b&gt;Program ended with exit code: 0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: menlo; font-size: 11px;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: menlo; font-size: 11px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: -webkit-standard; font-size: small;&quot;&gt;This shows that returning before and/or not branching results in defer statements not being encountered hence the deferred code is not executed. &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: -webkit-standard; font-size: small;&quot;&gt;This is no different to say a finally-block in C#. The reason for my initial confusion is that there is no additional content for a defer block as there is for a&amp;nbsp;&lt;/span&gt;finally block, i.e. the presence of the try, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: x-small;&quot;&gt;try&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: x-small;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: x-small;&quot;&gt;&amp;nbsp; // Try some stuff&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: x-small;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: x-small;&quot;&gt;finally&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: x-small;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: x-small;&quot;&gt;&amp;nbsp; // Always do something having tried something regardless of whether it worked or not&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: x-small;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Whereas the only and actual context of the defer block is it&#39;s position.&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2016/02/swifts-defer-statement-is-funkier-than.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-6238448245371867396</guid><pubDate>Tue, 26 Jan 2016 14:12:00 +0000</pubDate><atom:updated>2016-01-26T06:12:39.816-08:00</atom:updated><title>The Perils of debugging with return statements in languages without semi-colon statement terminators, i.e. Swift</title><description>This is a pretty obvious post but perhaps writing it will stop me falling prey to this issue.&lt;br /&gt;
&lt;br /&gt;
When I&#39;m debugging and I know that some code executed in a function is not to blame but is noisy in terms of what it causes to happen etc. I&#39;ll often just prevent it from being executed in order to simplify the system, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; foo()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;/*&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;f()&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;g()&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;h()&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;// Do lots of other things...&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;*/&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Sometimes I like to be quicker to I just put in an early return statement, i.e.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; foo()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;return&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;f&lt;/span&gt;()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;g()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;h()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;// Do lots of other things...&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I must also go temporarily warning blind and ignore the following:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGFWLvn3st3t6S50eh53QSRCVzQSKJTGBGKOgR_h2uTndGFtcB1XxzHACpwrf1nbF-IOsD5D870orzaHjny4MWhWNafmjkemQFrwZxx0mtCkkyfneQ2zDuOb8FfiCiRH1SdT5sgQw-pbs/s1600/Untitled.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGFWLvn3st3t6S50eh53QSRCVzQSKJTGBGKOgR_h2uTndGFtcB1XxzHACpwrf1nbF-IOsD5D870orzaHjny4MWhWNafmjkemQFrwZxx0mtCkkyfneQ2zDuOb8FfiCiRH1SdT5sgQw-pbs/s1600/Untitled.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span id=&quot;goog_1697705513&quot;&gt;&lt;/span&gt;&lt;span id=&quot;goog_1697705514&quot;&gt;&lt;/span&gt;&lt;br /&gt;
The effect of this is that rather than prevent everything after the return statement from executing it as per the warning the return statement takes f() as its argument and explicitly calls it returning its value, though not executing the remaining functions. In this case as foo() (and f() though it&#39;s not shown) is void that is nothing. In fact if foo() or f() had non-void return types this wouldn&#39;t compile.&lt;br /&gt;
&lt;br /&gt;
The fix is easy. Just put a semi-colon after the return.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; foo()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;return&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;f&lt;/span&gt;()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;g&lt;/span&gt;()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;h&lt;/span&gt;()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #008400; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;// Do lots of other things...&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
I use this &#39;technique&#39; when I&#39;m debugging C++ where this works fine. This is slightly interesting as C++ has the same semantics. The following C++ code has the same problem as the&amp;nbsp;Swift, in that this code also invokes f() as its return value.&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;void&lt;/span&gt; foo()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;return&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;f&lt;/span&gt;();&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;g&lt;/span&gt;();&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;g&lt;/span&gt;();&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;





&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I guess the reason it&#39;s not an issue with C++ (as much or at all) is that my muscle memory or something else is always wanting to terminate lines with semi-colons so the natural way to write the return would be &#39;return;&#39; whereas in Swift without the semi-colon requirement it&#39;s natural not to hence this issue becomes slightly more prevalent.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2016/01/the-perils-of-debugging-with-return.html</link><author>noreply@blogger.com (Pete Barber)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGFWLvn3st3t6S50eh53QSRCVzQSKJTGBGKOgR_h2uTndGFtcB1XxzHACpwrf1nbF-IOsD5D870orzaHjny4MWhWNafmjkemQFrwZxx0mtCkkyfneQ2zDuOb8FfiCiRH1SdT5sgQw-pbs/s72-c/Untitled.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-7044138399773522438</guid><pubDate>Tue, 19 Jan 2016 21:34:00 +0000</pubDate><atom:updated>2016-01-19T13:48:36.069-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tvOS iOS swift &quot;Apple TV&quot; OAuth OAuth2 iCloud KV Key-Value Photone OneDrive</category><title>OAuth authentication on tvOS</title><description>Recently I&#39;ve just published an &lt;a href=&quot;http://www.apple.com/uk/tv/&quot;&gt;Apple TV&lt;/a&gt; (&lt;a href=&quot;https://developer.apple.com/tvos/&quot;&gt;tvOS&lt;/a&gt;) &lt;a href=&quot;https://itunes.apple.com/us/app/photone/id1068952548?mt=8&quot;&gt;App&lt;/a&gt; to view photos stored on &lt;a href=&quot;http://windows.microsoft.com/en-gb/windows-8/getting-started-onedrive-tutorial&quot;&gt;Microsoft OneDrive&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEin5n7_ytqbyahhfwAIuWMiIpNLasUoXACVadzgfn8KiTVwUsykkmzieYiY3CZ0sfiYqVRNxyDlTRtsPLllE4OJX1pLv6EWDcRq4wH6pZjodKuP3vNEuAbGnnxyF7e44Io6u4LpsHZB5xs/s1600/500x500bb.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;358&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEin5n7_ytqbyahhfwAIuWMiIpNLasUoXACVadzgfn8KiTVwUsykkmzieYiY3CZ0sfiYqVRNxyDlTRtsPLllE4OJX1pLv6EWDcRq4wH6pZjodKuP3vNEuAbGnnxyF7e44Io6u4LpsHZB5xs/s640/500x500bb.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Implementing this on tvOS rather than iOS presented one unique challenge. The &lt;a href=&quot;https://dev.onedrive.com/README.htm&quot;&gt;OneDrive REST API&lt;/a&gt;&amp;nbsp;requires OAuth2 &lt;a href=&quot;https://dev.onedrive.com/auth/readme.htm&quot;&gt;authentication&lt;/a&gt;&amp;nbsp;in order to obtain an OAuth token which is then used for all the other calls.&lt;br /&gt;
&lt;br /&gt;
Normally (well based on my limited experience) OAuth within Apps is handled by using a UIWebView along with delegate code that performs the OAuth handshake (image linked from IBM).&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;http://www.ibm.com/developerworks/library/wa-oauthsupport/ThreeLeggedOAuthDance.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://www.ibm.com/developerworks/library/wa-oauthsupport/ThreeLeggedOAuthDance.gif&quot; height=&quot;528&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
tvOS does not contain any form of web view, i.e. no UIWebView and no WKWebView (not that it would be that much use due to the lack of hooks). As the actual authentication is performed within the UIWebView by the authenticating 3rd party (Microsoft in this case requiring the user logs in with their Microsoft Account credentials) there&#39;s not a lot that can be done without it.&lt;br /&gt;
&lt;br /&gt;
However, both iOS and tvOS are generally logged into using an Apple Id which is also used to login into iCloud and generally for an Apple TV owned by the same person who owns another iOS device these use the same Apple Id. Therefore, what I did was to write a very simple iOS App that:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Performs the OAuth Authentication handshake&lt;/li&gt;
&lt;li&gt;Stores the resulting OAuth token in the iCloud KeyValue Store&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjyFeswZmd85U3-NqE_aaol92TQN4vZvLZaucUzEX050rQyb7AG8JnHUylUZ_hSLwrM9NORRNFLU3wknrOJwgAiY-D240pM5gReXldXt8ZG5m14eERQkkEZAZuTmXx3PrKwLV_A75vLaU/s1600/Simulator+Screen+Shot+19+Jan+2016%252C+21.09.54.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjyFeswZmd85U3-NqE_aaol92TQN4vZvLZaucUzEX050rQyb7AG8JnHUylUZ_hSLwrM9NORRNFLU3wknrOJwgAiY-D240pM5gReXldXt8ZG5m14eERQkkEZAZuTmXx3PrKwLV_A75vLaU/s320/Simulator+Screen+Shot+19+Jan+2016%252C+21.09.54.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsouJSW6_0hmJL-WWFo8SWP-0Uj5K4E335CJ7YVQvdlzAA7k3UYU-HoDL03hbrwShnfW_6KXOtKJIWpeAmphmGUa8BvlWibdGbfwy1p9bO7DQEMcRunVlxNboD4tjRV2QzRgwM6kJue84/s1600/Simulator+Screen+Shot+19+Jan+2016%252C+21.09.54.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsouJSW6_0hmJL-WWFo8SWP-0Uj5K4E335CJ7YVQvdlzAA7k3UYU-HoDL03hbrwShnfW_6KXOtKJIWpeAmphmGUa8BvlWibdGbfwy1p9bO7DQEMcRunVlxNboD4tjRV2QzRgwM6kJue84/s320/Simulator+Screen+Shot+19+Jan+2016%252C+21.09.54.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuNe-nG-sgfdAkf8ECPsC9JH4VmZDCYWs63FlOwJ7LvKtFXhtlxyjsqPOQKQTiQcF057r-MutnW6jtoB2uKcAHjBZdIWSbSad4M9Bc9rwW5xsZG6BUzdlFb5WJVvqzYfsSpSEpBkFfAqM/s1600/Simulator+Screen+Shot+19+Jan+2016%252C+21.10.17.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuNe-nG-sgfdAkf8ECPsC9JH4VmZDCYWs63FlOwJ7LvKtFXhtlxyjsqPOQKQTiQcF057r-MutnW6jtoB2uKcAHjBZdIWSbSad4M9Bc9rwW5xsZG6BUzdlFb5WJVvqzYfsSpSEpBkFfAqM/s320/Simulator+Screen+Shot+19+Jan+2016%252C+21.10.17.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoVBT0wARYXbgUcm-UWKUz81TBaWhGrs4iDTsfb5XvqqI0byZJuvWAUTVFhybM3CTMnT_1mWmQ3EV72RcuW0z20grgSG_AYdIov1ZjJapCRwqLrya9CC_2nkPWwtWWsyQ4UogjWaXNCGc/s1600/Simulator+Screen+Shot+19+Jan+2016%252C+21.10.24.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoVBT0wARYXbgUcm-UWKUz81TBaWhGrs4iDTsfb5XvqqI0byZJuvWAUTVFhybM3CTMnT_1mWmQ3EV72RcuW0z20grgSG_AYdIov1ZjJapCRwqLrya9CC_2nkPWwtWWsyQ4UogjWaXNCGc/s320/Simulator+Screen+Shot+19+Jan+2016%252C+21.10.24.png&quot; width=&quot;179&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
When written this is usually synchronized to iCloud very quickly. On the other side the tvOS app reads the iCloud KeyValue Store checking to see if the OAuth token exists.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_wQ7rvCnz2XVtk6v3SVf25PFaaBwg5wbEZ0TufJJ2PKwEExT69mfdSf007mAn4KaVER2wKEuLQ-e61flwmTybXihaBId1DLh_D3VlW3JbppaBlzCU49hBy_mHrvkh-xCdLYT2WwjTl7Q/s1600/Screen+Shot+2016-01-19+at+21.13.38.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;360&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_wQ7rvCnz2XVtk6v3SVf25PFaaBwg5wbEZ0TufJJ2PKwEExT69mfdSf007mAn4KaVER2wKEuLQ-e61flwmTybXihaBId1DLh_D3VlW3JbppaBlzCU49hBy_mHrvkh-xCdLYT2WwjTl7Q/s640/Screen+Shot+2016-01-19+at+21.13.38.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
If it does then it can continue as per any other App that has successfully performed the OAuth handshake.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEie70cGHGUYF3Tt2BRSBVZwYlx1OyIYxy96s5EYNGNx1U88xhspORM_vfvas7HjSEY-WnPPdlZkBQIB4-HltmzbhoL_lZZrwjVqKnm0tjklyUr7RrUQO8sH6yfWrtFsO2fHI9gfO5fvg2w/s1600/Screen+Shot+2016-01-19+at+21.14.42.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;360&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEie70cGHGUYF3Tt2BRSBVZwYlx1OyIYxy96s5EYNGNx1U88xhspORM_vfvas7HjSEY-WnPPdlZkBQIB4-HltmzbhoL_lZZrwjVqKnm0tjklyUr7RrUQO8sH6yfWrtFsO2fHI9gfO5fvg2w/s640/Screen+Shot+2016-01-19+at+21.14.42.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I believe that iCloud Storage and the process of writing to and reading from iCloud is secure. This is important as following the handshake the OAuth token acts effectively as a password. Each token obtained from Microsoft is valid for one hour so after that the user needs to perform the authentication from the iOS device again.&lt;br /&gt;
&lt;br /&gt;
It is possible to request an OAuth &lt;a href=&quot;http://OAUth2 refresh token&quot;&gt;refresh token&lt;/a&gt; which allows a client to update an expired token as long as access to OneDrive for the App has not been revoked. However, I prefer to err on the side caution at the moment. I also only request read-only access to OneDrive as well.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
For this to work the same user (Apple Id) needs to be logged into both the Apple TV and the iOS device as the same user and additionally be signed into iCloud on these devices. From a programmatic perspective both Apps need the iCloud capability enabling but only Key-value storage.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJcmMtuLLCjM3XMngycHi-qcTzkCGZlo4pAqK7BMVtRP_psLVHnvovwDuldZmIyhYGV27gSRJ2WV4ww7ZBX_ptEH7AkW2tHQln24gLoZc_3pGlpVptFbxnyPZQxyi4GjQcQ49tuzNwBA8/s1600/Untitled.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;336&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJcmMtuLLCjM3XMngycHi-qcTzkCGZlo4pAqK7BMVtRP_psLVHnvovwDuldZmIyhYGV27gSRJ2WV4ww7ZBX_ptEH7AkW2tHQln24gLoZc_3pGlpVptFbxnyPZQxyi4GjQcQ49tuzNwBA8/s640/Untitled.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
However, you&#39;ll notice that CloudKit has also been enabled. This is so that &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/CloudKit/Reference/CKContainer_class/&quot;&gt;CKContainer&lt;/a&gt; methods can be called. In particular (well only)&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #703daa; font-variant-ligatures: no-common-ligatures;&quot;&gt;CKContainer&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;.&lt;/span&gt;defaultContainer&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;().&lt;/span&gt;accountStatusWithCompletionHandler&lt;/div&gt;
&lt;br /&gt;
In order to establish whether the user is currently signed in to iCloud and any changes (signing in &amp;amp; out potentially as a different user) whilst the App is running.&lt;br /&gt;
&lt;br /&gt;
Enabling this automatically creates an Entitlement file (named &amp;lt;AppName&amp;gt;.entitlements) and within it creates Key confusingly called &#39;iCloud Key-Value Store&#39; with the default value of &#39;$(TeamIdentifierPrefix)$(CFBundleIdentifier)&#39; - this is not the key to access values you store BTW but is just the iCloud KV configuration. This will happen for both the iOS and tvOS Apps.&lt;br /&gt;
&lt;br /&gt;
NOTE: The two collapsed keys are as a result of enabling CloudKit.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHPjG3DbhKaNuhOYVfCb-OF1zr7-KczqshPeBpNXPfgjqK_h8qFkKr7HD8tKQwf9LR_dHcT8ukCkuliG_-2xrnaqB52oRk71LGGXZkdktbNKldeT0ktJObhs4O5YcsEXufJzRnN6E5wGs/s1600/ent.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;146&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHPjG3DbhKaNuhOYVfCb-OF1zr7-KczqshPeBpNXPfgjqK_h8qFkKr7HD8tKQwf9LR_dHcT8ukCkuliG_-2xrnaqB52oRk71LGGXZkdktbNKldeT0ktJObhs4O5YcsEXufJzRnN6E5wGs/s640/ent.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
For both Apps to have access to the same iCloud Key-Value storage the results of expanding the &#39;&#39;$(TeamIdentifierPrefix)$(CFBundleIdentifier)&#39; macros needs to be the same. For my App I&#39;ve created &amp;nbsp;a single App that has both an iOS and tvOS component so their CFBundleIdentifier is the same. The TeamIdentifierPrefix is taken from your Developer Apple Id.&lt;br /&gt;
&lt;br /&gt;
The first part of the value has to be $(TeamIdentifierPrefix) as in order to make the KV Storage secure this value forms part of the signing process. If you replaced the whole value with say &#39;BOB&#39; then it won&#39;t build properly.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV_V305r-vE_lcu4Fii6_RuXO3Qtp6FtMll8xIQFGG4tlu14Qz4wLS3LnrLuRmba1AHWNjB9cMMfbq6fOC4JA_UoGC_lfH_LrOCBjTNgQDgPyuvCQD0_lDeG8UFhziSpLbKuAofh80kGY/s1600/err.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;228&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV_V305r-vE_lcu4Fii6_RuXO3Qtp6FtMll8xIQFGG4tlu14Qz4wLS3LnrLuRmba1AHWNjB9cMMfbq6fOC4JA_UoGC_lfH_LrOCBjTNgQDgPyuvCQD0_lDeG8UFhziSpLbKuAofh80kGY/s640/err.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
As such it&#39;s possible for all your Apps (published from the with the same Apple Id) to share iCloud Key-Value Storage contents.&lt;br /&gt;
&lt;br /&gt;
Reading &amp;amp; writing is very simple. I just use a single Key-Value pair to read, write (&amp;amp; where necessary delete the token. This is accomplished using:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #703daa; font-variant-ligatures: no-common-ligatures;&quot;&gt;NSUbiquitousKeyValueStore&lt;/span&gt;.&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;defaultStore&lt;/span&gt;().&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;setDictionary&lt;/span&gt;(stuff.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;asDict&lt;/span&gt;(), forKey: &lt;span style=&quot;color: #d12f1b; font-variant-ligatures: no-common-ligatures;&quot;&gt;&quot;mykey&quot;&lt;/span&gt;)&lt;/div&gt;
&lt;br /&gt;
to write and&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #703daa; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt; stuff = &lt;/span&gt;NSUbiquitousKeyValueStore&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;defaultStore&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;().&lt;/span&gt;dictionaryRepresentation&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #d12f1b; font-variant-ligatures: no-common-ligatures;&quot;&gt;&quot;mykey&quot;&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;? [&lt;/span&gt;String&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;:&lt;/span&gt;AnyObject&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;]&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
to read.&lt;br /&gt;
&lt;br /&gt;
This example reads &amp;amp; writes a dictionary as I needed to set store a set of KV Pairs (a dictionary) as the value of a single KV-pair but fundamental data types can be stored directly too.&lt;br /&gt;
&lt;br /&gt;
When starting the App, according to the docs it is important to call &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/Foundation/Reference/NSUbiquitousKeyValueStore_class/index.html#//apple_ref/occ/instm/NSUbiquitousKeyValueStore/synchronize&quot;&gt;synchronize&lt;/a&gt; method to initiate timely iCloud synchronization.&lt;br /&gt;
&lt;br /&gt;
When the tvOS based Apple TV was first released there were various articles about how to enable users to login to their accounts for certain apps. These often involved similar configuration requiring the user to use an iOS device to input a string of numbers presented by the tvOS App. However, this solutions was usually for Apps that managed their own accounts. This solution is similar in that solves the cumbersome entry problem but also enables the use of browser (UIWebView) based OAuth2 on a device that directly support it.&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2016/01/oauth-authentication-on-tvos.html</link><author>noreply@blogger.com (Pete Barber)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEin5n7_ytqbyahhfwAIuWMiIpNLasUoXACVadzgfn8KiTVwUsykkmzieYiY3CZ0sfiYqVRNxyDlTRtsPLllE4OJX1pLv6EWDcRq4wH6pZjodKuP3vNEuAbGnnxyF7e44Io6u4LpsHZB5xs/s72-c/500x500bb.jpg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-283764829316701029</guid><pubDate>Wed, 23 Dec 2015 12:19:00 +0000</pubDate><atom:updated>2015-12-23T04:19:21.731-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">git OneDrive iCloud remote clone backup GitHub</category><title>Git remote repos with OneDrive</title><description>I have various&amp;nbsp;&lt;a href=&quot;https://github.com/petebarber?tab=repositories&quot;&gt;public&lt;/a&gt; git repositories on &lt;a href=&quot;https://github.com/&quot;&gt;GitHub&lt;/a&gt;&amp;nbsp;but I like to keep some source (usually my active &lt;a href=&quot;https://itunes.apple.com/us/developer/peter-barber/id788569890&quot;&gt;App Store apps&lt;/a&gt;) private. Whilst it&#39;d be nice to use GitHub private repositories, given my Apps are for fun and don&#39;t really make anything, don&#39;t require collaboration then the pricing is prohibitive.&lt;br /&gt;
&lt;br /&gt;
However, I really like the idea of having an offsite copy of my repository. As it happens I have an Office 365 Subscription which comes with 1TB of OneDrive space. I use OneDrive on OSX to sync a bunch of folders. I could use a synchronised OneDrive for my work directory but I don&#39;t want OneDrive synchronising all the temporary build files etc every time I build.&lt;br /&gt;
&lt;br /&gt;
It turns out the ideal solution is to create a remote clone in a OneDrive synchronised folder. In fact I have a dedicated OneDrive folder called &#39;src&#39; that contains clones of all of my git repos. Then, each time I commit to the local git repository and push OneDrive performs the synchronisation. If it happens that the code I&#39;m working on is public then adding a public GitHub repo is a doddle.&lt;br /&gt;
&lt;br /&gt;
Having created a local Git repo (though usually Xcode does this when starting a new project) it&#39;s easy to create the OneDrive clone:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;cd /Users/Pete/OneDrive/src&lt;/li&gt;
&lt;li&gt;git clone --bare file:////Users/Pete/Projects/&amp;lt;ProjectName&amp;gt;/.git &amp;lt;ProjectName&amp;gt;.git&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
As the remote clone is really just a backup come master that will never be working repository I create a bare clone and suffix the directory name with &#39;.git&#39;. I think this is a fairly common convention.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
At this point the source repository is cloned but the source is now a remote of the new repository rather than than the other way round. This is easy to fix:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
cd-ing into &amp;lt;ProjectName&amp;gt;.git (my example Project is Photone&amp;gt; and running git remotes gives:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
~/OneDrive/src/Photone.git[23]git remote -v&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
origin&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;file:////Users/Pete/Projects/PhotoneViewer/.git (fetch)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
origin&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;file:////Users/Pete/Projects/PhotoneViewer/.git (push)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-family: -webkit-standard; font-size: xx-small;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;/div&gt;
than running:
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;git remote remove origin&lt;/li&gt;
&lt;/ol&gt;
Removes the relationship between the new remote and the original source. To implement the desired reverse relationship:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;cd /Users/Pete/Projects&amp;lt;ProjectName&amp;gt;&lt;/li&gt;
&lt;li&gt;git remote add OneDrive file:////Users/Pete/OneDrive/src/&amp;lt;ProjectName&amp;gt;.git&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
I name my OneDrive remote repos &#39;OneDrive&#39;. This helps if I have multiple remotes.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
git remote (for my current project) now gives:&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
~/Projects/PhotoneViewer[49]git remote -v&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
OneDrive&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;file://Users/Pete/OneDrive/src/Photone.git (fetch)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
OneDrive&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;file://Users/Pete/OneDrive/src/Photone.git (push)&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
From this point onwards I use SourceTree. However, if you use the command line then a couple of extra steps are required otherwise git complains.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
Firstly, when pushing if you just want to do:&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
git push OneDrive you need to tell git that the new OneDrive remote is the master. This is done by:&lt;/div&gt;
&lt;div style=&quot;line-height: normal;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
git push --set-upstream OneDrive master&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: -webkit-standard; font-size: xx-small;&quot;&gt;Secondly, unless you&#39;ve already set the push.default setting or just use &#39;git push --all&#39; then you&#39;ll need to decide which option you want. The help from git describes these well:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
Git 2.0 from &#39;matching&#39; to &#39;simple&#39;. To squelch this message&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
and maintain the traditional behavior, use:&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; git config --global push.default matching&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
To squelch this message and adopt the new behavior now, use:&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&amp;nbsp; git config --global push.default simple&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
When push.default is set to &#39;matching&#39;, git will push local branches&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
to the remote branches that already exist with the same name.&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
Since Git 2.0, Git defaults to the more conservative &#39;simple&#39;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
behavior, which only pushes the current branch to the corresponding&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
remote branch that &#39;git pull&#39; uses to update the current branch.&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
See &#39;git help config&#39; and search for &#39;push.default&#39; for further information.&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
(the &#39;simple&#39; mode was introduced in Git 1.7.11. Use the similar mode&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&#39;current&#39; instead of &#39;simple&#39; if you sometimes use older versions of Git)&lt;/div&gt;
&lt;br /&gt;
If you&#39;re using the remote clone as a backup then perhaps the original, i.e. matching behaviour is&amp;nbsp;desirable.
Whilst I use OneDrive this configuration should work for any other file synchronisation service, e.g. iCloud, DropBox or a standard mounted File System, e.g. Samba, NFS etc.
&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2015/12/git-remote-repos-with-onedrive.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-3585938887593150731</guid><pubDate>Wed, 23 Dec 2015 10:50:00 +0000</pubDate><atom:updated>2015-12-23T02:50:43.580-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">swift enum protocol</category><title>Swift Enums and Protocols</title><description>I&#39;m trying to clear out my inbox before Christmas and I noticed an emails to myself entitled &#39;Enum question. Add protocol to enum?&#39;.&lt;br /&gt;
&lt;br /&gt;
The short answer is yes. The longer one. Take the following protocol&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
protocol&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt; Foo&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; f() -&amp;gt; &lt;span style=&quot;color: #703daa; font-variant-ligatures: no-common-ligatures;&quot;&gt;Void&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;


&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
A simple enum can be created that implements it:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;enum&lt;/span&gt; Test: &lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;Foo&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;case&lt;/span&gt; One&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; f()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/div&gt;
&lt;div style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;(&lt;/span&gt;&quot;Hello&quot;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
So the following short program:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;let&lt;/span&gt; baz = &lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;Test&lt;/span&gt;.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;One&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal; min-height: 13px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;baz&lt;/span&gt;.&lt;span style=&quot;color: #31595d; font-variant-ligatures: no-common-ligatures;&quot;&gt;f&lt;/span&gt;()&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
generates the output:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;Hello&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;Program ended with exit code: 0&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-family: -webkit-standard; font-size: small;&quot;&gt;The protocol can also be implemented by extension so the following is equivalent and produces the same results:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;font-family: -webkit-standard; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
extension&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;Test&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;: &lt;/span&gt;&lt;span style=&quot;color: #4f8187; font-variant-ligatures: no-common-ligatures;&quot;&gt;Foo&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
{&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bb2ca2; font-variant-ligatures: no-common-ligatures;&quot;&gt;func&lt;/span&gt; f()&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/div&gt;
&lt;div style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-variant-ligatures: no-common-ligatures;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;(&lt;/span&gt;&quot;Hello&quot;&lt;span style=&quot;color: black; font-variant-ligatures: no-common-ligatures;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;





&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; line-height: normal;&quot;&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
It&#39;s not surprising that Swift&#39;s enum types support protocols. Off the top of my head I can&#39;t think of any clever reasons why you&#39;d have enums implement a protocol. Given an enum is a first class type in Swift then it makes perfect sense. In fact it&#39;s documented on page 424 in The Swift Programming Language.&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2015/12/swift-enums-and-protocols.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-6497483439954850033</guid><pubDate>Tue, 27 Oct 2015 12:46:00 +0000</pubDate><atom:updated>2015-10-27T05:46:30.838-07:00</atom:updated><title>Things I learnt from Swift Summit</title><description>I attended the first &lt;a href=&quot;https://www.swiftsummit.com/&quot;&gt;Swift Summit&lt;/a&gt; on 21st of March; there were two days but I only went to the first. Here are some of the facts I learned:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Int is not a fundamental type as you would think of it in most languages.&amp;nbsp;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Instead it&#39;s a struct that derives SignedIntegerType with the actual value being an instance of the really fundamental type Bultin.World&lt;/li&gt;
&lt;li&gt;Being a struct means it&#39;s a proper object hence its methods, the ability to extend (see later) and can (&amp;amp; does )implement protocols&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Int can be extended&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;As it&#39;s an object (see above) it&#39;s possible to write extensions methods.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;I have a far better understanding of what @autoclosure does now&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;It basically captures &amp;nbsp;the function rather than invoking it&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;nil is never actually treated as nil when used&lt;/li&gt;
&lt;/ul&gt;
And now for some opinion about the day.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;People seem to be struggling with error handling&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;A couple of talks presented code to avoid pyramids of doom in regard to making a call, checking for success/failure and if successful continuing.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;People think they&#39;re doing Functional Programming&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Just because Swift supports Functional Programming style and some people use elements of FP they assume Swift is mainly a Functional language and that they are doing Functional Programming.&lt;/li&gt;
&lt;li&gt;Passing functions arounds as first-class objects does not make your program functional&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Some people now hate Objective-C&lt;/li&gt;
&lt;/ul&gt;
</description><link>http://petebarber.blogspot.com/2015/10/things-i-learn-from-swift-summit.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-6230312953733826396</guid><pubDate>Thu, 10 Sep 2015 21:07:00 +0000</pubDate><atom:updated>2015-09-10T14:54:49.548-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">&quot;Apple TV&quot;</category><category domain="http://www.blogger.com/atom/ns#">&quot;Jamerson Quave&quot;</category><category domain="http://www.blogger.com/atom/ns#">Swift</category><category domain="http://www.blogger.com/atom/ns#">tvOS</category><title>A slight enhancement on Developing tvOS Apps with Swift</title><description>Apple announced tvOS yesterday. Downloading Xcode 7.1 Beta comes with the SDK and simulator for tvOS apps. The &lt;a href=&quot;https://developer.apple.com/library/prerelease/tvos/documentation/General/Conceptual/AppleTV_PG/YourFirstAppleTVApp.html&quot;&gt;official documentation&lt;/a&gt; starts to run through how to create a basic app but is doesn&#39;t mention where to place and load the JS from and the same for the TVML.&lt;br /&gt;
&lt;br /&gt;
Fortunately and vert quickly &lt;a href=&quot;http://jamesonquave.com/&quot;&gt;Jamerson Quave&lt;/a&gt; put together a &lt;a href=&quot;http://jamesonquave.com/blog/developing-tvos-apps-for-apple-tv-with-swift/&quot;&gt;tutorial&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I followed the Apple docs but checked Jameson&#39;s tutorial to verify the missing declaration of&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;var appController: TVApplicationController?
&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
from AppDelegate and also for the JS and then TVML loading. I don&#39;t understand and the docs don&#39;t seem to see where the JS &amp;amp; &lt;a href=&quot;https://developer.apple.com/library/prerelease/tvos/documentation/LanguagesUtilities/Conceptual/ATV_Template_Guide/index.html#//apple_ref/doc/uid/TP40015064-CH41-SW1&quot;&gt;TVML&lt;/a&gt; should be loaded from. They seem to suggest it should be remote, i.e. not part of the App Bundle but I don&#39;t know why. Anyhow I thought I&#39;d see if I could.&lt;br /&gt;
&lt;br /&gt;
The following assumes you&#39;ve got to the end of Jameson&#39;s tutorial.&lt;br /&gt;
&lt;br /&gt;
Loading the JS file; that then loads the TVML is easy. Add main.js to your application and change the lines within application:didFinishLaunchingWithOptions in AppDelegate.swift from:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;
let jsFilePath = NSURL(string: &quot;http://localhost:8000/main.js&quot;)
let javascriptURL = jsFilePath!

appControllerContext.javaScriptApplicationURL = javascriptURL!

&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
To&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;guard let jsUrl = NSBundle.mainBundle().URLForResource(&quot;main&quot;, withExtension: &quot;js&quot;) else&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;{&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;&amp;nbsp; &amp;nbsp; return false&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;}&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This just loads the Javascript file (main.js) from the bundle instead. It&#39;s not a great improvement but it removes one dependency on the local web server.&lt;br /&gt;
&lt;br /&gt;
I then tried to add hello.tvml to the bundler and modify main.js to create the fetched (via XmlHttpRequest). Unfortunately I couldn&#39;t create the Document in the JS. It seems that the normally (I&#39;ve not done JS in a long time so what do I know) available document object isn&#39;t available to more documents and/or elements can&#39;t be created.&lt;br /&gt;
&lt;br /&gt;
An attempt to create one, i.e.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;var otherDoc = Document()&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
gives&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;b&gt;015-09-10 21:53:50.213 tv1[55699:1483712] ITML &amp;lt;Error&amp;gt;: Document is not a function. (In &#39;Document()&#39;, &#39;Document&#39; is an instance of IKDOMDocumentConstructor) - file:///Users/pete/Library/Developer/CoreSimulator/Devices/C2E7E5BD-1823-48BF-89E9-D3A499EE778A/data/Containers/Bundle/Application/F9C514E1-1A95-46A8-83D1-1BC96BC9A220/tv1.app/main.js - line:18:25&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
The objects mentioned in the&amp;nbsp;&lt;a href=&quot;https://developer.apple.com/library/prerelease/tvos/documentation/TVMLJS/Reference/TVJSDevice_Ref/index.html#//apple_ref/javascript/cl/Device&quot;&gt;TVJS documentation&lt;/a&gt;&amp;nbsp;don&#39;t seem to be able to create one either.&lt;br /&gt;
&lt;br /&gt;
Anyway, hopefully another small step. &lt;a href=&quot;https://github.com/petebarber/tv1&quot;&gt;Full source on github&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
A good be being dumb here and another look at the docs &amp;amp; samples suggests that writing apps. via JS is just one way and that a more iOS like app. can be written. Perhaps this is similar to Windows Metro that had both a JS and .Net (C#) version of WinRT; &amp;amp; C++ for completion.&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2015/09/a-slight-enhancement-on-developing-tvos.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-2311072712439176026</guid><pubDate>Mon, 15 Jun 2015 20:57:00 +0000</pubDate><atom:updated>2015-06-15T13:57:26.518-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">iOS Swift &quot;Sprite Kit&quot; &quot;Hungry Bunny&quot; Keychain NSCoding</category><title>Release news: Hungry Bunny &amp; KeyChainItemCRUDKit</title><description>Not a technical post today, just a bit of news on the things I&#39;ve been working on.&lt;br /&gt;
&lt;br /&gt;
Firstly, my latest SpriteKit game written in Swift is now available on the &lt;a href=&quot;https://itunes.apple.com/us/app/hungry-bunny/id992219160?mt=8&quot;&gt;App Store&lt;/a&gt;. It&#39;s called Hungry Bunny and is effectively an endless runner/skill test. It&#39;s free with ads.
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;http://a4.mzstatic.com/us/r30/Purple7/v4/1c/ea/29/1cea29b4-d26e-8fb2-cc73-bda938af4ec1/screen322x572.jpeg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://a4.mzstatic.com/us/r30/Purple7/v4/1c/ea/29/1cea29b4-d26e-8fb2-cc73-bda938af4ec1/screen322x572.jpeg&quot; height=&quot;320&quot; width=&quot;180&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Secondly, now that Hungry Bunny is complete I&#39;ve returned to working on another project. This is nowhere near complete but I got to the point where I needed to securely store an OAuth2 token on iOS. I came across the &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/&quot;&gt;Keychain API&lt;/a&gt;. However, the API for this was long winded and I only wanted to use it in a simple manner. Therefore I created a Swift framework&amp;nbsp;to provide CRUD access to it along with a higher level interface where any type conforming to &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/&quot;&gt;NSCoding&lt;/a&gt;&amp;nbsp;and be saved, loaded &amp;amp; deleted.&lt;br /&gt;
&lt;br /&gt;
This is available on &lt;a href=&quot;https://github.com/petebarber/KeyChainItemCRUDKit&quot;&gt;github&lt;/a&gt; and also as my first ever &lt;a href=&quot;https://cocoapods.org/pods/KeyChainItemCRUDKit&quot;&gt;CocoaPod&lt;/a&gt;. All the docs are in the README plus there&#39;s an example iOS program (Single View App) and the Unit Tests.
&lt;br /&gt;
&lt;br /&gt;
&lt;div id=&quot;ibb-widget-root-992219160&quot;&gt;
&lt;/div&gt;
&lt;script&gt;(function(t,e,i,d){var o=t.getElementById(i),n=t.createElement(e);o.style.height=250;o.style.width=300;o.style.display=&#39;inline-block&#39;;n.id=&#39;ibb-widget&#39;,n.setAttribute(&#39;src&#39;,(&#39;https:&#39;===t.location.protocol?&#39;https://&#39;:&#39;http://&#39;)+d),n.setAttribute(&#39;width&#39;,&#39;300&#39;),n.setAttribute(&#39;height&#39;,&#39;250&#39;),n.setAttribute(&#39;frameborder&#39;,&#39;0&#39;),n.setAttribute(&#39;scrolling&#39;,&#39;no&#39;),o.appendChild(n)})(document,&#39;iframe&#39;,&#39;ibb-widget-root-992219160&#39;,&quot;banners.itunes.apple.com/banner.html?partnerId=&amp;aId=&amp;bt=catalog&amp;t=catalog_black&amp;id=992219160&amp;c=gb&amp;l=en-GB&amp;w=300&amp;h=250&quot;);&lt;/script&gt;
</description><link>http://petebarber.blogspot.com/2015/06/release-news-hungry-bunny.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-5072581411528370455</guid><pubDate>Sat, 02 May 2015 19:49:00 +0000</pubDate><atom:updated>2015-05-03T02:45:41.081-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">bitmap</category><category domain="http://www.blogger.com/atom/ns#">OS X</category><category domain="http://www.blogger.com/atom/ns#">persist</category><category domain="http://www.blogger.com/atom/ns#">PNG</category><category domain="http://www.blogger.com/atom/ns#">Swift</category><title>Drawing into bitmaps and saving as a PNG in Swift on OS X</title><description>Not an in depth post today. For a small iOS Swift/SpriteKit game I&#39;m writing for fun I wanted a very basic grass sprite that could be scrolled; to create a parallax effect. This amounts to a 800x400 bitmap which contains sequential isosceles triangles of 40 pixels with random heights (of up to 400 pixels) and coloured using a lawn green colour.&lt;br /&gt;
&lt;br /&gt;
Initially I was creating an SKShapeNode and creating the triangles above but when scrolling the redrawing of these hurt performance, especially when running on the iOS Simulator hence the desire to use a sprite.&lt;br /&gt;
&lt;br /&gt;
I had a go at creating these with Photoshop. Whilst switching to a sprite improved performance the look of the triangles drawn by hand wasn&#39;t as good as the randomly generated ones. Therefore I thought I&#39;d generate the sprite.&lt;br /&gt;
&lt;br /&gt;
It wasn&#39;t really practical to do this on iOS as the file was needed in Xcode so I thought I&#39;d try experimenting with a command line OS X (Cocoa) program in Swift. A GUI would possibly be nice to preview the results (and re-generate if needed) and to select the save-to file location but this solution sufficed.&lt;br /&gt;
&lt;br /&gt;
I&#39;d not done any non-iOS Swift development and never generated PNGs so various amounts of Googling and StackOverflow-ing was needed. Whilst the results of these searches were very helpful I didn&#39;t come across anything showing a complete program to create a bitmap, draw into it and then save so the finished program is presented below. It&#39;s also available as a &lt;a href=&quot;https://gist.github.com/59b6a76c84713f19b799.git&quot;&gt;gist&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre  style=&quot;font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;&quot;&gt;&lt;code style=&quot;color:#000000;word-wrap:normal;&quot;&gt;1:  import Cocoa  
2:    
3:  private func saveAsPNGWithName(fileName: String, bitMap: NSBitmapImageRep) -&amp;gt; Bool  
4:  {  
5:      let props: [NSObject:AnyObject] = [:]  
6:      let imageData = bitMap.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: props)  
7:        
8:      return imageData!.writeToFile(fileName, atomically: false)  
9:  }  
10:    
11:  private func drawGrassIntoBitmap(bitmap: NSBitmapImageRep)  
12:  {  
13:      var ctx = NSGraphicsContext(bitmapImageRep: bitmap)  
14:        
15:      NSGraphicsContext.setCurrentContext(ctx)  
16:        
17:      NSColor(red: 124 / 255, green: 252 / 255, blue: 0, alpha: 1.0).set()  
18:        
19:      let path = NSBezierPath()  
20:        
21:      path.moveToPoint(NSPoint(x: 0, y: 0))  
22:        
23:      for i in stride(from: 0, through: SIZE.width, by: 40)  
24:      {  
25:          path.lineToPoint(NSPoint(x: CGFloat(i + 20), y: CGFloat(arc4random_uniform(400))))  
26:          path.lineToPoint(NSPoint(x: i + 40, y: 0))  
27:      }  
28:        
29:      path.stroke()  
30:      path.fill()  
31:        
32:  }  
33:    
34:  let SIZE = CGSize(width: 800, height: 400)  
35:    
36:  if Process.arguments.count != 2  
37:  {  
38:      println(&quot;usage: grass &amp;lt;file&amp;gt;&quot;)  
39:      exit(1)  
40:  }  
41:    
42:  let grass = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(SIZE.width), pixelsHigh: Int(SIZE.height), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSDeviceRGBColorSpace, bytesPerRow: 0, bitsPerPixel: 0)  
43:        
44:  drawGrassIntoBitmap(grass!)  
45:  saveAsPNGWithName(Process.arguments[1], grass!)  
46:    
&lt;/code&gt;&lt;/pre&gt;</description><link>http://petebarber.blogspot.com/2015/05/drawing-into-bitmaps-and-saving-as-png.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-155006113146436654</guid><pubDate>Tue, 24 Feb 2015 21:47:00 +0000</pubDate><atom:updated>2015-02-24T13:47:43.675-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">swift let var value reference type container</category><title>When a Swift immutable collection isn&#39;t or is at least immutable-ish</title><description>Take the following code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; struct Foo  
 {  
     var value: Int  
 }  
   
 let foo = [Foo(value: 1), Foo(value: 2)]  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
By using &#39;let foo&#39; an immutable array has been created. As such no members of this array can be replaced and nor can an element&#39;s contents be modified. As such both statements below result in compilation errors.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; foo[0] = Foo(value: 777) // error!  
 foo[0].value = 8 // error!  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
If foo were declared var then both of the above statements would work.&lt;br /&gt;
&lt;br /&gt;
This changes slightly when using reference types:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; class Bar  
 {  
     var value: Int = 0  
       
     init(value: Int)  
     {  
         self.value = value  
     }  
 }  
   
 let bar = [Bar(value: 1), Bar(value: 2)]  
   
 bar[0] = Foo(value: 777) // error!  
 bar[0].value = 8 // allowed  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The first case of trying to replace an element fails as before but modifying the instance referenced by that element is permitted. This is because the immutable collection holds a reference to the instance of Bar. Modifying the instance does not change the reference so the collection remains unchanged.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
If you&#39;re making an effort to make your code as immutable (const) as possible (I&#39;m from a C++ background so I endeavour to making everything as const I can) then this is a gotcha to lookout for.&lt;br /&gt;
&lt;br /&gt;
The only way to make Bar properly const is to provide a private setter for the member, assuming it&#39;s defined in it&#39;s own source file (so this example doesn&#39;t work in a Plaground), i.e.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; class Baz  
 {  
     &lt;b&gt;private(set) &lt;/b&gt;var value: Int = 0  
       
     init(value: Int)  
     {  
         self.value = value  
     }  
       
     func f()  
     {  
         value = 88;  
     }  
 }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Which now prevents value being assigned too.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; let baz = [Baz(value: 1), Baz(value: 2)]  
 baz[0].value = 8 // error!  
   
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
Even if all the properties have private setters there might be methods on the reference type that modify the instance, such as f() above. Just having an immutable collection of reference types is not sufficient to stop these mutating the instances referred too. Invoking f() in the above collection will change the value to 88, i.e.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt; println(&quot;\(baz[0].value)&quot;) // Gives 1  
 baz[0].f()  
 println(&quot;\(baz[0].value)&quot;) // Gives 88  
   
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This differs to C++ where if a collection is const or is returned as a const reference (from a method) than not only is the container immutable but so are its contents. It would be good if Swift were to gain a mechanism that would mark the collection and its contents completely immutable other than having to use a value type.&lt;br /&gt;
&lt;br /&gt;
Anyway, beware of making a collection of reference types immutable and assuming that both the collection and its contents cannot be modified!</description><link>http://petebarber.blogspot.com/2015/02/when-swift-immutable-collection-isnt-or.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-717361374381367602</guid><pubDate>Sun, 22 Feb 2015 15:30:00 +0000</pubDate><atom:updated>2015-02-23T06:09:30.744-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Swift Objective-C iOS</category><title>Book review: Swift Essentials</title><description>&lt;h4&gt;
Disclaimer&lt;/h4&gt;
&lt;br /&gt;
&lt;i&gt;I was asked to review:&amp;nbsp;&lt;a href=&quot;http://bit.ly/SwE6701&quot;&gt;Swift Essentials&lt;/a&gt;&amp;nbsp;by &lt;a href=&quot;https://twitter.com/alblue&quot;&gt;Alex Blewitt&lt;/a&gt; from Packt Publishing (I&#39;d previously reviewed another book of theirs for the &lt;a href=&quot;http://accu.org/&quot;&gt;ACCU&lt;/a&gt;) and ideally publicise the review. In return I was given a free copy of the eBook and offered another free eBook of my choice upon publication of this review. However, no pressure was exerted and nor was any editorial control requested or given.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;
In short&lt;/h4&gt;
&lt;br /&gt;
It&#39;s a good book that provides a more than basic introduction to iOS development using Swift. My major criticism is the title, it does not do the book justice. The sub-title &#39;Get up and running lightning fast with this practical guide to building applications with Swift&#39; is a far better description. This book is ideal if you&#39;re already able to program but would like to learn about iOS development with Swift rather than with Objective-C. It&#39;s more about learning iOS app development with Swift rather than about Swift.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;
In long&lt;/h4&gt;
&lt;br /&gt;
The offer to review this book came at good time for me. I&#39;d read quite a lot of the&amp;nbsp;Swift Programming Language &lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309&quot;&gt;book&lt;/a&gt;, read many blog articles, attended most of the&amp;nbsp;&lt;a href=&quot;https://twitter.com/swiftldn&quot;&gt;Swift London&lt;/a&gt;&amp;nbsp;meet ups (which incidentally gets a mention in the resource section of the book; so I may have inadvertently met the author) and published my first&amp;nbsp;iOS App written entirely in Swift, a game called &lt;a href=&quot;https://itunes.apple.com/cg/app/boingy-splat/id955258511?mt=8&quot;&gt;Boingy Splat&lt;/a&gt;. Therefore I was interested to see what someone else made of the language itself, working with it to create iOS apps and importantly to see parts and techniques I&#39;d missed.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
The books takes you from doing mainly pure Swift using REPL on the command line and then introducing and using Playgrounds to implementing a full app, albeit without persisting any data. Smaller apps demonstrating features that will be used in the final app are created along the way. These are small and snappy and allow something concrete to be created along the way, quickly! There is a section on creating Playground packages and adding documentation that seems far from essential; interesting though.&lt;br /&gt;
&lt;br /&gt;
Some of the first chapter introducing the fundamental types and constructs can get a bit dull if you&#39;ve been there with other languages but it&#39;s information that needs presenting. Importantly as soon as possible the Swift idioms (iteration, containers, enums, let versus var etc.) are presented. This is the information that existing developers need.&lt;br /&gt;
&lt;br /&gt;
The Playgrounds chapters introduces a little Cocoa Touch by the way of displaying images. Other than the choice of the author&#39;s face as the content (ironic rather than egomaniacal I hope :-)) it&#39;s good to be already manipulating the OS with Swift rather than just &#39;doing&#39; Swift.&lt;br /&gt;
&lt;br /&gt;
From chapter 3 onwards (there are 7 in total) the focus switches to creating apps. The most important concepts (that will allow you to search for and understand more detailed documentation and blogs) are introduced, i.e. Table Views, Master-Detail, Storyboards (and creating purely programmatic views), Custom Views, MVC architecture and accessing remote data. No time is spent on app signing etc. which is an understandable omission but a mention and a reference could prove useful for those very new to iOS development.&lt;br /&gt;
&lt;br /&gt;
The later chapters tend to introduce a new iOS features and new Swift feature making use of one with the other. Even Unit Testing gets a mention when introducing classes and using mocking as example of sub-classing. Likewise full and practical use of Swift&#39;s enums and extension methods are given in the remote data chapter.&lt;br /&gt;
&lt;br /&gt;
I was pleased to see Storyboards getting central billing for creating UIs as this is now more or less de rigueur and was always my issue with the Big Nerd Ranch book. The use of Segues is also covered along with how to create the UI using Interface Builder. In this chapter and the rest of the book a fair amount of time is spent showing how to pass data from from View to another. It&#39;s a shame the &lt;a href=&quot;http://en.wikipedia.org/wiki/Singleton_pattern&quot;&gt;Singleton&lt;/a&gt; anti-pattern is presented as a way to share data in the final program especially given the coupling this causes which makes Unit Testing as advocated earlier in the book hard to accomplish.&lt;br /&gt;
&lt;br /&gt;
As this is a new book it was good to see it tackle Auto Layout and not just from the Interface Builder perspective but go so far to explain and use the &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage/VisualFormatLanguage.html&quot;&gt;Visual Format Language&lt;/a&gt;. It&#39;s quite a feat to present this succinctly and not just cop out with a reference to the official documentation. It&#39;s this information that takes a somewhat scary area of iOS development and removes the fear.&lt;br /&gt;
&lt;br /&gt;
The remote data chapter is good in that talks about synchronous versus asynchronous calls and makes use of Swift Closures for the callbacks. It covers useful details such as having to update the UI on the main thread versus performing work on others and shows the mechanisms to achieve this. A small library of useful methods that can be used outside of the book is created in the process. Again, this isn&#39;t super detailed information but it&#39;s setting new iOS developers on the right course allowing common areas of pain and confusion to be avoided.&lt;br /&gt;
&lt;br /&gt;
Other than title the I have only a couple of criticisms. Firstly, I kept finding references to things either just before they were presented or to unhighlighted aspects of the code. This suggested that in order to keep the size of the book down that various pages had been chopped but less than perfect re-editing had occurred. I was able to reconcile all of these but it meant spending time flicking back through pages. Secondly, in a similar vein whilst the book contains screenshots there are sections where some intermediate screenshots or those of the final effect would aid the description. It&#39;s arguable as to whether the persistence of data is an &#39;essential&#39;. I think it would have been worth taking the page count to 250 (from the current 228) to cover basic persistence; 2nd edition?&lt;br /&gt;
&lt;h4&gt;
&lt;/h4&gt;
&lt;h4&gt;
Formatting&lt;/h4&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
I read the eBook on iPad mini 2 using the Kindle reader app (mobi format); I haven&#39;t seen the paper version.&lt;br /&gt;
&lt;br /&gt;
For technical eBooks, especially to be read on anything smaller than a full size tablet means the formatting, especially of the code is very important. The text is laid out well and most of the code is split-up so the differences between text and code highlight the other which works well. There were a few times when I needed to switch to landscape mode to make the code more readable.&lt;br /&gt;
&lt;br /&gt;
Towards the end where a full program in larger sections is presented it breaks down as there is little text on the same page. Different colours would have been useful for the comments and given the use of inline lambdas (for asynchronous calls) then some other formatting or again another colour would have helped separate these from the code consuming them; though I did notice some use of bold. The use of ANSI rather K&amp;amp;R style braces (though this seems preferred by Apple and is more compact) may have help readability.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;
Conclusion&lt;/h4&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
When Swift was first released (June &#39;14) a lot of people thought that learning Swift would make then iOS developers whereas the real challenge (other than learning how to program) is to learn how to program against iOS. At that time learning just Swift by itself as a first language had no real benefit especially as all the books and blog articles used Objective-C plus the Cocoa Touch APIs were still rather Swift unfriendly.&lt;br /&gt;
&lt;br /&gt;
Whilst knowing Objective-C is probably essential for a full-time iOS developer today and a basic knowledge will be useful for a long time to come, 8 months down the line learning iOS development just with Swift is extremely viable. This book will allow you to do this. It&#39;s not as comprehensive at the de facto book for learning iOS development: &lt;a href=&quot;http://www.bignerdranch.com/we-write/ios-programming/&quot;&gt;Big Nerd Ranch&#39;s iOS Programming&lt;/a&gt; but it is more than sufficient, uses the latest language and covers features introduced with Xcode 6, e.g. Auto Layout and demonstrates app development along the lines of how Apple sees it, by using Storyboards and Auto Layout. It&#39;s also significantly shorter!&lt;br /&gt;
&lt;br /&gt;
Whilst it doesn&#39;t cover everything and what it does cover doesn&#39;t go into great detail it introduces and uses most of the key concepts of both Swift and iOS. To write an app you&#39;ll still need to read and refer to the official documentation and read blogs etc. but after reading this book there will not be much that is alien to you.&lt;br /&gt;
&lt;br /&gt;
I&#39;d recommend this book if you can already program and would like to learn how to develop for iOS. If you already know iOS but would like to learn Swift with familiar examples and see how to access the Cocoa APIs using Swift then this is also a good book. Even if you can&#39;t program and aspire to iOS development then this is a book that you could pick up and get a long with pretty soon after you&#39;d got to grips with the basics of programming.&lt;br /&gt;
&lt;br /&gt;
I think it is possible for this to become the &#39;go to&#39; book for starting iOS development.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2015/02/book-review-swift-essentials.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-4754543483907067222</guid><pubDate>Wed, 28 Jan 2015 13:59:00 +0000</pubDate><atom:updated>2015-01-28T05:59:52.724-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">swift &quot;optional chaining&quot; Dictionary</category><title>Optional chaining with dictionaries (in Swift)</title><description>Whilst learning Swift and &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40013043-CH1-SW1&quot;&gt;SpriteKit&lt;/a&gt;&amp;nbsp;I came across an issue with the documentation for &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKNode_Ref/index.html#//apple_ref/occ/instp/SKNode/userData&quot;&gt;SKNode.userData&lt;/a&gt;. In an early XCode beta it was specified as an implicitly unwrapped optional whereas now it is clearly an optional.&lt;br /&gt;
&lt;br /&gt;
Therefore the code I originally used to access values was:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;var node = SKNode()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;...&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;if let userData = node.userData&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; if let value = userData[&quot;key&#39;]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; // do something with value&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Once I discovered optional chaining I was able to shorten this to:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;var node = SKNode()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;...&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;if let value = userData&lt;b&gt;?&lt;/b&gt;[&quot;key&quot;]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; // do something with value&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I.e. Use optional chaining to check for existence of userData&lt;br /&gt;
&lt;br /&gt;
This is not something I&#39;ve seen written about before.&amp;nbsp;&lt;a href=&quot;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html#//apple_ref/doc/uid/TP40014097-CH21-ID245&quot;&gt;Optional chaining &amp;nbsp;of subscripts&lt;/a&gt; is mentioned in &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/index.html#//apple_ref/doc/uid/TP40014097-CH3-ID0&quot;&gt;The Swift Programming Language book&lt;/a&gt;&amp;nbsp;but this only appears in reference to providing a custom subscript operator for a class as opposed to using it with stock types and a brief mention in connection with Dictionaries when the key is not present is also &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html&quot;&gt;made&lt;/a&gt;, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;var dict = [&quot;foo&quot;: &quot;hello&quot;]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;dict[&quot;bar&quot;]? = &quot;world&quot;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The latter is interesting as without the &#39;?&#39; then &quot;world&quot; will be inserted along with the creation of the &quot;bar&quot; key whereas using optional chaining the key must already exist.&lt;br /&gt;
&lt;br /&gt;
Both forms can be combined, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;var dict : [String : String]?&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;dict?[&quot;bar&quot;]?= &quot;world&quot;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
which will only insert world if the key &quot;bar&quot; exists and dict exists.&lt;br /&gt;
&lt;br /&gt;
The use of optional chaining with subscript types can lead, like lots of other uses of optional chaining to more readable &amp;amp; succinct code, this is just another example.</description><link>http://petebarber.blogspot.com/2015/01/optional-chaining-with-dictionaries-in.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-5084047645759096827</guid><pubDate>Sun, 29 Jun 2014 10:48:00 +0000</pubDate><atom:updated>2014-06-29T03:50:24.312-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">&quot;XCode 6&quot;</category><category domain="http://www.blogger.com/atom/ns#">ARC</category><category domain="http://www.blogger.com/atom/ns#">CodeProject</category><category domain="http://www.blogger.com/atom/ns#">interoperability</category><category domain="http://www.blogger.com/atom/ns#">Objective-C</category><category domain="http://www.blogger.com/atom/ns#">Swift</category><category domain="http://www.blogger.com/atom/ns#">unowned</category><category domain="http://www.blogger.com/atom/ns#">weak</category><title>Resolving strong references between Swift and Objective-C classes - Using unowned and weak references from Swift to Objective-C classes</title><description>My Swift and SpriteKit exploration continues. At the moment I&#39;m writing the collision handling code.&lt;br /&gt;
&lt;br /&gt;
Rather than derive game objects from &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;SKSpriteNode&lt;/span&gt; with each derived class containing the code for handling collisions with the other types of game objects I&#39;m following something akin to a Component-Entity model.&lt;br /&gt;
&lt;br /&gt;
I have per-game-object handler classes of which an instance of each is stored in the actual &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;SKSpriteNode&#39;s&lt;/span&gt; &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;userData&lt;/span&gt; dictionary. In turn each handler instance has a reference to the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;SKSpriteNode&lt;/span&gt; that references it. Given &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-XID_52&quot;&gt;ARC&lt;/a&gt; is used this is a classical strong-reference chain which will prevent memory from being freed. The usual solution to this in Objective-C is to have one of the references be weak. In Swift there are two types of weak references: &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-XID_55&quot;&gt;weak&lt;/a&gt; which is the same as in Objective-C and &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-XID_58&quot;&gt;unowned&lt;/a&gt; (which I think is new). The difference is that an unowned reference can never be &lt;b&gt;nil&lt;/b&gt;, i.e. it&#39;s optional whether a weak pointer reference an object but an unowned pointer must always reference something. As such the member variable is always defined using &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;let&lt;/span&gt; and must be initialized, i.e. an init method is required.&lt;br /&gt;
&lt;br /&gt;
The following code shows how I was intending to implement this. There is the strong reference from node.UserData to PhysicsActions and then the unowned reference back again from PhysicsActions.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;class&lt;/span&gt; PhysicsActions&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; unowned&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;let&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;node : &lt;/span&gt;SKSpriteNode&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; init&lt;/span&gt;(associatedNode : &lt;span class=&quot;s3&quot;&gt;SKSpriteNode&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; // Store really weak (unowned) reference&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; &amp;nbsp; self&lt;/span&gt;.&lt;span class=&quot;s3&quot;&gt;node&lt;/span&gt; = associatedNode&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; func&lt;/span&gt; onContact(other : &lt;span class=&quot;s3&quot;&gt;PhysicsActions&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;// Do stuff with node&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;func&lt;/span&gt; makeNode(imageNamed name: &lt;span class=&quot;s2&quot;&gt;String&lt;/span&gt;) -&amp;gt; &lt;span class=&quot;s2&quot;&gt;SKSpriteNode&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; let&lt;/span&gt; node = &lt;span class=&quot;s2&quot;&gt;SKSpriteNode&lt;/span&gt;(imageNamed: name)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s3&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; node.&lt;/span&gt;userData&lt;span class=&quot;s3&quot;&gt; = &lt;/span&gt;NSMutableDictionary&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s3&quot;&gt;&amp;nbsp; // Store strong reference&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; node.&lt;span class=&quot;s2&quot;&gt;userData&lt;/span&gt;[&lt;span class=&quot;s4&quot;&gt;&quot;action&quot;&lt;/span&gt;] = makeActionFn(node)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; return&lt;/span&gt; node&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
However, when I went to use this code it crashed within the onContact method when it attempted to use the node. Changing this the reference type from unowned to weak fixed this, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;weak let&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;node :&amp;nbsp;&lt;/span&gt;SKSpriteNode?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This verified that the rest of the code was ok so this seemed to look like another Swift/Objective-C interoperability issue. Firstly, I made a pure Swift example which is a &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-XID_55&quot;&gt;simplified version from the The Swift Programming Language book&lt;/a&gt;.&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;class&lt;span class=&quot;s1&quot;&gt; Foo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; var&lt;/span&gt; bar : &lt;span class=&quot;s3&quot;&gt;Bar&lt;/span&gt;?&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; func&lt;/span&gt; addBar(bar: &lt;span class=&quot;s3&quot;&gt;Bar&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; self&lt;/span&gt;.&lt;span class=&quot;s3&quot;&gt;bar&lt;/span&gt; = bar&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;class&lt;span class=&quot;s1&quot;&gt; Bar&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; unowned&lt;span class=&quot;s1&quot;&gt; &lt;/span&gt;let&lt;span class=&quot;s1&quot;&gt; foo : &lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;Foo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; init&lt;/span&gt;(foo : &lt;span class=&quot;s3&quot;&gt;Foo&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; self&lt;/span&gt;.&lt;span class=&quot;s3&quot;&gt;foo&lt;/span&gt; = foo&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; func&lt;/span&gt;&amp;nbsp;f()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; println&lt;span class=&quot;s1&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&quot;foo:&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;\(&lt;/span&gt;foo&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;var&lt;/span&gt; foo : &lt;span class=&quot;s3&quot;&gt;Foo&lt;/span&gt;? = &lt;span class=&quot;s3&quot;&gt;Foo&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;var&lt;/span&gt; bar = &lt;span class=&quot;s3&quot;&gt;Bar&lt;/span&gt;(foo: &lt;span class=&quot;s3&quot;&gt;foo&lt;/span&gt;!)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo&lt;span class=&quot;s1&quot;&gt;!.&lt;/span&gt;ç&lt;span class=&quot;s1&quot;&gt;(&lt;/span&gt;bar&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar&lt;span class=&quot;s1&quot;&gt;.&lt;/span&gt;f&lt;span class=&quot;s1&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;Which works and results in:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;







&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo:C14XXXUnownedTest3Foo (has 1 child)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Ok, not a fundamental problem but let&#39;s try having an unowned reference to an Objective-C class which is just like the real case as that&#39;s what &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;SKSpriteNode&lt;/span&gt; is.&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;Foo2.h&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;@interface&lt;/span&gt; Foo2 : NSObject&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@end&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;b&gt;Foo2.m&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@implementation&lt;span class=&quot;s1&quot;&gt; Foo2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(&lt;span class=&quot;s2&quot;&gt;id&lt;/span&gt;)init&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; return&lt;span class=&quot;s1&quot;&gt; [&lt;/span&gt;super&lt;span class=&quot;s1&quot;&gt; init];&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@end&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;main.swift&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;class&lt;/span&gt; Bar2&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; unowned&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt; foo2 : &lt;span class=&quot;s2&quot;&gt;Foo2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; init&lt;/span&gt;(foo2 : &lt;span class=&quot;s2&quot;&gt;Foo2&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; &amp;nbsp; self&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;foo2&lt;/span&gt; = foo2&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; func&lt;/span&gt; f()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s3&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; println&lt;span class=&quot;s3&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&quot;foo2:&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;\(&lt;/span&gt;foo2&lt;span class=&quot;s3&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;var&lt;/span&gt; foo2 = &lt;span class=&quot;s2&quot;&gt;Foo2&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;var&lt;/span&gt; bar2 = &lt;span class=&quot;s2&quot;&gt;Bar2&lt;/span&gt;(foo2: &lt;span class=&quot;s2&quot;&gt;foo2&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar2&lt;span class=&quot;s3&quot;&gt;.&lt;/span&gt;f&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Which when f&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;oo2.f()&lt;/span&gt; is invoked results in:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;libswift_stdlib_core.dylib`_swift_abortRetainUnowned:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;0x100142420:&amp;nbsp; pushq&amp;nbsp; %rbp&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;0x100142421:&amp;nbsp; movq &amp;nbsp; %rsp, %rbp&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;0x100142424:&amp;nbsp; leaq &amp;nbsp; 0x17597(%rip), %rax &amp;nbsp; &amp;nbsp; &amp;nbsp; ; &lt;span class=&quot;s1&quot;&gt;&quot;attempted to retain deallocated object&quot;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;0x10014242b:&amp;nbsp; movq &amp;nbsp; %rax, 0x348be(%rip) &amp;nbsp; &amp;nbsp; &amp;nbsp; ; gCRAnnotations + 8&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;0x100142432:&amp;nbsp; int3&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;0x100142433:&amp;nbsp; nopw &amp;nbsp; %cs:(%rax,%rax)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Again, changing &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;unowned&lt;/span&gt;&amp;nbsp;&lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt;&amp;nbsp;foo2 :&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Foo2&lt;/span&gt;&amp;nbsp;&lt;/span&gt;to &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;weak var&lt;/span&gt;&amp;nbsp;foo2 :&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Foo2?&lt;/span&gt;&amp;nbsp;works.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;I can&#39;t explain what the actual problem is but it looks like the enhanced weak reference support (unowned) only works with pure Swift classes. If you have cyclic references to Objective-C classes from Swift then don&#39;t use unowned. In fact writing the previous sentence led me to try the following:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt; orig = &lt;span class=&quot;s2&quot;&gt;Foo2&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;unowned&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt; other = &lt;span class=&quot;s2&quot;&gt;orig&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;println&lt;span class=&quot;s3&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&quot;other:&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;\(&lt;/span&gt;other&lt;span class=&quot;s3&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;










&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;println&lt;span class=&quot;s3&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&quot;orig:&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;\(&lt;/span&gt;orig&lt;span class=&quot;s3&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s3&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s3&quot;&gt;No cyclic references, just an ordinary reference counted instance of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Foo2&lt;/span&gt;&amp;nbsp;&lt;/span&gt;(the Objective-C class) which is then assigned to an unowned reference. The final call to &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;println&lt;/span&gt; will keep the instance around until the end. This crashes as per the previous example when the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;other&lt;/span&gt; variable is accessed. Changing the type assigned to &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;orig&lt;/span&gt; from &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Foo2&lt;/span&gt; (Objective-C) to &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Foo&lt;/span&gt; (Swift) make it work.&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s3&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s3&quot;&gt;Therefore it seems unowned should not be used to refer to Objective-C classes, just Swift classes.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;</description><link>http://petebarber.blogspot.com/2014/06/using-unowned-and-weak-references-from.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-7397915913276488549</guid><pubDate>Tue, 24 Jun 2014 22:29:00 +0000</pubDate><atom:updated>2014-06-24T15:29:56.391-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">&quot;XCode 6&quot;</category><category domain="http://www.blogger.com/atom/ns#">CodeProject</category><category domain="http://www.blogger.com/atom/ns#">SKNode</category><category domain="http://www.blogger.com/atom/ns#">SpriteKit</category><category domain="http://www.blogger.com/atom/ns#">Swift</category><title>Beware: Despite the docs SKNode userData is optional</title><description>In the Swift documentation for SKNode the userData member (a dictionary) is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
userData&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
A dictionary containing arbitrary data.&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;b&gt;Declaration&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;SWIFT&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;var&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt; userData: &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/index.html#//apple_ref/swift/cl/NSMutableDictionary&quot;&gt;&lt;span class=&quot;s4&quot;&gt;NSMutableDictionary&lt;/span&gt;&lt;/a&gt;!&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p6&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p6&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;OBJECTIVE-C&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span class=&quot;s5&quot;&gt;@property&lt;/span&gt;&lt;span class=&quot;s6&quot;&gt;(&lt;/span&gt;nonatomic&lt;span class=&quot;s6&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt; &lt;/span&gt;retain&lt;span class=&quot;s6&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt; &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/index.html#//apple_ref/doc/c_ref/NSMutableDictionary&quot;&gt;&lt;span class=&quot;s4&quot;&gt;NSMutableDictionary&lt;/span&gt;&lt;span class=&quot;s7&quot;&gt; &lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;span class=&quot;s6&quot;&gt;*&lt;/span&gt;userData&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
However, in Objective-C the userData member is initially nil. Given that this is the same class then it should also be in Swift and using it, e.g.&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt; node = &lt;span class=&quot;s2&quot;&gt;SKSpriteNode&lt;/span&gt;(imageNamed: name)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;










&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;node.&lt;span class=&quot;s2&quot;&gt;userData&lt;/span&gt;[&lt;span class=&quot;s4&quot;&gt;&quot;action&quot;&lt;/span&gt;] = Action() // custom class&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
causes a crash:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;







&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;fatal error: Can&#39;t unwrap Optional.None&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
This is because the it is in fact nil despite the &#39;!&#39; following the Swift definition. This must be a documentation bug. The correct code is:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt;&amp;nbsp;node =&amp;nbsp;&lt;span class=&quot;s2&quot;&gt;SKSpriteNode&lt;/span&gt;(imageNamed: name)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;s3&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;node.&lt;/span&gt;userData&lt;span class=&quot;s3&quot;&gt;&amp;nbsp;=&amp;nbsp;&lt;/span&gt;NSMutableDictionary&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;node.&lt;span class=&quot;s2&quot;&gt;userData&lt;/span&gt;[&lt;span class=&quot;s4&quot;&gt;&quot;action&quot;&lt;/span&gt;] = Action() // custom class&lt;/span&gt;&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2014/06/beware-despite-docs-sknode-userdata-is.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-1350344076682400344</guid><pubDate>Sun, 15 Jun 2014 20:21:00 +0000</pubDate><atom:updated>2014-06-15T13:59:14.627-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Xcode &quot;Xcode 5&quot; &quot;Xcode 6&quot; &quot;Sprite Kit&quot; iOS Swift Game generated landscape portait sks GameScene.sks SKScene size CodeProject &quot;iOS 8&quot;</category><title>XCode 5 versus XCode 6 default generated Game (Sprite Kit) project and scene sizes</title><description>As a way to learn about Swift I&#39;ve been trying to write a simple game using Sprite Kit. My initial plan was to just allow a ball to be dragged around the screen and ensure it was constrained by the screen edges. This was a little harder than originally envisioned. This was partly because the default Game project generated by Xcode 6 is slightly different to that generated by Xcode 5 and so none of the existing Sprite Kit tutorials referred to this area.&lt;br /&gt;
&lt;br /&gt;
My main issue was that I could move the ball around and when the drag finished the ball was endowed with velocity so it travelled after release and despite writing some simple code to ensure if the additional travel would take it off the screen this would bound it.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;func&lt;/span&gt; boundPosToScene(newPos : &lt;span class=&quot;s2&quot;&gt;CGPoint&lt;/span&gt;) -&amp;gt; &lt;span class=&quot;s2&quot;&gt;CGPoint&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; var&lt;/span&gt; retVal&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; let&lt;/span&gt; radius = &lt;span class=&quot;s2&quot;&gt;ball&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;size&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;width&lt;/span&gt; / 2&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; if&lt;/span&gt; newPos.&lt;span class=&quot;s2&quot;&gt;x&lt;/span&gt; -&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;radius&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;lt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; retVal.&lt;span class=&quot;s2&quot;&gt;x&lt;/span&gt; =&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;radius&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; if&lt;/span&gt; newPos.&lt;span class=&quot;s2&quot;&gt;x&lt;/span&gt; +&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;radius&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;gt; &lt;span class=&quot;s1&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;size&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;width&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; retVal.&lt;span class=&quot;s2&quot;&gt;x&lt;/span&gt; = &lt;span class=&quot;s1&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;size&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;width&lt;/span&gt; -&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;radius&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; if&lt;/span&gt; newPos.&lt;span class=&quot;s2&quot;&gt;y&lt;/span&gt; -&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;radius&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;lt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; retVal.&lt;span class=&quot;s2&quot;&gt;y&lt;/span&gt; =&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;radius&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; if&lt;/span&gt; newPos.&lt;span class=&quot;s2&quot;&gt;y&lt;/span&gt; +&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;radius&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;gt; &lt;span class=&quot;s1&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;size&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;height&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; retVal.&lt;span class=&quot;s2&quot;&gt;y&lt;/span&gt; = &lt;span class=&quot;s1&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;size&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;height&lt;/span&gt; -&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;radius&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; return&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt; retVal;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Where &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ball&lt;/span&gt; is a member variable of the class containing this method that is an instance of SKSpriteNode.&lt;br /&gt;
&lt;br /&gt;
Despite this the ball kept disappearing. Reading various online articles, tutorials &amp;amp; StackOverflow posts there seemed to be an issue with Sprite Kit projects always starting up in landscape mode. When I added some debug to this statement (&amp;amp; else where), i.e.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(&lt;/span&gt;&quot;view.bounds&lt;span class=&quot;s1&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;bounds&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;, self.frame:&lt;span class=&quot;s1&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;, self.size:&lt;span class=&quot;s1&quot;&gt;\(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;&quot;&lt;span class=&quot;s1&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;I got:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;







&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;view.bounds(0.0,0.0,320.0,568.0), scene:(0.0,0.0,1024.0,768.0), scene.size:(1024.0,768.0)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
OK. The view looks good but the scene is in landscape and even if orientated to portrait it&#39;s a lot larger than the actual view. But why? There were lots of posts about the landscape issues but nothing about the non-aligned size with Xcode 5. Well, a quick test by generating projects with 5 &amp;amp; 6 (NOTE: I&#39;m new to Sprite Kit as well as Swift so I hadn&#39;t used it with 5) shows that the generated &#39;Game&#39; Xcode 5 projects programmatically creates a scene that is sized to the view:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;- (&lt;span class=&quot;s1&quot;&gt;void&lt;/span&gt;)viewDidLoad&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; [&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;viewDidLoad&lt;span class=&quot;s2&quot;&gt;];&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;// Configure the view.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span class=&quot;s3&quot;&gt;SKView&lt;/span&gt; * skView = (&lt;span class=&quot;s3&quot;&gt;SKView&lt;/span&gt; *)&lt;span class=&quot;s1&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;s3&quot;&gt;view&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; skView.&lt;span class=&quot;s3&quot;&gt;showsFPS&lt;/span&gt; = &lt;span class=&quot;s1&quot;&gt;YES&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; skView.&lt;span class=&quot;s3&quot;&gt;showsNodeCount&lt;/span&gt; = &lt;span class=&quot;s1&quot;&gt;YES&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;// Create and configure the scene.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;SKScene&lt;span class=&quot;s2&quot;&gt; * scene = [&lt;/span&gt;MyScene&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;sceneWithSize&lt;span class=&quot;s2&quot;&gt;:skView.&lt;/span&gt;bounds&lt;span class=&quot;s2&quot;&gt;.&lt;/span&gt;size&lt;span class=&quot;s2&quot;&gt;];&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; scene.&lt;/span&gt;scaleMode&lt;span class=&quot;s2&quot;&gt; = &lt;/span&gt;SKSceneScaleModeAspectFill&lt;span class=&quot;s2&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;// Present the scene.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [skView &lt;span class=&quot;s3&quot;&gt;presentScene&lt;/span&gt;:scene];&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
whereas with XCode 6 it uses an SKS file (called GameScene):&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;func&lt;/span&gt; viewDidLoad() {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; super&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;viewDidLoad&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; if&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt; scene = &lt;/span&gt;&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;GameScene&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.unarchiveFromFile(&lt;/span&gt;&lt;span class=&quot;s3&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;GameScene&quot;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;) &lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;? &lt;/span&gt;&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;GameScene&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; // Configure the view.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt; skView = &lt;span class=&quot;s1&quot;&gt;self&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;view&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;SKView&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; skView.&lt;span class=&quot;s2&quot;&gt;showsFPS&lt;/span&gt; = &lt;span class=&quot;s2&quot;&gt;true&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; skView.&lt;span class=&quot;s2&quot;&gt;showsNodeCount&lt;/span&gt; = &lt;span class=&quot;s2&quot;&gt;true&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s4&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;/* Sprite Kit applies additional optimizations to improve rendering performance */&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s4&quot;&gt;&amp;nbsp; &amp;nbsp; skView.&lt;/span&gt;ignoresSiblingOrder&lt;span class=&quot;s4&quot;&gt; = &lt;/span&gt;true&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s4&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;/* Set the scale mode to scale to fit the window */&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; scene.&lt;span class=&quot;s2&quot;&gt;scaleMode&lt;/span&gt; = .&lt;span class=&quot;s2&quot;&gt;AspectFill&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Looking at the GameScene.sks:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2Ahku4PYiEr1QVJu2ORvNGyABeLBljizLYNCvSNIayJo4uhWN8ZO0iQJdDBxU8lXJv0tsiLeOzPuQ8PmSXyGASnqh8W5LwjryXlNIC-v6tLhwgZbqWWmFmJcA_TwSkIJTV0_Ee3LItyI/s1600/SKS.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2Ahku4PYiEr1QVJu2ORvNGyABeLBljizLYNCvSNIayJo4uhWN8ZO0iQJdDBxU8lXJv0tsiLeOzPuQ8PmSXyGASnqh8W5LwjryXlNIC-v6tLhwgZbqWWmFmJcA_TwSkIJTV0_Ee3LItyI/s1600/SKS.png&quot; height=&quot;428&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
You can see that the default scene size is 1024x768, i.e. iPad in landscape mode. Changing this to 320x576 (for iPhone 4 inch) fixes the problem.&lt;br /&gt;
&lt;br /&gt;
NOTE: &lt;i&gt;When making this change make sure you explicitly save the changes. Just modifying the values and building/running the project often seems to result in them being reset. I make sure I navigate away from the Size boxes before pressing Cmd-S.&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Of course this is inflexible as it doesn&#39;t size to device. As I&#39;d like my app to run on multiple device types I&#39;m probably better of following the Xcode 5 generated code or perhaps just adding re-sizing code once the Scene has been loaded from the SKS file; as I&#39;m not sure what else that gives me.&lt;br /&gt;
&lt;br /&gt;
If you do this. In GameViewController.swift you&#39;ll also probably want to change&lt;br /&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;scene.&lt;/span&gt;scaleMode&lt;span class=&quot;s1&quot;&gt; = &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.AspectFill&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;to&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s2&quot;&gt;







&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;scene.&lt;/span&gt;scaleMode&lt;span class=&quot;s1&quot;&gt; = .&lt;/span&gt;ResizeFill&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Anyway, this difference in the generated starter apps is something to be aware of &amp;amp; the fact that the initial landscape startup issue reported against Xcode 5 seems to be present though possibly in a different guise. Certainty in the generated app from Xcode 5 it all works fine for me. When I added logging code to display the View and Scene size they were both reported as 320x568.&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2014/06/xcode-5-versus-xcode-6-default.html</link><author>noreply@blogger.com (Pete Barber)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2Ahku4PYiEr1QVJu2ORvNGyABeLBljizLYNCvSNIayJo4uhWN8ZO0iQJdDBxU8lXJv0tsiLeOzPuQ8PmSXyGASnqh8W5LwjryXlNIC-v6tLhwgZbqWWmFmJcA_TwSkIJTV0_Ee3LItyI/s72-c/SKS.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-7215543011642925990</guid><pubDate>Sat, 07 Jun 2014 16:53:00 +0000</pubDate><atom:updated>2014-08-04T15:04:50.685-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Swift Objective-C iOS &quot;iOS 8&quot; CodeProject OSX subclass designated convenience initializer super &quot;super class&quot; base &quot;base class&quot;</category><title>Subclassing Objective-C classes in Swift and the perils of Initializers</title><description>--- Begin update&lt;br /&gt;
&lt;br /&gt;
1. The post below applies to Xcode 6 Beta 2. With Beta 3 &amp;amp; 4 the relationships between the Swift and Objective-C regarding the calling of super class initializers has been formalized.&lt;br /&gt;
&lt;br /&gt;
1a. A Swift class can have multiple non-convenience initializers but in this case they must all property initialize any properties. Also, a non-convenience initializer cannot invoke another initializer, i.e. it cannot call self.init(...)&lt;br /&gt;
&lt;br /&gt;
1b. When calling an Objective-C any initializer can be called but unlike what happened in Beta 2 if the initializer invoked is not the Objective-C &#39;designated&#39; initializer and thus calls it this DOES NOT now result in a virtual like call to an initializer with that signature in the subclass, i.e. the main problem that led to the writing of this blog post.&lt;br /&gt;
&lt;br /&gt;
2. If you want to subclass an SKSpriteNote in Beta 4 the following &lt;a href=&quot;http://stackoverflow.com/questions/25009021/skspritenode-subclassing-swift&quot;&gt;StackOverflow post &lt;/a&gt;shows how.&lt;br /&gt;
&lt;br /&gt;
3. It seems that for some OS classes, in my case SpriteKit either Apple directly or some Xcode magic now provides a shim Swift wrapper of the Objective-C interface (the one for SKSpriteNode has a comment with a date of 2011 but when attempting to &quot;show in Finder&quot; nothing happens). As such all the initializers for SKSpriteNode are marked as convenience except for the &#39;designated&#39; initializer, e.g.&lt;br /&gt;
&lt;br /&gt;
class MySpriteNode : SKSpriteNode {}&lt;br /&gt;
&lt;br /&gt;
3a. This is now essentially a Swift only project with the MySpriteNode now deriving from SKSpriteNode which is a Swift class. As such the Swift rules must be followed. This means that any convenience initializers in MySpriteNode can only call non-convenience initializers for MySpriteNode and these MUST in turn call a non-convenience initializers in the superclass. In the case of SKSpriteNode there is a only a single designated initializer which is:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;/**&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; Designated Initializer&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; Initialize a sprite with a color and the specified bounds.&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; @param texture the texture to use (can be nil for colored sprite)&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; @param color the color to use for tinting the sprite.&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; @param size the size of the sprite in points&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; */&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;p2&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span class=&quot;s2&quot;&gt;init&lt;/span&gt;(texture: &lt;span class=&quot;s3&quot;&gt;SKTexture&lt;/span&gt;!, color: &lt;span class=&quot;s3&quot;&gt;UIColor&lt;/span&gt;!, size: &lt;span class=&quot;s3&quot;&gt;CGSize&lt;/span&gt;)&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
Therefore from a derived class it&#39;s impossible to use the super class convenience methods which is what Swift demands but is a shame as it often means re-implementing similar code in the derived class, as per the StackOverflow link.&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
It&#39;s not perfect by any means but it&#39;s a lot more consistent than Beta 2. I don&#39;t (yet) understand where the magic shim comes from and this is update is based on Beta 4 whereas of writing Beta 5 has just been released!&lt;/div&gt;
&lt;br /&gt;
--- End update&lt;br /&gt;
&lt;br /&gt;
As a way to learn &lt;a href=&quot;https://developer.apple.com/swift/&quot;&gt;Swift&lt;/a&gt; I decided to have a play with &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html&quot;&gt;Sprite Kit&lt;/a&gt;. One of the first things I did was to create a subclass of &lt;a href=&quot;https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKSpriteNode_Ref/Reference/Reference.html&quot;&gt;SKSpriteNode&lt;/a&gt;. This has a very handy initializer:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;init(imageNamed name: string) &lt;i&gt;(in Swift)&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(instanceType)initWithImageNamed:(NString*)name &lt;i&gt;(in Objective-C)&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I then derived from this resulting in:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;class Ball : SKSpriteNode&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; init()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; super.init(imageNamed: &quot;Ball&quot;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
and added it to my scene as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;var ball = Ball()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;ball&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt; = &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;CGPoint&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(x:&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;CGRectGetMidX&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;), y:&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;CGRectGetMidY&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;))&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;addChild&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;ball&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;Having successfully written similar code using SKSpriteKitNode directly:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;var ball = SKSpriteNode(imageNamed: &quot;Ball&quot;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;ball&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;=&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;CGPoint&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(x:&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;CGRectGetMidX&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;), y:&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;CGRectGetMidY&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;frame&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;))&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;addChild&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;ball&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;I was expecting it to work. However I received the following runtime error:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;







&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;fatal error: use of unimplemented initializer &#39;init(texture:)&#39; for class &#39;Ploing.Ball&#39;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
The odd thing was that it was complaining about the lack of initializer in the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Ball&lt;/span&gt; class. Why would calling a base class initializer result in a call back to the derived class? I wasn&#39;t sure but to work around this I decided to add the missing initializer:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;init&lt;/span&gt;(texture: &lt;span class=&quot;s2&quot;&gt;SKTexture&lt;/span&gt;!)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; &amp;nbsp; super&lt;/span&gt;.&lt;span class=&quot;s1&quot;&gt;init&lt;/span&gt;(texture: texture)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Having duly done this I then got the next error:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;fatal error: use of unimplemented initializer &#39;init(texture:color:size:)&#39; for class &#39;Ploing.Ball&#39;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Ok, same process add that one to the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Ball&lt;/span&gt; class.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;init&lt;/span&gt;(texture texture: &lt;span class=&quot;s2&quot;&gt;SKTexture&lt;/span&gt;!, color color: &lt;span class=&quot;s2&quot;&gt;UIColor&lt;/span&gt;!, size size: &lt;span class=&quot;s2&quot;&gt;CGSize&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;nbsp; &amp;nbsp; super&lt;/span&gt;.&lt;span class=&quot;s1&quot;&gt;init&lt;/span&gt;(texture: texture, color: color, size: size)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
That time it worked but I was puzzled as to why. What seems to be happening is that the derived class is calling the non-designated initializer in the super class which is then calling the designated (or another non-designated one in between). However, when this initializer is called rather than calling the super class implementation it calls one in the derived class, whether it exists or not. In the case of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Ball&lt;/span&gt; they didn&#39;t hence the error.&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
It turns out that in the middle of the the &lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_266&quot;&gt;Initialization section of the The Swift Programming Language&lt;/a&gt; there is the following:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;i&gt;&lt;b&gt;Initializer Inheritance and Overriding&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;para&quot; style=&quot;background: rgb(255, 255, 255); border: 0px; color: #414141; line-height: 20.299999237060547px; margin-bottom: 15px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;i&gt;&lt;b&gt;Unlike subclasses in Objective-C, Swift subclasses do not not inherit their superclass initializers by default. Swift’s approach prevents a situation in which a simple initializer from a superclass is automatically inherited by a more specialized subclass and is used to create a new instance of the subclass that is not fully or correctly initialized.&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;para&quot; style=&quot;background: rgb(255, 255, 255); border: 0px; color: #414141; line-height: 20.299999237060547px; margin-bottom: 15px; outline: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;i&gt;&lt;b&gt;If you want your custom subclass to present one or more of the same initializers as its superclass—perhaps to perform some customization during initialization—you can provide an overriding implementation of the same initializer within your custom subclass.&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
This explains it. Basically, in Swift, initialization methods work like virtual functions in fact super virtual functions. If a super class initializer is invoked and that in turns calls another initializer (designated or not) then it forwards the call to the derived class. The upshot of this seems to be that for any derived class in Swift it would need to re-implement all of the super classes initializers or at least any which may be invoked from the the other initializers.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Time for some experiments&lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
I therefore set out to confirm this with a simple Swift example:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;class&lt;span class=&quot;s1&quot;&gt; Foo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; var&lt;/span&gt; one: &lt;span class=&quot;s3&quot;&gt;Int&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; var&lt;/span&gt; two: &lt;span class=&quot;s3&quot;&gt;Int&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; init&lt;/span&gt;(value value:&lt;span class=&quot;s3&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;init&lt;/span&gt;(value: value, AndAnother:7)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; init&lt;/span&gt;(value value:&lt;span class=&quot;s3&quot;&gt;Int&lt;/span&gt;, AndAnother value1:&lt;span class=&quot;s3&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; one = value;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; two = value1;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; func&lt;/span&gt; print()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; println(&lt;span class=&quot;s4&quot;&gt;&quot;One:&lt;/span&gt;\(one)&lt;span class=&quot;s4&quot;&gt;, Two:&lt;/span&gt;\(two)&lt;span class=&quot;s4&quot;&gt;&quot;&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;class&lt;/span&gt; Bar : &lt;span class=&quot;s3&quot;&gt;Foo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; init&lt;span class=&quot;s1&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; super&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;init&lt;/span&gt;(value:1);&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt; bar = &lt;span class=&quot;s2&quot;&gt;Bar&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar&lt;span class=&quot;s3&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;s3&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
This wouldn&#39;t compile.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Designated initializer for &#39;Foo&#39; cannot delegate (with &#39;self.init&#39;); did you mean this to be a convenience initializer?&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Must call a designated initializer of the superclass &#39;Foo&#39;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
Whereas in Objective-C the designated initializer is effectively advisory, in Swift it&#39;s enforced. If a initializer isn&#39;t marked with the convenience &amp;nbsp;keyword then it seems (at least during compilation) all initializers are&amp;nbsp;treated as being the designated initializor and not at the same time. In this case the compilation failed as there was no designated initializer for &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Bar.init &lt;/span&gt;to call and as &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Foo.init(value value:Int)&lt;/span&gt; wasn&#39;t marked as a connivence initializer it was assumed to the designated one and is thus forbidden from calling another initializer in its own class; somewhat paradoxically.&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
These two rules prevent issues that occur with Objective-C&#39;s advisory initializer. All but the designated initializer must be prefixed with the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;convenience&lt;/span&gt; keyword. Secondly this is the only initializer permitted to call the super class initializer meaning all the the convenience initializers can only (cross) call other convenience or the designated initializer for their class. Following these rules gives a working example:&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;class&lt;span class=&quot;s1&quot;&gt; Foo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; var&lt;/span&gt; one: &lt;span class=&quot;s3&quot;&gt;Int&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; var&lt;/span&gt; two: &lt;span class=&quot;s3&quot;&gt;Int&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;b&gt;convenience&lt;/b&gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;init&lt;/span&gt;(value value:&lt;span class=&quot;s3&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; self&lt;/span&gt;.&lt;span class=&quot;s2&quot;&gt;init&lt;/span&gt;(value: value, AndAnother:7)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; init&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;(value value:&lt;span class=&quot;s2&quot;&gt;Int&lt;/span&gt;, AndAnother value1:&lt;span class=&quot;s2&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; one&lt;/span&gt; = value;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; two&lt;/span&gt; = value1;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;func&lt;/span&gt; print()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s4&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;One:&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;\(&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s4&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;, Two:&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;\(&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s4&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;class&lt;/span&gt; Bar : &lt;span class=&quot;s3&quot;&gt;Foo&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;init&lt;span class=&quot;s1&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s2&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(value: 7, AndAnother: 8)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;let&lt;/span&gt;&amp;nbsp;bar =&amp;nbsp;&lt;span class=&quot;s2&quot;&gt;Bar&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar&lt;span class=&quot;s3&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s3&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
Which gives the results:&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;One:7, Two:8&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
It also completely bypasses the convenience method of the superclass rendering it useless for calling from a derived class.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
As for explaining the original problem this doesn&#39;t help either as the new rules prevent the problem. Herein lies the clue though. It suggests the problem is not with Swift classes subclassing other Swift classes but when a Swift class subclasses an Objective-C class.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Subclassing Objective-C from Swift&lt;/h3&gt;
Time for another experiment but this time using a base class written in Objective-C:&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;b&gt;Base.h&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@interface&lt;span class=&quot;s1&quot;&gt; Base : &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;NSObject&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(&lt;span class=&quot;s3&quot;&gt;id&lt;/span&gt;)initWithValue:(&lt;span class=&quot;s2&quot;&gt;NSInteger&lt;/span&gt;) value;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(&lt;span class=&quot;s3&quot;&gt;id&lt;/span&gt;)initWithValue:(&lt;span class=&quot;s2&quot;&gt;NSInteger&lt;/span&gt;) value AndAnother:(&lt;span class=&quot;s2&quot;&gt;NSInteger&lt;/span&gt;) value1;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(&lt;span class=&quot;s3&quot;&gt;void&lt;/span&gt;)print;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@end&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;b&gt;Base.m&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;#import &lt;span class=&quot;s1&quot;&gt;&quot;Base.h&quot;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@interface&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; ()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@property&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; one;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@property&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;NSInteger&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; two;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@end&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@implementation&lt;span class=&quot;s2&quot;&gt; Base&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(&lt;span class=&quot;s4&quot;&gt;id&lt;/span&gt;)initWithValue:(&lt;span class=&quot;s3&quot;&gt;NSInteger&lt;/span&gt;) value&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p4&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;initWithValue&lt;span class=&quot;s2&quot;&gt;:value &lt;/span&gt;AndAnother&lt;span class=&quot;s2&quot;&gt;:2];&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(&lt;span class=&quot;s4&quot;&gt;id&lt;/span&gt;)initWithValue:(&lt;span class=&quot;s3&quot;&gt;NSInteger&lt;/span&gt;) value AndAnother:(&lt;span class=&quot;s3&quot;&gt;NSInteger&lt;/span&gt;) value1&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;if&lt;/span&gt; ([&lt;span class=&quot;s4&quot;&gt;super&lt;/span&gt; &lt;span class=&quot;s3&quot;&gt;init&lt;/span&gt;])&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s4&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s3&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;one&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt; = value;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s4&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s3&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;two&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt; = value1;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;return&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;self&lt;span class=&quot;s2&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(&lt;span class=&quot;s4&quot;&gt;void&lt;/span&gt;)print&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p5&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;NSLog&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;(&lt;/span&gt;@&quot;One:%d, Two:%d&quot;&lt;span class=&quot;s2&quot;&gt;, (&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;_one&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;, (&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;_two&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;@end&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;main.swift&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;class&lt;/span&gt; Baz : &lt;span class=&quot;s2&quot;&gt;Base&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s3&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; init&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(value: 7)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;init&lt;/span&gt;(value value:&lt;span class=&quot;s2&quot;&gt;Int&lt;/span&gt;, AndAnother value1:&lt;span class=&quot;s2&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(value: value, andAnother: value1);&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;var&lt;/span&gt; baz = &lt;span class=&quot;s2&quot;&gt;Baz&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;baz&lt;span class=&quot;s3&quot;&gt;.&lt;/span&gt;print&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Looking at &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Base&lt;/span&gt; it&#39;s obvious that the desginated initializer is i&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;nitWithValue:(int)value AndAnother:(int)value1&lt;/span&gt; whereas &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Baz&lt;/span&gt; is calling a &#39;convenience&#39; initializer. This compiles happily but when run gives the following error:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;fatal error: use of unimplemented initializer &#39;init(value:andAnother:)&#39; for class &#39;Test.Baz&#39;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
This is the same type of error as in the original &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;SKSpriteNote&lt;/span&gt; sample. Here, &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Baz&#39;s&lt;/span&gt; initializer is successfully invoking the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Base.initWihValue:(int) value&lt;/span&gt; but when it invokes &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Base&#39;s&lt;/span&gt; designated initializer this is when the super virtual functionality of the initializers comes into play and an attempt is made to call this non-existent method on &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Baz&lt;/span&gt;. This is easily fixed by adding that method such as it calls the super class method as in:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;class&lt;/span&gt;&amp;nbsp;Baz :&amp;nbsp;&lt;span class=&quot;s2&quot;&gt;Base&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s3&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; init&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(value: 7)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;init&lt;/span&gt;(value value:&lt;span class=&quot;s2&quot;&gt;Int&lt;/span&gt;, AndAnother value1:&lt;span class=&quot;s2&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(value: value, andAnother: value1);&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Giving the result:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;2014-06-07 17:40:18.632 Test[47394:303] One:7, Two:2&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Alternatively and staying true to the Swift way the parameterless intializer which is obviously a convenience initializer should be marked as such. This then causes a compilation failure as this is now forbidden from calling the super class initialzer and instead must make a cross call to the designated initializer giving the following code:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;class&lt;/span&gt; Baz : &lt;span class=&quot;s2&quot;&gt;Base&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s3&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; &lt;b&gt;convenience&lt;/b&gt;&lt;span class=&quot;s3&quot;&gt; &lt;/span&gt;init&lt;span class=&quot;s3&quot;&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(value: 7, AndAnother: 8)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;init&lt;/span&gt;(value value:&lt;span class=&quot;s2&quot;&gt;Int&lt;/span&gt;, AndAnother value1:&lt;span class=&quot;s2&quot;&gt;Int&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;(value: value, andAnother: value1);&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot;&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Which gives the result:&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;b&gt;2014-06-07 17:40:46.345 Test[47407:303] One:7, Two:8&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
This differs from the previous version as the connivence initializer now calls the designated one within &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Baz&lt;/span&gt; meaning the &amp;nbsp;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-(&lt;span class=&quot;s1&quot;&gt;id&lt;/span&gt;)initWithValue:(&lt;span class=&quot;s2&quot;&gt;NSInteger&lt;/span&gt;) value&lt;/span&gt; is never called which is why the second value is nolonger 2.&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
Conclusion&lt;/h3&gt;
&lt;div class=&quot;p1&quot;&gt;
Whilst this explains the behaviour I was seeing when subclassing &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;SKSpriteNode&lt;/span&gt; and by providing a mirror of its initializers without marking any as &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;convenience&lt;/span&gt; it solves the problem of using the convenient &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;init(imageNamed name: string)&lt;/span&gt; method it doesn&#39;t do so particularly elegantly nor in a fully Swift style; as it leaves the class with a set of initializers none of which are marked &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;convenience&lt;/span&gt;.&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
A designated initializer could be specified by marking all but the&amp;nbsp;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;s1&quot;&gt;init&lt;/span&gt;(texture texture: &lt;span class=&quot;s2&quot;&gt;SKTexture&lt;/span&gt;!, color color: &lt;span class=&quot;s2&quot;&gt;UIColor&lt;/span&gt;!, size size: &lt;span class=&quot;s2&quot;&gt;CGSize&lt;/span&gt;)&lt;/span&gt; method as &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;convenience&lt;/span&gt; and having them cross call this one. However this would prevent the direct calling of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;init(imageNamed name: string) &lt;/span&gt;which is a lot more&amp;nbsp;convenient&amp;nbsp;than the other methods.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
Ironically, whilst Swift formalizes the designated initializer concept and it looks like it will work well for pure Swift code it can be somewhat inconvenient when subclassing Objective-C classes.&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
P.S. For those of us who have trouble spelling the keyword &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;convenience&lt;/span&gt; is far from convenient. A keywoard to mark the designated initializer may well have been more convenient and probably easier to spell!&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2014/06/subclassing-objective-c-classes-in.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-367455629614873551</guid><pubDate>Sun, 05 Jan 2014 19:05:00 +0000</pubDate><atom:updated>2014-01-05T11:21:05.587-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">intelliJ</category><category domain="http://www.blogger.com/atom/ns#">keyboard</category><category domain="http://www.blogger.com/atom/ns#">KeyRemap4MacBook</category><category domain="http://www.blogger.com/atom/ns#">OS X</category><category domain="http://www.blogger.com/atom/ns#">remapping</category><category domain="http://www.blogger.com/atom/ns#">Windows</category><category domain="http://www.blogger.com/atom/ns#">Windows to OS X</category><title>Keyboard configuration for Windows&#39; developers on OS X (&amp; also IntelliJ)</title><description>Recently I&#39;ve been doing some ActionScript programming. Rather than target a Flash Player app. I&#39;ve been using ActionScript in combination with Adobe AIR in order to create an iOS app. This has meant I&#39;ve been spending time in OS X and using &lt;a href=&quot;http://www.jetbrains.com/idea/&quot;&gt;IntelliJ&lt;/a&gt; with the the &lt;a href=&quot;http://www.jetbrains.com/idea/features/flex_ide.html&quot;&gt;ActionScript/Flex/AIR&lt;/a&gt; plugin as my IDE.&lt;br /&gt;
&lt;br /&gt;
Most of my previous work has been done on UNIX (so command lines &amp;amp; vi) and Windows. In particular I depend on the various Windows &amp;amp; Visual Studio editor key combinations plus the Insert, Delete, Home &amp;amp; End keys. For starters this means I use a PC keyboard with the iMac rather than the Apple keyboard as it lacks these keys; I&#39;m also based in the UK so I use a British PC keyboard.&lt;br /&gt;
&lt;br /&gt;
In addition to these keys I wanted the following combos to be available across all of OS X and any apps.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Alt-Tab to cycle through apps.&lt;/li&gt;
&lt;li&gt;Ctrl-F for find.&lt;/li&gt;
&lt;li&gt;Ctrl-S for save current document (I habitually press this whilst editing).&lt;/li&gt;
&lt;li&gt;Ctrl-C &amp;amp; Ctrl-V for copy &amp;amp; paste.&lt;/li&gt;
&lt;li&gt;Ctrl-Z for undo.&lt;/li&gt;
&lt;li&gt;Obtain the correct behaviour for the &#39;\|&#39; key and the &#39;`¬&#39; key. They were swapped initially.&lt;/li&gt;
&lt;li&gt;&#39;@&#39; &amp;amp; &#39;&quot;&#39; correctly mapped.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
Additionally, I wanted these combos to be available in IntelliJ:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Ctrl-Left Arrow &amp;amp; Ctrl-Right Arrow to move the previous/next word respectively&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;Plus their selected text equivalents.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Ctrl-Home/Ctrl-End to move to the top/bottom of the document being edited.&lt;/li&gt;
&lt;/ul&gt;
This post is a description of what I installed &amp;amp; configured to allow to&lt;br /&gt;
achieve this.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Configuring a British PC keyboard&lt;/h3&gt;
The first step was to tell OS X I was using a PC keyboard, specifically a British one. This is achieved through the System Preferences-&amp;gt;Keyboard-&amp;gt;Input Sources.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixiPjiCq-C51oClPLa2y2QL91-8jY9QlM_oAYWZOIkQjHiEIacmp4nJy7VY2ynRmNZfBhNHrL872IsOwoaPE-OLTsTHfnTcVlmv07oTr4xtUTeuVxMT8nFuJtGZ9d7NlKhadO4vuopQLU/s1600/Input+Sources.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;288&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixiPjiCq-C51oClPLa2y2QL91-8jY9QlM_oAYWZOIkQjHiEIacmp4nJy7VY2ynRmNZfBhNHrL872IsOwoaPE-OLTsTHfnTcVlmv07oTr4xtUTeuVxMT8nFuJtGZ9d7NlKhadO4vuopQLU/s320/Input+Sources.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Here new input sources can be added by clicking the &#39;+&#39;. I added &#39;British - PC&#39;. Adding doesn&#39;t mean it will be used though. For this also check the &#39;Show Input menu in menu bar&#39; option. This adds a country flag and the name of the input source. Clicking on this allows the input source to be changed. If you swap between a PC keyboard and the iMac keyboard (which I do from time to time) this is an easy way to swap.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqxhRAl7Vrku5og9InFZNGg2OIEfo5FwINO8kz7qPIF05fQA1GE_KNCVU_poweA55-K29JZNzErprSJU9j7qjwcplTe_xSgspzkS8tJiE0SMpt7vYZqgaV9s_lHeQa5LV8YLfVQQW8Clg/s1600/Input+menu.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;16&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqxhRAl7Vrku5og9InFZNGg2OIEfo5FwINO8kz7qPIF05fQA1GE_KNCVU_poweA55-K29JZNzErprSJU9j7qjwcplTe_xSgspzkS8tJiE0SMpt7vYZqgaV9s_lHeQa5LV8YLfVQQW8Clg/s320/Input+menu.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
What all this gives you is the &#39;&lt;b&gt;&quot;&lt;/b&gt;&#39; and &#39;&lt;b&gt;@&lt;/b&gt;&#39; keys in the right place. Otherwise they&#39;re transposed. Note: backslash and backquote remain transposed.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Windows&#39; key combos&lt;/h3&gt;
&lt;div&gt;
The second step was obtaining the Windows&#39; key combos. This requires mapping the Windows&#39; combos to the corresponding OS X combos whilst preventing the Windows&#39; combos being interpreted as something else. After some searching it seemed like the preferred solution to this is using a 3rd party program called &lt;a href=&quot;https://pqrs.org/macosx/keyremap4macbook/&quot;&gt;KeyRemap4MacBook&lt;/a&gt;. According to various reviews it does the job well but configuring it, especially creating your own mappings is complicated. The former being down to the UI and the latter to the XML format. All these things are true but once you&#39;ve got used to it, like a lot of things it&#39;s nowhere near as daunting as it first seems; and the document is very good too. Part of the motivation for this post is to record the configuration &amp;amp; steps for my benefit should I need to do it again.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
KeyRemap4MacBook comes with a number of canned mappings. In addition to mapping across the board they can be limited to include or exclude a specific set of apps. In particular I make use of a set of pre-defined mappings from the &#39;For PC Users&#39; section which won&#39;t be applied in VMs (generally running Windows, especially useful when running Windows 8 in Parallels from the Bootcamp partition) and terminals.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
As I still use the Apply keyboard from time to time when I want to do very Apple-ly stuff I have the &#39;Don&#39;t remap &amp;nbsp;Apple&#39;s keyboards&#39; option enabled.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;
What I use&lt;/h4&gt;
&lt;div&gt;
The canned mappings I use from &#39;For PC Users&#39; section are:&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Use PC Style Copy/Paste&lt;/li&gt;
&lt;li&gt;Use PC Style Undo&lt;/li&gt;
&lt;li&gt;Use PC Style Save&lt;/li&gt;
&lt;li&gt;Use PC Style Find&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
These can easily be seen these in KeyRemap4MacBook using the &#39;show enabled only&#39; (from the many definitions) option:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlm97PMVT8rVgQlHx0znD8MgBu8YHoKbbLX27DVCvTxasvhIzO8q6IYvCCfjTTQ3RpheCvWuz_NApjKOapeu01j-QXrlYyKOaFM2j0qBwroMoLIt7E6Tkuc74o3YV5DzTUwZ0dLxGIgG4/s1600/KeyRemap4MacBook+PC.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;308&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlm97PMVT8rVgQlHx0znD8MgBu8YHoKbbLX27DVCvTxasvhIzO8q6IYvCCfjTTQ3RpheCvWuz_NApjKOapeu01j-QXrlYyKOaFM2j0qBwroMoLIt7E6Tkuc74o3YV5DzTUwZ0dLxGIgG4/s320/KeyRemap4MacBook+PC.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Without doing very little work this meets the majority of my needs. In addition to the &#39;For PC Users&#39; and &#39;General&#39; section you may also notice the three re-mappings at the start. These are custom mappings I had to create. I&#39;m not going to explain the XML format as this is available from the &lt;a href=&quot;https://pqrs.org/macosx/keyremap4macbook/xml.html.en&quot;&gt;documentation&lt;/a&gt;. Instead, here are my custom mappings.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;lt;?xml version=&lt;span class=&quot;s1&quot;&gt;&quot;1.0&quot;&lt;/span&gt;?&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;lt;root&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;appdef&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &lt;/span&gt;&amp;lt;appname&amp;gt;&lt;span class=&quot;s2&quot;&gt;INTELLIJ&lt;/span&gt;&amp;lt;/appname&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;equal&amp;gt;&lt;/span&gt;com.jetbrains.intellij&lt;span class=&quot;s3&quot;&gt;&amp;lt;/equal&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;/appdef&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;replacementdef&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &lt;/span&gt;&amp;lt;replacementname&amp;gt;&lt;span class=&quot;s2&quot;&gt;MY_IGNORE_APPS&lt;/span&gt;&amp;lt;/replacementname&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;replacementvalue&amp;gt;&lt;/span&gt;VIRTUALMACHINE, TERMINAL, REMOTEDESKTOPCONNECTION, VNC, INTELLIJ&lt;span class=&quot;s3&quot;&gt;&amp;lt;/replacementvalue&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;/replacementdef&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;replacementdef&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp; &lt;/span&gt;&amp;lt;replacementname&amp;gt;&lt;span class=&quot;s2&quot;&gt;MY_IGNORE_APPS_APPENIDX&lt;/span&gt;&amp;lt;/replacementname&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;replacementvalue&amp;gt;&lt;/span&gt;(Except in Virtual Machine, Terminal, RDC, VNC and IntelliJ)&lt;span class=&quot;s3&quot;&gt;&amp;lt;/replacementvalue&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;/replacementdef&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;item&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;name&amp;gt;&lt;/span&gt;Use PC style alt-TAB for application switching&lt;span class=&quot;s3&quot;&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;appendix&amp;gt;&lt;/span&gt;{{ MY_IGNORE_APPS_APPENIDX }}&lt;span class=&quot;s3&quot;&gt;&amp;lt;/appendix&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;identifier&amp;gt;&lt;/span&gt;private.swap_alt-tab_and_cmd-tab&lt;span class=&quot;s3&quot;&gt;&amp;lt;/identifier&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;not&amp;gt;&lt;/span&gt;{{ MY_IGNORE_APPS }}&lt;span class=&quot;s3&quot;&gt;&amp;lt;/not&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt;__KeyToKey__ KeyCode::TAB, ModifierFlag::OPTION_L, KeyCode::TAB, ModifierFlag::COMMAND_L&lt;span class=&quot;s3&quot;&gt;&amp;lt;/autogen&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;/item&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;item&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;name&amp;gt;&lt;/span&gt;Swap backslash and backquote for British PC keyboard&lt;span class=&quot;s3&quot;&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;identifier&amp;gt;&lt;/span&gt;private.swap_backslash_and_quote_for_britishpc&lt;span class=&quot;s3&quot;&gt;&amp;lt;/identifier&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt;__KeyToKey__ KeyCode::DANISH_DOLLAR, KeyCode::BACKQUOTE&lt;span class=&quot;s3&quot;&gt;&amp;lt;/autogen&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt;__KeyToKey__ KeyCode::BACKQUOTE, KeyCode::DANISH_DOLLAR&lt;span class=&quot;s3&quot;&gt;&amp;lt;/autogen&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;/item&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p3&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;item&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;name&amp;gt;&lt;/span&gt;Use PC Ctrl-Home/End to move to top/bottom of document&lt;span class=&quot;s3&quot;&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;appendix&amp;gt;&lt;/span&gt;{{ MY_IGNORE_APPS_APPENIDX }}&lt;span class=&quot;s3&quot;&gt;&amp;lt;/appendix&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;identifier&amp;gt;&lt;/span&gt;private.use_PC_ctrl-home/end&lt;span class=&quot;s3&quot;&gt;&amp;lt;/identifier&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;not&amp;gt;&lt;/span&gt;{{ MY_IGNORE_APPS }}&lt;span class=&quot;s3&quot;&gt;&amp;lt;/not&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt;__KeyToKey__ KeyCode::HOME, ModifierFlag::CONTROL_L, KeyCode::CURSOR_UP, ModifierFlag::COMMAND_L&lt;span class=&quot;s3&quot;&gt;&amp;lt;/autogen&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt;__KeyToKey__ KeyCode::HOME, ModifierFlag::CONTROL_R, KeyCode::CURSOR_UP, ModifierFlag::COMMAND_L&lt;span class=&quot;s3&quot;&gt;&amp;lt;/autogen&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt;__KeyToKey__ KeyCode::END, ModifierFlag::CONTROL_L, KeyCode::CURSOR_DOWN, ModifierFlag::COMMAND_L&lt;span class=&quot;s3&quot;&gt;&amp;lt;/autogen&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;nbsp; &lt;span class=&quot;s3&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt;__KeyToKey__ KeyCode::END, ModifierFlag::CONTROL_R, KeyCode::CURSOR_DOWN, ModifierFlag::COMMAND_L&lt;span class=&quot;s3&quot;&gt;&amp;lt;/autogen&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&amp;nbsp;&lt;/span&gt;&amp;lt;/item&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;p1&quot;&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&amp;lt;/root&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;I didn&#39;t want these mappings other than swapping backslash and backquote to be applied in various apps. i.e. VMs, VNC &amp;amp; RDC (where Windows is running anyway) and Terminal where it&amp;nbsp;&lt;/span&gt;interferes&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;with bash.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;To enable this I used the &lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;a href=&quot;https://pqrs.org/macosx/keyremap4macbook/xml.html.en#appdef&quot;&gt;&amp;lt;not&amp;gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; element giving a list of excluded apps. along with using the &lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;appendix&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; element to state this in the description.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Rather than copy the list of apps. and description I used KeyRemap4MacBook&#39;s &lt;/span&gt;&lt;a href=&quot;https://pqrs.org/macosx/keyremap4macbook/xml.html.en#replacementdef&quot; style=&quot;font-family: inherit;&quot;&gt;replacement&lt;/a&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; macro feature. There is a list of &lt;/span&gt;&lt;a href=&quot;https://github.com/tekezo/KeyRemap4MacBook/blob/version_9.2.0/src/core/server/Resources/appdef.xml&quot; style=&quot;font-family: inherit;&quot;&gt;builtin apps.&lt;/a&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; that can be referred too but I also looked at the &lt;/span&gt;&lt;a href=&quot;https://github.com/tekezo/KeyRemap4MacBook/blob/version_9.2.0/src/core/server/Resources/include/checkbox/for_pc_users.xml&quot; style=&quot;font-family: inherit;&quot;&gt;XML file&lt;/a&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt; from the source that contains the &#39;For PC Users&#39; mapping.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The _L &amp;amp; _R refer to keys which appear twice: on the left &amp;amp; right side of the keyboard.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The format allows multiple mappings to be grouped. These don&#39;t have to be similar but this the intention, i.e. all the ctrl-home/end mappings are together. Each &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt; entry is a separate mapping but they are enabled/disabled collectively.&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The format isn&#39;t too bad. The weird thing from an XML perspective is the&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;autogen&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;element. This is source combo followed by combo to generate instead&amp;nbsp;&lt;/span&gt;separated&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;by a comma. I think it would be easier to understand if this element were broken down into child elements with say&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;to&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;and&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;from&amp;gt;&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;elements.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This private.xml is also available as a&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://gist.github.com/petebarber/8269903&quot; style=&quot;font-family: inherit;&quot;&gt;GIST&lt;/a&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;h3&gt;
IntelliJ&lt;/h3&gt;
&lt;div class=&quot;p1&quot;&gt;
IntelliJ complicates things slightly as it provides its own key-mapping functionality similar to that of KeyRemap4MacBook but solely for itself. This means that there can be a conflict with KeyRemap4MacBook.&lt;br /&gt;
&lt;br /&gt;
I&#39;m writing this a while after I originally implemented it. In fact part of the reason I&#39;m writing this post at all is so I have a record of what&#39;s required. Since getting this working it looks like I&#39;ve changed my IntelliJ Keymap (from Preferences). Originally it was set to &#39;Mac OS X&#39; but is now set to &#39;Default&#39;.&lt;br /&gt;
&lt;br /&gt;
When is was set to &#39;Mac OS X&#39; the KeyRemap4MacBook mappings worked well except that Ctrl-Home/End wouldn&#39;t work. This is because that combination is mapped to something else. Additionally the &#39;Mac OS X&#39; mappings don&#39;t provide support for Ctrl-Left/Right-Arrow for hoping back and forth over words. My initial solution to this was to modify (by taking a copy) the &#39;Mac OS X&#39; mapping:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Change &#39;Move Caret to Next Word&#39; from &#39;alt -&amp;gt;&#39; to &#39;ctrl-&amp;gt;&#39;.&lt;/li&gt;
&lt;li&gt;Change &#39;Move Caret to Previous Word&#39; from &#39;alt &amp;lt;-&#39; to &#39;ctrl &amp;lt;-&#39;.&lt;/li&gt;
&lt;li&gt;Change &#39;Move Caret to Next Word with Selection&#39; from &#39;alt shift -&amp;gt;&#39; to &#39;ctrl shift -&amp;gt;&#39;.&lt;/li&gt;
&lt;li&gt;Change &#39;Move Caret to Previous Word with Selection&#39; from &#39;alt shift &amp;lt;-&#39; to &#39;ctrl shift &amp;lt;-&#39;.&lt;/li&gt;
&lt;li&gt;Change &#39;Move Caret to Text End&#39; from &#39;cmd end&#39; to &#39;ctrl end&#39;.&lt;/li&gt;
&lt;li&gt;Change &#39;Move Caret to Text Start&#39; from &#39;cmd home&#39; to &#39;ctrl home&#39;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
However, it seems that the &#39;Default&#39; key mappings are as per-Windows but when KeyRemap4MacBook is running they all conflict. In fact I may have missed this completely when initially figuring this out.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Therefore the far easier solution is to select the &#39;Default&#39; IntelliJ mapping and using KeyRemap4MacBook make it &lt;a href=&quot;https://pqrs.org/macosx/keyremap4macbook/xml.html.en#appdef&quot;&gt;aware&lt;/a&gt; of IntelliJ and exclude it from key re-mapping as per the other applications. This is the purpose of the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;appdef&lt;/span&gt; section in &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;private.xml&lt;/span&gt;. KeyRemap4MacBook doesn&#39;t need definitions for other excluded apps. as these are built-in.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
The mappings are not perfect. IntelliJ is great but this is now down to IntelliJ&#39;s mapping and having excluded it from KeyRemap4MacBook mapping. I still miss Ctrl-Left/Right-Arrow and Ctrl-Home/End in other apps. but hopefully this should just be a case of defining more mappings and the Ctrl-Z (undo) mapping effectiveness seems to vary.&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://petebarber.blogspot.com/2014/01/keyboard-configuration-for-windows.html</link><author>noreply@blogger.com (Pete Barber)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixiPjiCq-C51oClPLa2y2QL91-8jY9QlM_oAYWZOIkQjHiEIacmp4nJy7VY2ynRmNZfBhNHrL872IsOwoaPE-OLTsTHfnTcVlmv07oTr4xtUTeuVxMT8nFuJtGZ9d7NlKhadO4vuopQLU/s72-c/Input+Sources.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-2188755581217202395</guid><pubDate>Mon, 30 Dec 2013 13:25:00 +0000</pubDate><atom:updated>2013-12-30T05:42:38.601-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">7</category><category domain="http://www.blogger.com/atom/ns#">actionscript</category><category domain="http://www.blogger.com/atom/ns#">adobe</category><category domain="http://www.blogger.com/atom/ns#">air</category><category domain="http://www.blogger.com/atom/ns#">app icon</category><category domain="http://www.blogger.com/atom/ns#">flex</category><category domain="http://www.blogger.com/atom/ns#">flex sdk</category><category domain="http://www.blogger.com/atom/ns#">icon</category><category domain="http://www.blogger.com/atom/ns#">intelliJ</category><category domain="http://www.blogger.com/atom/ns#">ios 7</category><category domain="http://www.blogger.com/atom/ns#">sdk iOS</category><title>Using IntelliJ, Adobe ActionScript and AIR SDK to create &amp; package iOS 7 apps.</title><description>Just a quick post. Lately I&#39;ve been learning ActionScript. Having seen how easy it is to get an ActionScript project for Flash Player running on Android using &lt;a href=&quot;https://get.adobe.com/air/&quot;&gt;Adobe AIR&lt;/a&gt; I wanted to do the same for my iPhone. Getting stuff running on the AIR emulator and on the iOS simulator (under OS X) and AIR was pretty easy. In my case this was using IntelliJ as the IDE (rather than Flash Builder) coupled with Flex 4.6 SDK. The real fun started when I started to package my application for submission to the App Store, in particular creating the App icons.&lt;br /&gt;
&lt;br /&gt;
The version of the AIR SDK that comes with the Flex 4.6 SDK is 3.1. However this isn&#39;t aware of the new iOS 7 App icons. It would seem a simple matter of adding additional entries to the &lt;a href=&quot;http://help.adobe.com/en_US/air/build/WS5b3ccc516d4fbf351e63e3d118666ade46-7ff1.html&quot;&gt;Application Descriptor&lt;/a&gt;&amp;nbsp;file, i.e. to support the the 152x152 icon just add&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;image152x152&amp;gt;icon152.png&amp;lt;/image152x152&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
to the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;icon&amp;gt;&lt;/span&gt; section. Unfortunately the schema knows this isn&#39;t valid (well doesn&#39;t know about) and you end up with the following error:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;error 103: application.icon.image152x152 is an unexpected element/attribute&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
To fix, the first step is to download &amp;amp; install the latest version AIR SDK which is 3.9 (4.0 beta aside). This does not mean download &amp;amp; install the latest version of the Flex SDK as this contains an older version of the AIR SDK. Also, as this needs installing on top of the one present in the existing Flex SDK installation do not download the installer version, instead use the zip (Windows) or tbz2 (OS X). The following link takes you to both:&amp;nbsp;&lt;a href=&quot;http://www.adobe.com/devnet/air/air-sdk-download.html&quot;&gt;http://www.adobe.com/devnet/air/air-sdk-download.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Then extract these within the Flex SDK (you might want to take a copy of this first but if things go wrong you can always re-download it). The easiest way is to just copy/move the archive to the Flex SDK directory and extract the files there which will overwrite the existing ones.&lt;br /&gt;
&lt;br /&gt;
NOTE: &lt;i&gt;Up to this point the same thing occurred on both Windows &amp;amp; OS X. The following steps only worked on OS X. In particular updating the scheme in the Application Descriptor didn&#39;t work and when reverted back to 3.1 (&amp;amp; support for iOS 7 App Icons removed) then packaging the app. was a problem as the AIR SDK seemed to be missing various binaries to create the ARM binaries. I haven&#39;t pursued this further as I was working on OS X at this point.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
In theory everything should work now. However if you proceed to package the app. it will still give the same 103 error. This is because the scheme version number in the Application Descriptor needs updating. Most likely the line will be:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;lt;application xmlns=&quot;http://ns.adobe.com/air/application/3.1&quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;3.1&amp;nbsp;&lt;/span&gt;needs changing to &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;3.9&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
This may not fix the problem though. If you&#39;re using IntelliJ (sorry don&#39;t know about Flash Builder) and have selected the &#39;Generated&#39; option for the Application Descriptor then it appears by default IntelliJ (AIR?) creates this with a version of 3.1. In this case you&#39;ll need to stop using this option. Instead choose the &#39;Custom template&#39; and either create your own or have IntelliJ (AIR?) generate one for you. If you choose the latter option then IntelliJ offers a drop down to specify the version. However, it only lists 3.1 to 3.8. Therefore this will need manually changing to 3.9.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAfiW9zCw5BUw-HZMHgTFNqvvSyzDAm5P8moZBR9Tx6n4M-Wq0y6xBcDvh5EMulewpx17RCrDQKwiuXHC43dPCs_CRCAHRF4Fq4vNQ3xdCvyEIQthZ6rRyM4jm59PkRvENcndKkqe2UmM/s1600/AD.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;168&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAfiW9zCw5BUw-HZMHgTFNqvvSyzDAm5P8moZBR9Tx6n4M-Wq0y6xBcDvh5EMulewpx17RCrDQKwiuXHC43dPCs_CRCAHRF4Fq4vNQ3xdCvyEIQthZ6rRyM4jm59PkRvENcndKkqe2UmM/s320/AD.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
At this point it should be possible to successfully package an iOS app with iOS 7 App Icon support.&lt;br /&gt;
&lt;br /&gt;</description><link>http://petebarber.blogspot.com/2013/12/using-intellij-adobe-actionscript-and.html</link><author>noreply@blogger.com (Pete Barber)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAfiW9zCw5BUw-HZMHgTFNqvvSyzDAm5P8moZBR9Tx6n4M-Wq0y6xBcDvh5EMulewpx17RCrDQKwiuXHC43dPCs_CRCAHRF4Fq4vNQ3xdCvyEIQthZ6rRyM4jm59PkRvENcndKkqe2UmM/s72-c/AD.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-7930369573173451709</guid><pubDate>Wed, 14 Aug 2013 22:33:00 +0000</pubDate><atom:updated>2013-08-14T15:33:46.751-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C++</category><category domain="http://www.blogger.com/atom/ns#">C++11</category><category domain="http://www.blogger.com/atom/ns#">capture</category><category domain="http://www.blogger.com/atom/ns#">lambda</category><category domain="http://www.blogger.com/atom/ns#">lambda capture</category><category domain="http://www.blogger.com/atom/ns#">lvalue</category><category domain="http://www.blogger.com/atom/ns#">lvalue reference</category><category domain="http://www.blogger.com/atom/ns#">reference</category><title>Capturing lvalue references in C++11 lambdas</title><description>Recently the question &quot;&lt;b&gt;what is the type of an lvalue reference when captured by reference in a C++11 lambda?&lt;/b&gt;&quot; was asked. It turns out that it&#39;s a reference to whatever the original reference was too. This is just like taking a reference to an existing reference, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int foo = 7;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int&amp;amp; rfoo = foo;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int&amp;amp; rfoo1 = rfoo;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int&amp;amp; rfoo2 = rfoo1;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
All references refer to foo rather than rfoo2-&amp;gt;rfoo1-&amp;gt;rfoo-&amp;gt;foo meaning the following code&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;std::cout &amp;lt;&amp;lt; &quot;foo:&quot; &amp;lt;&amp;lt; foo &amp;lt;&amp;lt; &quot;, rfoo:&quot; &amp;lt;&amp;lt; rfoo&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &quot;, rfoo1:&quot; &amp;lt;&amp;lt; rfoo1 &amp;lt;&amp;lt; &quot;, rfoo2:&quot; &amp;lt;&amp;lt; rfoo2&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;++foo;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;std::cout &amp;lt;&amp;lt; &quot;foo:&quot; &amp;lt;&amp;lt; foo &amp;lt;&amp;lt; &quot;, rfoo:&quot; &amp;lt;&amp;lt; rfoo&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &quot;, rfoo1:&quot; &amp;lt;&amp;lt; rfoo1 &amp;lt;&amp;lt; &quot;, rfoo2:&quot; &amp;lt;&amp;lt; rfoo2&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;std::cout &amp;lt;&amp;lt; &quot;&amp;amp;foo:&quot; &amp;lt;&amp;lt; &amp;amp;foo &amp;lt;&amp;lt; &quot;, &amp;amp;rfoo:&quot; &amp;lt;&amp;lt; &amp;amp;rfoo&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &quot;, &amp;amp;rfoo1:&quot; &amp;lt;&amp;lt; &amp;amp;rfoo1 &amp;lt;&amp;lt; &quot;, &amp;amp;rfoo2:&quot; &amp;lt;&amp;lt; &amp;amp;rfoo2&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Which gives:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo:7, rfoo:7, rfoo1:7, rfoo2:7&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo:8, rfoo:8, rfoo1:8, rfoo2:8&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;amp;foo:00D3FB0C, &amp;amp;rfoo:00D3FB0C, &amp;amp;rfoo1:00D3FB0C, &amp;amp;rfoo2:00D3FB0C&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;I.e. all the references are aliases for the original foo hence the same value is displayed including when the original is modified and that the address of each variable is the same, that of &lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
There is nothing surprising here it&#39;s just basic C++ but it&#39;s along time since I&#39;ve thought about it which is why with lambdas, l-value, r-value and universal references I sometimes I do a double take on what was once obvious.&lt;br /&gt;
&lt;br /&gt;
The same happens with lambda capture but it&#39;s a slightly more interesting story. Take the following example:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int foo = 99;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int&amp;amp; rfoo = foo;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int&amp;amp; rfoo1 = foo;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;std::cout &amp;lt;&amp;lt; &quot;foo:&quot; &amp;lt;&amp;lt; foo &amp;lt;&amp;lt; &quot;, rfoo:&quot; &amp;lt;&amp;lt; rfoo&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &quot;, rfoo1:&quot; &amp;lt;&amp;lt; rfoo1&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;std::cout &amp;lt;&amp;lt; &quot;&amp;amp;foo:&quot; &amp;lt;&amp;lt; &amp;amp;foo &amp;lt;&amp;lt; &quot;, &amp;amp;rfoo:&quot; &amp;lt;&amp;lt; &amp;amp;rfoo&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &quot;, &amp;amp;rfoo1:&quot; &amp;lt;&amp;lt; &amp;amp;rfoo1&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;auto l = [foo, rfoo, &amp;amp;rfoo1]()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; std::cout &amp;lt;&amp;lt; &quot;foo:&quot; &amp;lt;&amp;lt; foo &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; std::cout &amp;lt;&amp;lt; &quot;rfoo:&quot; &amp;lt;&amp;lt; rfoo &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; std::cout &amp;lt;&amp;lt; &quot;rfoo1:&quot; &amp;lt;&amp;lt; rfoo1 &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; std::cout &amp;lt;&amp;lt; &quot;&amp;amp;foo:&quot; &amp;lt;&amp;lt; &amp;amp;foo &amp;lt;&amp;lt; &quot;, &amp;amp;rfoo:&quot;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &amp;amp;rfoo &amp;lt;&amp;lt; &quot;, &amp;amp;rfoo1:&quot; &amp;lt;&amp;lt; &amp;amp;rfoo1&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;};&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo = 100;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;l();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Which gives:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo:99, rfoo:99, rfoo1:99&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;amp;foo:00D3FB0C, &amp;amp;rfoo:00D3FB0C, &amp;amp;rfoo1:00D3FB0C&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo:99&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo:99&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo1:100&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;amp;foo:00D3FAE0, &amp;amp;rfoo:00D3FAE4, &amp;amp;rfoo1:00D3FB0C&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
To begin with it behaves as per the first example in that foo, &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo &lt;/span&gt;and &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo1 &lt;/span&gt;all give the same value as &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo &lt;/span&gt;and &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo1 &lt;/span&gt;are effectively aliases for &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo &lt;/span&gt;as shown when displaying their addresses; they&#39;re all the same.&lt;br /&gt;
&lt;br /&gt;
However, when these same variables are captured it&#39;s a different story: The capture of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo&lt;/span&gt;&amp;nbsp;is of no surprise as this is by-value so displays the captured value of 99 despite the original &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo &lt;/span&gt;being changed to 100 prior to the lambda being invoked. Its address is that of a new variable; a member of the lambda.&lt;br /&gt;
&lt;br /&gt;
It starts to get interesting with the capture of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo&lt;/span&gt;. When the lambda is invoked this too displays 99, the original captured value. Also, its address is not that of the original &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo&lt;/span&gt;. It seems that the reference itself has not been captured but rather what it refers too, in this case an &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int &lt;/span&gt;with the value of 99. It appears to have been magically dereferenced as part of the capture.&lt;br /&gt;
&lt;br /&gt;
This is the correct behaviour and when thought about becomes somewhat obvious. It&#39;s just like assigning a variable from a reference, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int foo = 7;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int&amp;amp; rfoo = foo;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int bar = rfoo;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar&lt;/span&gt; doesn&#39;t become an &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int&amp;amp;&lt;/span&gt; and &amp;nbsp;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo&lt;/span&gt; is magically dereferenced except in this scenario there is nothing magical at all, it&#39;s as expected. If &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;int&lt;/span&gt; were replaced with &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;auto&lt;/span&gt;, e.g.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;auto bar = rfoo;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
then it would be expected that &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar&lt;/span&gt; is an int as &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;auto&lt;/span&gt; strips of CV and reference qualifiers.&lt;br /&gt;
&lt;br /&gt;
Finally, there is &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo1&lt;/span&gt;. This too is odd as it is attempting to take a reference to a reference. As seen in the first example this is perfectly fine. The end effect is that there can&#39;t be a reference to reference and so on and all are aliases of the original variable.&lt;br /&gt;
&lt;br /&gt;
This is pretty much what&#39;s happening here. It&#39;s irrelevant that the target of the capture is a reference. In the end the capture by reference is capture by reference of the underlying variable, i.e. what &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo1&lt;/span&gt; refers too, in this case &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo&lt;/span&gt; not &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo1&lt;/span&gt; itself. This is demonstrated twofold by &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo1&lt;/span&gt; within the lambda displaying the updated value of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo&lt;/span&gt; and also that the address of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rfoo1&lt;/span&gt; within the lambda is that of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;foo&lt;/span&gt; outside it.&lt;br /&gt;
&lt;br /&gt;
This is as per the standard section 5.1.2 Lambda expression sub-note 14:&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;An entity is captured by copy if it is implicitly captured and the capture-default is = or if it is explicitly&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;captured with a capture that does not include an &amp;amp;. For each entity captured by copy, an unnamed nonstatic&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;data member is declared in the closure type. The declaration order of these members is unspecified.&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;b&gt;The type of such a data member is the type of the corresponding captured entity if the entity is not a&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;b&gt;reference to an object, or the referenced type otherwise&lt;/b&gt;. [ Note: If the captured entity is a reference to a&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;function, the corresponding data member is also a reference to a function. —end note ]&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
The sentence in bold states that for a reference captured by value then the type of the captured value is the type referred to, i.e. the reference aspect as been removed the crucial part being &quot;or the referenced type otherwise&quot;. (NOTE: I haven&#39;t experimented with references to functions).&lt;br /&gt;
&lt;br /&gt;
Finally, a vivid example showing that a reference captured by value involves a dereference.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;class Bar&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;private:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int mValue;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;public:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Bar(const Bar&amp;amp;) : mValue(9999)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;public:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Bar(const int value) : mValue(value) {}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int GetValue() const { return mValue; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;void SetValue(const int value) { mValue = value; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;};&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Bar bar(1);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Bar&amp;amp; rbar = bar;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Bar&amp;amp; rbar1 = bar;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;std::cout &amp;lt;&amp;lt; &quot;&amp;amp;bar:&quot; &amp;lt;&amp;lt; &amp;amp;bar &amp;lt;&amp;lt; &quot;, &amp;amp;rbar:&quot; &amp;lt;&amp;lt; &amp;amp;rbar&amp;lt;&amp;lt; &quot;, &amp;amp;rbar1:&quot; &amp;lt;&amp;lt; &amp;amp;rbar1 &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;auto l2 = [bar, rbar, &amp;amp;rbar1]()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;std::cout &amp;lt;&amp;lt; &quot;bar:&quot; &amp;lt;&amp;lt; bar.GetValue() &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;std::cout &amp;lt;&amp;lt; &quot;rbar:&quot; &amp;lt;&amp;lt; rbar.GetValue() &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;std::cout &amp;lt;&amp;lt; &quot;rbar1:&quot; &amp;lt;&amp;lt; rbar1.GetValue() &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;std::cout &amp;lt;&amp;lt; &quot;&amp;amp;bar:&quot; &amp;lt;&amp;lt; &amp;amp;bar &amp;lt;&amp;lt; &quot;, &amp;amp;rbar:&quot; &amp;lt;&amp;lt; &amp;amp;rbar&amp;lt;&amp;lt; &quot;, &amp;amp;rbar1:&quot; &amp;lt;&amp;lt; &amp;amp;rbar1 &amp;lt;&amp;lt; &#39;\n&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;};&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar.SetValue(2);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;l2();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
The class bar provides a crude copy-constructor that sets the stored value to 9999. The following output is similar to that in the previous example in that the addresses of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar&lt;/span&gt; and &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rbar&lt;/span&gt; in the lambda differ from that of bar showing they&#39;re copies whilst &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rbar1&lt;/span&gt; is the same. Secondly, the value of &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;mValue&lt;/span&gt; stored within &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Bar&lt;/span&gt; is shown as 9999 for the first two captured variables meaning they were copy-constructed.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;amp;bar:00D3FB0C, &amp;amp;rbar:00D3FB0C, &amp;amp;rbar1:00D3FB0C&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;bar:9999&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rbar:9999&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;rbar1:2&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;amp;bar:00D3FAE0, &amp;amp;rbar:00D3FAE4, &amp;amp;rbar1:00D3FB0C&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
Making the copy-construct private (by commenting out the seemingly unnecessary &#39;public:&#39;) prevents compilation.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;1&amp;gt;------ Build started: Project: References, Configuration: Debug Win32 ------&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;1&amp;gt; &amp;nbsp;main.cpp&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;1&amp;gt;c:\users\pete\desktop\references\references\main.cpp(85): error C2248: &#39;Bar::Bar&#39; : cannot access private member declared in class &#39;Bar&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;1&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c:\users\pete\desktop\references\references\main.cpp(59) : see declaration of &#39;Bar::Bar&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;1&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c:\users\pete\desktop\references\references\main.cpp(54) : see declaration of &#39;Bar&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;1&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c:\users\pete\desktop\references\references\main.cpp(59) : see declaration of &#39;Bar::Bar&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;1&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c:\users\pete\desktop\references\references\main.cpp(54) : see declaration of &#39;Bar&#39;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Writing this post has clarified the situation for me, I hope it helps you as well.&lt;br /&gt;
&lt;br /&gt;
The sample code is available &lt;a href=&quot;https://gist.github.com/petebarber/6236315&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;</description><link>http://petebarber.blogspot.com/2013/08/capturing-lvalue-references-in-c11.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-9156213616650872113</guid><pubDate>Wed, 21 Nov 2012 08:44:00 +0000</pubDate><atom:updated>2013-12-30T05:31:00.257-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Windows 8 iMac 21.5 Boot Camp</category><title>Windows 8 Pro on an early 2009 iMac 21.5 (Core 2 Duo)</title><description>A couple of weeks back I thought I&#39;d have a go writing a Windows Store App. &amp;nbsp;To do this requires Windows 8. &amp;nbsp;At the time I was running Windows 7 Home Premium on an early 2009 iMac 21.5 (Core 2 Duo). &amp;nbsp;This had been installed using Boot Camp including install Boot Camp assistant and the drivers supplied by Apple.&lt;br /&gt;
&lt;br /&gt;
To upgrade to Windows 8 I wanted to avoid a re-installation of all my apps. and data etc so I went with an in place upgrade. &amp;nbsp;This all seemed to work properly and soon I was running Windows 8 and could access the Windows Store App templates from Visual Studio. &amp;nbsp;However, soon after Windows 8 kept crashing, well freezing. &amp;nbsp;It got to the point that after every reboot I&#39;d be lucky to get 5 minutes of up time between each freeze.&lt;br /&gt;
&lt;br /&gt;
Given that Apple haven&#39;t provided Windows 8 drivers yet this wasn&#39;t exactly a surprise. &amp;nbsp;I decided to try and work around this by rebooting to OS X and using VMWare Fusion to access the Boot Camp partition. &amp;nbsp;Whilst rebooting in OS X I managed to corrupt the Windows installation. &amp;nbsp;I use a non-Apple wireless keyboard (as I need the insert, delete, home &amp;amp; end plus the easily accessible cursor keys for VS development) so holding down Alt to select the OS to boot into didn&#39;t work. &amp;nbsp;When I realized it was going back into Windows I just turned the machine off. &amp;nbsp;After a couple of times the Windows installation was toast! &amp;nbsp;To get back to the point of trying Fusion I had to do a fresh Windows install. &amp;nbsp;In this case installing a minimal Windows 7 installation: just enough to allow the download of Windows 8. &amp;nbsp;I then installed Windows 8 using the preserve nothing option.&lt;br /&gt;
&lt;br /&gt;
Having now gone through the steps I wanted to avoid I decided to give the new installation a go via direct boot, i.e. no Fusion. &amp;nbsp;That was two weeks ago. &amp;nbsp;Since then I&#39;ve re-installed all the apps. and my personal data and (fingers crossed) haven&#39;t had a single crash. &amp;nbsp;As the freezes were usually happening during some graphical operation e.g.&amp;nbsp;a status bar updating I assumed the fault probably lay with the video drivers. &amp;nbsp;I didn&#39;t install Boot Camp assistant and in particular the Windows 7 drivers from OS X disc. &amp;nbsp;Well, I did install one. &amp;nbsp;After a while I noticed I wasn&#39;t getting any sound even though all the audio drivers and hardware claimed they were happy. &amp;nbsp;Eventually I installed by the Cirrus Logic driver which made the speakers work. I haven&#39;t gone anywhere near the NVIDIA drivers.&lt;br /&gt;
&lt;br /&gt;
So, the whole point of this post is for those who run Windows via Boot Camp on early iMacs and want to run Windows 8 then perhaps a fresh install (or maybe uninstall the Boot Camp supplied drivers prior to upgrade) is probably the way to go.</description><link>http://petebarber.blogspot.com/2012/11/windows-8-pro-on-early-2009-imac-215.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-6248346524235711788</guid><pubDate>Fri, 03 Aug 2012 11:49:00 +0000</pubDate><atom:updated>2012-08-03T04:57:10.264-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Entity Framework DbContext SQLServerCE C# NUnit CodeProject SqlCeConnectionFactory databaseDirectory</category><title>Specifying the directory to create SQL CE databases when using Entity Framework</title><description>In the last few &lt;a href=&quot;http://petebarber.blogspot.co.uk/2012/08/integration-testing-with-nunit-and.html&quot;&gt;posts&lt;/a&gt; I&#39;ve been describing how to create instances of SQLCE in order to perform automated Integration Testing using NUnit and accessing the dB using Entity Framework. &amp;nbsp;I covered creating the dB using both Entity Framework and the SQL CE classes. &amp;nbsp;In particular I wanted control over the directory the dB was created in but I didn&#39;t want to tie to a specific location rather let it use the current working directory.&lt;br /&gt;
&lt;br /&gt;
Using the Entity Framework&#39;s &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v=vs.103).aspx&quot;&gt;DbContext &lt;/a&gt;constructor that takes the name of a connection string or database name it&#39;s suddenly very easy to end up NOT creating the dB you expected where you expected it to be. &amp;nbsp;This post shows how to avoid these. &amp;nbsp;Generally speaking the use of the DbContext constructor that takes a &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/cc716756.aspx&quot;&gt;Connection String&lt;/a&gt;&amp;nbsp;should be avoided unless the name of a connection string from the .config file is being specified.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example 1 - Using the SqlCeEngine class&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  const string DB_NAME = &quot;test1.sdf&quot;;  
2:  const string DB_PATH = @&quot;.\&quot; + DB_NAME; // Use &quot;.\&quot; for CWD or a specific path  
3:  const string CONNECTION_STRING = &quot;data source=&quot; + DB_PATH;  
4:    
5:  using (var eng = new SqlCeEngine(CONNECTION_STRING))  
6:  {  
7:    eng.CreateDatabase();  
8:  }  
9:    
10:  using (var conn = new SqlCeConnection(CONNECTION_STRING))  
11:  {  
12:    conn.Open(); // do stuff with db...  
13:  }  
14:    
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The important thing to note is that the constructor for SqlCeEngine that takes an argument requires a Connection String, i.e. a string containing the &quot;data source=...&quot;. &amp;nbsp;Just&amp;nbsp;specifying&amp;nbsp;the dB path is not sufficient. &amp;nbsp;To specify a specific directory &amp;nbsp;include the absolute or relative path. &amp;nbsp;To specify the current working directory, e.g. bin\debug then just use &quot;.\&quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example 2 - Using DbContext (doesn&#39;t work)&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  using (var ctx = new DbContext(&quot;test2.sdf&quot;))  
2:  {  
3:    ctx.Database.Create();  
4:  }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This code appears to work but doesn&#39;t create an instance of an SQL CE dB as desired. &amp;nbsp;Instead it creates a &lt;a href=&quot;http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx&quot;&gt;localDB&lt;/a&gt; instance in the user&#39;s home directory. &amp;nbsp;In my case: C:\Users\Pete\._test.sdf.mdf (&amp;amp; corresponding log file). &amp;nbsp;This is not really surprising as Entity Framework had no way of knowing that a SQL CE dB should be created.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example 3 - Using DbContext (does work)&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  Database.DefaultConnectionFactory =  
2:    new SqlCeConnectionFactory(  
3:      &quot;System.Data.SqlServerCe.4.0&quot;,  
4:      @&quot;.\&quot;, &quot;&quot;);  
5:    
6:  using (var ctx = new DbContext(&quot;test2.sdf&quot;))  
7:  {  
8:    ctx.Database.Create();  
9:    // do stuff with ctx...  
10:  }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The difference between the last and this example is changing the default type of dB that EF should create. &amp;nbsp;As shown this is done by installing a different factory.&lt;br /&gt;
&lt;br /&gt;
The 3rd parameter to SqlCeConnectionFactory is the directory that the dB should be created in. &amp;nbsp;Just like the first example specifying &quot;.\&quot; means the current working directory and specifying an absolute path to a directory will lead to them being created there.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;NOTE: As per the post &lt;a href=&quot;http://petebarber.blogspot.co.uk/2012/08/integration-testing-with-nunit-and.html&quot;&gt;Integration Testing with NUnit and Entity Framework&lt;/a&gt;&amp;nbsp;be aware that creating a dB using the Entity Framework results in the additional table &#39;_MigrationHistory&#39; being created which EF uses to keep the model and dB synchronized.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;NOTE1: Whereas SqlCeEngine is a SQL CE class from the System.Data.SqlServerCe assembly, SqlCeConnectionFactory appears to be part of the System.Data.Entity assembly which is part of the Entity Framework.&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;
In the above example the string passed to DbContext can be a name (of a connection string from the .config file) or a connection string. &amp;nbsp;In this case passing the name of the db, i.e. test2.sdf is equivalent to passing &quot;data source=test2.sdf&quot;, well more or less. &amp;nbsp;If the &#39;.sdf&#39; suffix is omitted with &quot;data source&quot; then the resultant dB is called test2 but if just test2 is passed then the resulting dB will be called test2.sdf.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example 4 - Using DbContext and the .config file&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  using (var ctx = new DbContext(&quot;test5&quot;))  
2:  {  
3:    ctx.Database.Create();  
4:  }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
App or Web .config
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  &amp;lt;connectionStrings&amp;gt;  
2:    &amp;lt;add name=&quot;test5&quot;  
3:      providerName=&quot;System.Data.SqlServerCe.4.0&quot;  
4:      connectionString=&quot;Data Source=test5.sdf&quot;/&amp;gt;  
5:  &amp;lt;/connectionStrings&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
This time no factory is specified but the argument to DbContext is the name of a Connection String in the .config file. &amp;nbsp;As can be seen this contains similar information to that in the factory method enabling EF to create a dB of the correct type.&lt;br /&gt;
&lt;br /&gt;
To use these the instances of these databases rather than calling the create method on the context just use the context directly or more likely in the case of EF a derived context which brings us to one last example.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example 5 - Using a derived context and .config file&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  public class TestCtx : DbContext  
2:  {      
3:  }  
4:  using (var ctx = new TestCtx())  
5:  {  
6:    ctx.Database.Create();  
7:  }    
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
App or Web .config&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  &amp;lt;connectionStrings&amp;gt;  
2:    &amp;lt;add name=&quot;TestCtx&quot;  
3:      providerName=&quot;System.Data.SqlServerCe.4.0&quot;  
4:      connectionString=&quot;Data Source=test6.sdf&quot;/&amp;gt;  
5:  &amp;lt;/connectionStrings&amp;gt;  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
If a derived context is created which will almost&amp;nbsp;certainly&amp;nbsp;be the case then if an instance of this is created and a dB created then EF will look for a Connection String in the .config file that has the same name as the context and take the information from there.</description><link>http://petebarber.blogspot.com/2012/08/specifying-directory-to-create-sql-ce.html</link><author>noreply@blogger.com (Pete Barber)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-2918646278142295649.post-697138137308648689</guid><pubDate>Thu, 02 Aug 2012 14:10:00 +0000</pubDate><atom:updated>2012-08-03T02:17:36.490-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Code First</category><category domain="http://www.blogger.com/atom/ns#">CodeProject</category><category domain="http://www.blogger.com/atom/ns#">DbContext</category><category domain="http://www.blogger.com/atom/ns#">DbContext SQLServerCE C# NUnit SqlCeConnectionFactory</category><category domain="http://www.blogger.com/atom/ns#">EF</category><category domain="http://www.blogger.com/atom/ns#">Entity Framework</category><category domain="http://www.blogger.com/atom/ns#">Intergration Test</category><category domain="http://www.blogger.com/atom/ns#">Pattern</category><category domain="http://www.blogger.com/atom/ns#">Unit of work</category><category domain="http://www.blogger.com/atom/ns#">Unit Test</category><title>Integration Testing with NUnit and Entity Framework</title><description>This post gives a quick introduction into creating SQL CE dBs for performing Integration Tests using NUnit.&lt;br /&gt;
&lt;br /&gt;
In the previous post&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;a href=&quot;http://petebarber.blogspot.co.uk/2012/04/using-nunit-and-entity-framework.html&quot;&gt;Using NUnit and Entity Framework DbContext to programmatically create SQL Server CE databases and specify the databse directory&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;a basic way was shown to how to create a new dB (using Entity Framework&#39;s DbContext) programmtically. &amp;nbsp;This was used to generate a new dB for a test hosted by NUnit.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The subsequent post&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;a href=&quot;http://petebarber.blogspot.co.uk/2012/06/generating-sql-server-ce-database.html&quot;&gt;Generating a SQL Server CE database schema from a SQL Server database using Entity Framework&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;showed how to generate a SQL CE dB schema from an existing SQL Server database.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This post ties theprevious ones together. &amp;nbsp;As mentioned in the first post the reason for this is an attempt at what amounts to Integration Testing using NUnit. &amp;nbsp;I&#39;m currently building a &lt;a href=&quot;http://martinfowler.com/eaaCatalog/repository.html&quot;&gt;Repository&lt;/a&gt; and &lt;a href=&quot;http://martinfowler.com/eaaCatalog/unitOfWork.html&quot;&gt;Unit Of Work&lt;/a&gt; abstraction on top of Entity Framework which will allow the isolation of the dB code (in fact it will isolate and abstract away most forms of data storage). &amp;nbsp;This means any business logic can be tested with a test-double that implements the Repository and UnitOfWork interfaces; which is straight forward Unit Testing. &amp;nbsp;The Integration Testing is to verify that the Repository and Unit Of Work implementations work correctly.&lt;br /&gt;
&lt;br /&gt;
The rest of the post isn&#39;t focused on these two patterns; though it may mention them. &amp;nbsp;Instead it documents my further experience of using NUnit to writes tests that interact with dB via Entity Framework. &amp;nbsp;The premise for this is that a dB already exists.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;As such the approach to using Entity Framework is a hybrid of Database First and Code First in that the dB schema exists and needs be maintained outside of EF and also that EF should not generate model classes, i.e. allowing the use of Code First POCOs. &amp;nbsp;This is possible as the POCOs can be defined, a connection made to dB and then the two are conflated via an EF DbContext. &amp;nbsp;It then seems that EF creates the model on the fly (internally compiles it) and as long as the POCO types map to the dB types then it all works as if by magic!&lt;br /&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;The advantage of doing it this way is that the existing dB is SQL Express based but for the Integration Testing a new dB can be created when needed, potentially one per test. &amp;nbsp;In order to keep the test dBs isolated from the real dB SQL Server Compact Edition (SQL Server CE V4) was used. &amp;nbsp;Therefore the requirement was for the EF code to be able to work with SQL Express and SQL CE with the primary definition of the schema taken from SQL Express. &amp;nbsp;It&#39;s not possible to use exactly the same schema as SQL CE only has a subset of the data-types provides by SQL CE. &amp;nbsp;However, the process described in the post&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://petebarber.blogspot.co.uk/2012/06/generating-sql-server-ce-database.html&quot; style=&quot;background-color: transparent;&quot;&gt;Generating a SQL Server CE database schema from a SQL Server database using Entity Framework&lt;/a&gt;&lt;span style=&quot;background-color: transparent;&quot;&gt;&amp;nbsp;showed how to create semantically equivalent SQL.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
From this point onwards it&#39;s assumed that an SQL file to create the dB has been generated. &amp;nbsp;Now&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;create a new C# class library project and using the NUGet add Entity Framework, NUnit and SQL CE 4.0. &amp;nbsp;All my work has been with EF 4.3.1. &amp;nbsp;Following this drag the Model1.edmx.sqlce file from the project used to generate to new project. &amp;nbsp;You may wish to rename it, e.g. to test.sqlce.&lt;/span&gt;&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Creating the database&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;The post&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://petebarber.blogspot.co.uk/2012/06/generating-sql-server-ce-database.html&quot;&gt;Generating a SQL Server CE database schema from a SQL Server database using Entity Framework&lt;/a&gt;&amp;nbsp;showed how to create a new CE dB per-test using the EF DbContext to do the hard work. &amp;nbsp;A different approach is now taken as the problem with creating a dB using DbContext is that in addition to creating any specified tables and&amp;nbsp;indices&amp;nbsp;etc. it also creates an additional table called &#39;__MigrationHistory&#39; which contains a description of the EF model used to create the dB. &amp;nbsp;The description of the problem caused by this will be delayed until the &quot;Why DbContext is no longer used to create the database&quot; section. &amp;nbsp;Suffice to say for the present using the new mechanism avoids the creation of this table.&lt;br /&gt;
&lt;br /&gt;
The code below is the beginnings of a test class. &amp;nbsp;It is assumed all the tests need a fresh copy of the dB hence the creation is performed in the Setup method. &amp;nbsp;All this code does is create a SQL CE dB and then&lt;br /&gt;
creates the schema.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  [TestFixture]  
2:  public class SimpleTests  
3:  {  
4:   const string DB_NAME = &quot;test.sdf&quot;;  
5:   const string DB_PATH = @&quot;.\&quot; + DB_NAME;  
6:   const string CONNECTION_STRING = &quot;data source=&quot; + DB_PATH;  
7:   [SetUp]  
8:   public void Setup()  
9:   {  
10:    DeleteDb();  
11:    using (var eng = new SqlCeEngine(CONNECTION_STRING))  
12:     eng.CreateDatabase();  
13:    using (var conn = new SqlCeConnection(CONNECTION_STRING))  
14:    {  
15:     conn.Open();  
16:      string sql=ReadSQLFromFile(@&quot;C:\Users\Pete\work\Jub\EFTests\Test.sqlce&quot;);  
17:      string[] sqlCmds = sql.Split(new string[] { &quot;GO&quot; }, int.MaxValue, StringSplitOptions.RemoveEmptyEntries);  
18:      foreach (string sqlCmd in sqlCmds)  
19:       try  
20:       {  
21:        var cmd = conn.CreateCommand();  
22:    
23:        cmd.CommandText = sqlCmd;  
24:        cmd.ExecuteNonQuery();  
25:       }  
26:       catch (Exception e)  
27:       {  
28:        Console.Error.WriteLine(&quot;{0}:{1}&quot;, e.Message, sqlCmd);  
29:        throw;  
30:       }  
31:    }  
32:   }  
33:   public void DeleteDb()  
34:   {  
35:    if (File.Exists(DB_PATH))  
36:     File.Delete(DB_PATH);  
37:   }  
38:   private string ReadSQLFromFile(string sqlFilePath)  
39:   {  
40:    using (TextReader r = new StreamReader(sqlFilePath))  
41:    {  
42:     return r.ReadToEnd();  
43:    }  
44:   }  
45:  }  
46:    
&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;
The dB file (Test.sdf) will be created in the current working directory. &amp;nbsp;As the test&amp;nbsp;assembly&amp;nbsp;is located in &amp;lt;project&amp;gt;\bin\debug which is where the NUnit test runner picks up the DLL from this directory this is where it is created. &amp;nbsp;If a specific directory is required then the &#39;.\&#39; can be replaced with the required path.&lt;br /&gt;
&lt;br /&gt;
The Setup method is marked with NUnit&#39;s SetUp attribute meaning it will be invoked on a per-test basis creating a new dB instance for each test. &amp;nbsp;The DeleteDb method could be marked with [TearDown] attribute but at the moment any previous dB is deleted before creating a new one. &amp;nbsp;It would be fine to do both as a belt and braces approach. &amp;nbsp;The reason I didn&#39;t make it the TearDown method is so that I could inspect the dB following a test if needed.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
SQL CE does not support batch execution of SQL scripts which is where it gets interesting as the SQL generated&amp;nbsp;&lt;a href=&quot;http://petebarber.blogspot.co.uk/2012/06/generating-sql-server-ce-database.html&quot;&gt;previously&lt;/a&gt;&amp;nbsp;is in batch form. &amp;nbsp;The code reads the entire file into a string and determines each individual statement by splitting string on the &#39;GO&#39; command that separates each SQL command.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;To help understand the SQL the following is the diagram of the dB I&#39;m working with. &amp;nbsp;All fields are strings except for the Ids which are numeric.&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi84ll_6kXkjcSAko8s9NNHI4Sfzk_xar2hxsK16hxyxzCs_hOYt75WDbkJj3ImGY8cXgqJKcd9MwSLyFk0AjbFKKhLpX8F3XgcRLSJWkcRHieBnBAv8NLJnpAQAdq8F3XcmJ1wsTbw_gY/s1600/model.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi84ll_6kXkjcSAko8s9NNHI4Sfzk_xar2hxsK16hxyxzCs_hOYt75WDbkJj3ImGY8cXgqJKcd9MwSLyFk0AjbFKKhLpX8F3XgcRLSJWkcRHieBnBAv8NLJnpAQAdq8F3XcmJ1wsTbw_gY/s1600/model.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Each of these commands is then executed. &amp;nbsp;The &lt;a href=&quot;http://petebarber.blogspot.co.uk/2012/06/generating-sql-server-ce-database.html&quot;&gt;previously&lt;/a&gt;&amp;nbsp;generated&amp;nbsp;&lt;span style=&quot;background-color: white;&quot;&gt;SQL&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;(the SQL for the dB I&#39;m working with is below) will not work completely out of the box. &amp;nbsp;The ALTER and DROP statements at the beginning don&#39;t apply as the schema is being applied to an empty dB, these should be removed. &amp;nbsp;Interestingly the schema generation step for my dB seems to miss out a &#39;GO&#39; between the penultimate and ultimate&amp;nbsp;statement. &amp;nbsp;I had to add one by hand. &amp;nbsp;Finally, the comments at the end prove a problem as there is no terminating &#39;GO&#39;. &amp;nbsp;Removing these fixes the problem. &amp;nbsp;In the code above the exception handler re-throws the exception after writing out the details. &amp;nbsp;For everything to proceed the SQL needs modifying to execute perfectly. &amp;nbsp;If the re-throw is removed then the code will tolerate&amp;nbsp;individual&amp;nbsp;command failures which in this context really just amount to warnings.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;i&gt;NOTE: Text highlighted in &lt;span style=&quot;color: red;&quot;&gt;red&lt;/span&gt;&amp;nbsp;has been removed and text in &lt;span style=&quot;color: blue;&quot;&gt;blue&lt;/span&gt; added.&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- Entity Designer DDL Script for SQL Server Compact Edition&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- Date Created: 07/29/2012 12:28:35&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- Generated from EDMX file: C:\Users\Pete\work\Jub\DummyWebApplicationToGenerateSQLServerCE4Script\Model1.edmx&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- Dropping existing FOREIGN KEY constraints&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- NOTE: if the constraint does not exist, an ignorable error will be reported.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; ALTER TABLE [RepComments] DROP CONSTRAINT [FK_RepComments_Reps];&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- Dropping existing tables&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- NOTE: if the table does not exist, an ignorable error will be reported.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; DROP TABLE [RepComments];&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; DROP TABLE [Reps];&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; DROP TABLE [Roads];&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating all tables&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating table &#39;RepComments&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;CREATE TABLE [RepComments] (&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [CommentId] int IDENTITY(1,1) NOT NULL,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [RepId] int &amp;nbsp;NOT NULL,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [Comment] ntext &amp;nbsp;NOT NULL&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating table &#39;Reps&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;CREATE TABLE [Reps] (&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [RepId] int IDENTITY(1,1) NOT NULL,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [RepName] nvarchar(50) &amp;nbsp;NOT NULL,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [RoadName] nvarchar(256) &amp;nbsp;NOT NULL,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [HouseNumberOrName] nvarchar(50) &amp;nbsp;NOT NULL,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [ContactTelNumber] nvarchar(20) &amp;nbsp;NOT NULL,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [Email] nvarchar(50) &amp;nbsp;NULL&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating table &#39;Roads&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;CREATE TABLE [Roads] (&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; [Name] nvarchar(256) &amp;nbsp;NOT NULL&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating all PRIMARY KEY constraints&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating primary key on [CommentId] in table &#39;RepComments&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ALTER TABLE [RepComments]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ADD CONSTRAINT [PK_RepComments]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; PRIMARY KEY ([CommentId] );&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating primary key on [RepId] in table &#39;Reps&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ALTER TABLE [Reps]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ADD CONSTRAINT [PK_Reps]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; PRIMARY KEY ([RepId] );&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating primary key on [Name] in table &#39;Roads&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ALTER TABLE [Roads]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ADD CONSTRAINT [PK_Roads]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; PRIMARY KEY ([Name] );&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating all FOREIGN KEY constraints&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating foreign key on [RepId] in table &#39;RepComments&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ALTER TABLE [RepComments]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ADD CONSTRAINT [FK_RepComments_Reps]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; FOREIGN KEY ([RepId])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; REFERENCES [Reps]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ([RepId])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; ON DELETE NO ACTION ON UPDATE NO ACTION;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;-- Creating non-clustered index for FOREIGN KEY &#39;FK_RepComments_Reps&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;CREATE INDEX [IX_FK_RepComments_Reps]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;ON [RepComments]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; ([RepId]);&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- Script has ended&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red; font-family: Courier New, Courier, monospace;&quot;&gt;-- --------------------------------------------------&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
Getting the SQL into a state where it will run flawlessly is a little bit of a hassle but given the number of times it will be used subsequently it&#39;s job a big job, well for a small dB anyway. &amp;nbsp;To verify that your dB has been created as needed an quick and easy way to test is to comment out the call to DeleteDb() and after a test has run open to the dB using Server Explorer within VS, i.e.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjc9aJ7CNV2cu4YJdahIUOyUf9VIh7ls7O2v7ah3XBhr5j6a6RPGj54d-eIKT_sVbMGGRlTymIj2uOmhFoklH-Xi4f2msg3Upa38T86phQ6cQhOOa1InNVqPOvv65OfP1unLuG42lrBn9U/s1600/ServerExplorer.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjc9aJ7CNV2cu4YJdahIUOyUf9VIh7ls7O2v7ah3XBhr5j6a6RPGj54d-eIKT_sVbMGGRlTymIj2uOmhFoklH-Xi4f2msg3Upa38T86phQ6cQhOOa1InNVqPOvv65OfP1unLuG42lrBn9U/s1600/ServerExplorer.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
Using the dB in a test&lt;/h3&gt;
&lt;div&gt;
Now that a fresh dB will be created for each test it&#39;s time to look at simple test:&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  [Test]  
2:  public void TestOne()  
3:  {  
4:   using (var conn = new SqlCeConnection(CONNECTION_STRING))  
5:    using (var ctx = new TestCtx(conn))  
6:    {  
7:     ctx.Roads.Add(new Road() { Name = &quot;Test&quot; });  
8:     ctx.SaveChanges();  
9:     Assert.That(1, Is.EqualTo(ctx.Roads.Count()));  
10:   }  
11:  }  
&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;
Road in this case is defined as:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  class Road  
2:  {  
3:   [Key]  
4:   public string Name { get; set; }  
5:  }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The first thing to note is that EF is not used to form the connection to the dB, instead one is made using the SqlCe specific classes. &amp;nbsp;Attempting to get EF to connect to a specific dB instance when &lt;i&gt;not&lt;/i&gt; referring to a named &lt;a href=&quot;http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx&quot;&gt;connection strings&lt;/a&gt; in the .config file is a bit of an art (I may write another entry about this). &amp;nbsp;However, EF is quite happy to work with an existing connection. &amp;nbsp;This makes for a good separation of responsibilities in the code where EF manages the interactions with the dB but the control of the connection is elsewhere.&lt;br /&gt;
&lt;br /&gt;
NOTE: It is likely that each test will require a connection and a context hence rather it might make more sense to move the creation of the SqlCeConnection and the context (TestCtx in this case) to a SetUp method and as these resources need disposing of adding a TearDown method to do that. &amp;nbsp;TestCtx could also be modified to pass true to the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/gg696604(v=vs.103).aspx&quot;&gt;DbContext &lt;/a&gt;constructor to give ownership of the connection to the context so that it will dispose of it then context is disposed off.&lt;br /&gt;
&lt;br /&gt;
I would have preferred to avoid having to defined a specific derived context and instead use DbContext directory, e.g.&lt;br /&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  [Test]  
2:  public void TesTwo()  
3:  {  
4:   using (var conn = new SqlCeConnection(CONNECTION_STRING))  
5:    using (var ctx = new DbContext(conn, false))  
6:    {  
7:     ctx.Set&amp;lt;Road&amp;gt;().Add(new Road() { Name = &quot;Test&quot; });  
8:     ctx.SaveChanges();  
9:     Assert.That(1, Is.EqualTo(ctx.Set&amp;lt;Road&amp;gt;().Count()));  
10:    }  
11:  }  
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
However when SaveChanges() is called the following exception is thrown:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;System.InvalidOperationException : The entity type Road is not part of the model for the current context.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;This is because EF knows nothing about the Road type. &amp;nbsp;When a derived context is created for the first time I think EF performs reflection on any properties that expose DbSet. &amp;nbsp;These are the types that form the Model. &amp;nbsp;Another option is to &lt;a href=&quot;http://blog.oneunicorn.com/2012/04/21/code-first-building-blocks/&quot;&gt;create the model, optionally compile it and then pass it to an instance of DbContext&lt;/a&gt;. &amp;nbsp;This way involves a lot less code.&lt;br /&gt;
&lt;br /&gt;
That&#39;s it. &amp;nbsp;The final section is just footnote about the move away from using EF to create the dB.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Why DbContext is no longer used to create the database&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;As mentioned creating the dB using:&lt;/span&gt;&lt;/div&gt;
&lt;pre style=&quot;background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5RwyszBPEPvL4uI2b0WuPsjka8cJ6t1GOn9wAw8pQqgpXjE3WT3T76bNW98XTIetyBVjR8L-AAlOMMlH4u8qwQ7GbKwpYNdR8yuFt7mYf3-R4SdSNso9D2XonAX5uqRnhyphenhyphenZt-uWWSYmfR/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;&quot;&gt;&lt;code style=&quot;color: black; word-wrap: normal;&quot;&gt;1:  using (var ctx = new DbContext(&quot;bar.sdf&quot;))  
2:  {  
3:   ctx.Database.Create();  
4:   // create schema etc.  
5:  }  
&lt;/code&gt;&lt;/pre&gt;
causes the&amp;nbsp;&#39;__MigrationHistory&#39;&amp;nbsp;table to be created. &amp;nbsp;Assuming this method was used, later on when TestCtx was used top open the dB and perform an operation the following exception would be thrown:&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;System.InvalidOperationException : The model backing the &#39;DbContext&#39; context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)&lt;/span&gt;.&lt;/i&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;This is&amp;nbsp;because&amp;nbsp;the context used to create the model was a raw DbContext (as per the previous post) whereas the dB was accessed via the TestCtx. &amp;nbsp;If the context used to create the dB is also changed to TestCtx then this problem goes away.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;However, given the original dB is not intended to be created nor be maintained (code migrations) by EF then using the non-context/EF approach to dB completely removes EF from the picture.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</description><link>http://petebarber.blogspot.com/2012/08/integration-testing-with-nunit-and.html</link><author>noreply@blogger.com (Pete Barber)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi84ll_6kXkjcSAko8s9NNHI4Sfzk_xar2hxsK16hxyxzCs_hOYt75WDbkJj3ImGY8cXgqJKcd9MwSLyFk0AjbFKKhLpX8F3XgcRLSJWkcRHieBnBAv8NLJnpAQAdq8F3XcmJ1wsTbw_gY/s72-c/model.png" height="72" width="72"/><thr:total>1</thr:total></item></channel></rss>