<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Andy Chou on Medium]]></title>
        <description><![CDATA[Stories by Andy Chou on Medium]]></description>
        <link>https://medium.com/@_achou?source=rss-7180499745ce------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7mFhXMT4-O6Nj8qEuMp9NA.jpeg</url>
            <title>Stories by Andy Chou on Medium</title>
            <link>https://medium.com/@_achou?source=rss-7180499745ce------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 06:42:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@_achou/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introducing faast.js]]></title>
            <link>https://medium.com/@_achou/introducing-faast-js-c9b33f3b730b?source=rss-7180499745ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9b33f3b730b</guid>
            <category><![CDATA[aws-lambda]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[serverless]]></category>
            <dc:creator><![CDATA[Andy Chou]]></dc:creator>
            <pubDate>Thu, 02 May 2019 18:28:59 GMT</pubDate>
            <atom:updated>2019-05-03T03:55:50.705Z</atom:updated>
            <content:encoded><![CDATA[<p>What if serverless functions were as easy to use as regular functions?</p><p>There would be no infrastructure to create, no command lines to run. You would simply write functions and call them, and they would handle everything else needed to run in the cloud. They would work with all the nice things in your IDE, and be programmable with the tools you already use. So powerful, you could summon a thousand cores to demolish large jobs in a snap. So convenient, it would start to feel limiting not to use them. <em>It would make you think differently about how to write code.</em></p><p>That’s what faast.js was designed to do.</p><p>Faast.js started as a side project to solve the problem of large scale software testing. Serverless functions seemed like a good fit because they could scale up to perform work in parallel, then scale down to eliminate costs when not being used. Even better, all infrastructure would be managed by the cloud provider. It seemed like a dream come true: a giant computer that could be as big as needed for the job at hand, yet could be rented in 100ms increments.</p><p>But trying to build this on AWS Lambda was challenging:</p><ul><li><strong>Complex setup.</strong> Lambda throws you into the deep end with IAM roles, permissions, command line tools, web consoles, and special calling conventions. Lambda and other FaaS are oriented towards an event-based processing model, and not optimized for batch processing.</li><li><strong>Primitive package dependency support.</strong> Everything has to be packaged up manually in a zip file. Every change to the code or tests requires a manual re-deploy.</li><li><strong>Native packages.</strong> Common testing tools like puppeteer are supported only if they are compiled specially for Lambda.</li><li><strong>Persistent infrastructure.</strong> Logs, queues, and functions are left behind in the cloud after a job is complete. These incur costs and count towards service limits, so they need to be managed or removed, creating an unnecessary ops burden.</li><li><strong>Developer productivity.</strong> Debugging, high quality editor support, and other basic productivity tools are awkward or missing from serverless function development tooling.</li></ul><p>Faast.js was born to solve these and many other practical problems, to make serverless batch processing as simple as possible.</p><h3>Design principles for faast.js</h3><p>Faast.js presents a <a href="https://web.stanford.edu/~ouster/cgi-bin/cs190-winter18/lecture.php?topic=modularDesign">“deep” interface</a>. The main interface to faast.js is a single function, faast():</p><pre>faast(&quot;aws&quot;, funcs);</pre><p>The only additional thing required is a cloud provider account. Each exported function in funcs is automatically available for invocation as a serverless function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v2qR_emx_YoW6WAU.png" /></figure><p>On the left is an ordinary JavaScript module. On the right is the code that invokes these functions in a serverless function. As the developer types m.functions., autocomplete pops up all of the exports from the module. Instant feedback on code changes across the serverless function call boundary makes developing with faast.js exceptionally efficient.</p><h3>Ephemeral infrastructure</h3><p>When faast() is invoked, it starts creating the infrastructure it needs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Srfj-unsjT8pi2CeatFgjQ.png" /></figure><p>The exact infrastructure created depends on the cloud provider and the options you provide. But the beautiful thing is users of faast.js don’t need to manage any of this infrastructure. You don’t even need to run a command to initialize it. Faast.js will automatically create new infrastructure for you on the fly, so there is no “deploy” step with faast.js; every time you invoke faast(), code is bundled and infrastructure is deployed.</p><p>The benefits of ephemeral infrastructure are many. New faast.js instances will start with a clean slate, without contamination of global variables or local filesystems with data leaked from prior function invocations. New code versions are deployed immediately, reducing the friction of rapid iteration. And multiple instances of the same workflow can be started at the same time without interference.</p><p>From a security standpoint, infrastructure that isn’t there very long is harder to exploit. Like all serverless platforms, infrastructure patching is handled by the cloud provider. But faast.js goes further because code is re-bundled every time it’s deployed. Just keep local package dependencies up to date and you’re automatically keeping infrastructure patched. No need to worry about migration and upgrade of long-running services.</p><p>When your job is done, call a single cleanup function to remove all the infrastructure that faast.js created. There’s even a garbage collector to clean up infrastructure remnants left over from crashes and incomplete cleanup.</p><p>Creating and destroying infrastructure repeatedly might seem to be wasteful or slow. But cloud providers have been working for years on making these infrastructure operations faster, and on AWS the infrastructure needed to create a cloud function from scratch can take as little as 2 seconds.</p><h3>As easy as serverless batch gets</h3><p>That’s the basics. Check out more in the <a href="https://faastjs.org/docs/introduction">documentation</a>.</p><p>You are now free to invoke code in the cloud at any scale you wish, with no hassles. Try it out!</p><h3>Use cases</h3><p>Here are a few ideas to get you started.</p><ul><li><strong>Test at scale.</strong> Run hundreds tests in parallel. Run <em>thousands</em> of tests in parallel. Run <a href="https://github.com/GoogleChrome/puppeteer">headless chrome</a> and grab screenshots of web pages a thousand at a time.</li><li><strong>Generate load.</strong> Don’t bother setting up a complex load testing environment. Mix request logic, stir with faast.js, and you’ve got instant load testing infrastructure. Imagine load testing every commit, or even have developers load test before they commit.</li><li><strong>Process documents and data.</strong> Scatter 1TB of data from S3 to Lambda functions in under 40 seconds. Run transformations, analysis, ETL, and other workloads faster, and do it with no operational overhead, just pure code.</li><li><strong>Process images.</strong> Make light work of resizing boatloads of images. <a href="https://github.com/lovell/sharp">Sharp</a> and other native image processing libraries can be used with minimal hassle with faast.js.</li></ul><p>This is just the tip of the iceberg. I’m excited to see what you’ll build with faast.js!</p><p>If you’d like to discuss faast.js ideas or issues, join our <a href="https://join.slack.com/t/faastjs/shared_invite/enQtNTk0NTczMzI4NzQxLTA2MWU1NDA1ZjBkOTc3MTNkOGMzMDY0OWU1NGQ5MzM2NDY1YTJiZmNmODk4NzI0OWI1MzZhZDdiOTIzODNkOGY">slack channel</a>. Also check out our <a href="https://faastjs.org/">website</a>, <a href="https://faastjs.org/docs/introduction">docs</a>, and <a href="https://github.com/faastjs/faast.js">github repository</a>.</p><h3>Up next</h3><p>In my next blog post, I’ll cover how faast.js helps understand and optimize cloud costs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9b33f3b730b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Don’t give up and use suppressImplicitAnyIndexErrors]]></title>
            <link>https://medium.com/@_achou/dont-give-up-and-use-suppressimplicitanyindexerrors-ca6b208b9365?source=rss-7180499745ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca6b208b9365</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Andy Chou]]></dc:creator>
            <pubDate>Wed, 09 Jan 2019 02:02:35 GMT</pubDate>
            <atom:updated>2019-01-11T17:45:24.592Z</atom:updated>
            <content:encoded><![CDATA[<p>Preserve type safety with a simple helper function</p><p>You’ve turned on strict mode with TypeScript and now your code complains about mysterious errors such as:</p><pre>Element implicitly has an &#39;any&#39; type because type &#39;{}&#39; has no index signature. [7017]</pre><p>Don’t despair. The problem is likely code that looks like:</p><pre>for (const key of <strong>Object</strong>.<strong>keys</strong>(obj)) {<br>    ...<br>}</pre><p>The problem here is the Object.keys() part. The type of this function in the TypeScript standard type library is:</p><pre><strong>keys</strong>(o: {}): string[];</pre><p>See the type for o? It’s given up on knowing anything about the type of the object, in particular what keys it has and the types of specific keys. This can be particularly galling when the type is a constant:</p><pre>const obj = { field: &quot;value&quot; };<br>for (const key of <strong>Object</strong>.<strong>keys</strong>(obj)) {<br>    const v = obj[key]<br>    // Why can&#39;t TypeScript figure out that &#39;v&#39; has type string??<br>}</pre><p>It’s not hard to fix this in such cases by adding a simple helper function that makes use of TypeScript’s handy keyof operator:</p><pre>export function <strong>keys</strong>&lt;<strong>O</strong> extends object&gt;(obj: <strong>O</strong>): <strong>Array</strong>&lt;keyof <strong>O</strong>&gt; {<br>    return <strong>Object</strong>.<strong>keys</strong>(obj) as <strong>Array</strong>&lt;keyof <strong>O</strong>&gt;;<br>}</pre><p>Now you can write your code like:</p><pre>for (const key of <strong>keys</strong>(obj)) {<br>   const v = obj[key]<br>    // Ok, &#39;v&#39; has type string<br>}</pre><p>When the static type of the object you are iterating over has a fixed set of keys (not uncommon), you’ll get more precise types for key and the error will disappear. If not, then you should consider adding a type index signature for the type.</p><p>There is one caveat though: keys(obj) will have type never[] if the argument has type object or {}. While never is moderately restrictive in what you can do with it, it is possible to do things with a never value that will cause trouble. So some vigilance is still required.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca6b208b9365" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using TypeScript to statically check exhaustive destructuring]]></title>
            <link>https://medium.com/@_achou/using-typescript-to-statically-check-exhaustive-destructuring-7d56a7514127?source=rss-7180499745ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d56a7514127</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Andy Chou]]></dc:creator>
            <pubDate>Mon, 11 Jun 2018 20:34:35 GMT</pubDate>
            <atom:updated>2018-06-13T17:18:24.129Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html">TypeScript 2.8</a> added the Required&lt;T&gt; type, which can provide static exhaustiveness checking when destructuring objects. Here’s how.</p><p>Let’s say you have an interface:</p><pre>interface State {<br>  a: number;<br>}</pre><p>and use destructuring to access its properties:</p><pre>cleanup(state: State) {<br>  let { a } = state;<br>  ...<br>}</pre><p>Now let’s say a new property is added in the future:</p><pre>interface State {<br>  a: number;<br>+ b: number;<br>}</pre><p>At this point the destructuring in the cleanup function is missing handling for b, but there’s no static error to guide you to it. If we write the code differently we will be alerted to the issue by TypeScript’s static checking:</p><pre>interface State {<br>    a: number;<br>    b: number;<br>}</pre><pre>function cleanup(state: State) {<br>    const { a, ...rest } = state;<br>    const _exhaustiveCheck: Required&lt;typeof rest&gt; = {};  // Error<br>}</pre><p>This also works for checking exhaustive destructuring of array tuples and function parameters.</p><p>This form of exhaustiveness check is different than the check done using the never type, which is more useful for switch statements and other control flow that is intended to cover all cases. <a href="https://basarat.gitbooks.io/typescript/docs/types/never.html">Here’s a link if you are looking for that.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d56a7514127" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RxSwift: share vs replay vs shareReplay]]></title>
            <link>https://medium.com/@_achou/rxswift-share-vs-replay-vs-sharereplay-bea99ac42168?source=rss-7180499745ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/bea99ac42168</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[rxswift]]></category>
            <dc:creator><![CDATA[Andy Chou]]></dc:creator>
            <pubDate>Wed, 11 Jan 2017 01:08:27 GMT</pubDate>
            <atom:updated>2017-08-31T15:47:23.706Z</atom:updated>
            <content:encoded><![CDATA[<p>A common beginner error with <a href="http://reactivex.io/">Rx</a> is forgetting that each subscription to an observable re-executes the chain of operators:</p><pre>let results = query.rx.text<br>    .flatMapLatest { query in<br>        networkRequestAPI(query)<br>    }</pre><pre>results.subscribe(...)   // a network request<br>results.subscribe(...)   // another network request</pre><p>We have multiple subscriptions to the same Observable but don’t want the observable to re-execute for each subscription. RxSwift has several operators for this: share(), replay(), replayAll(), shareReplay(), publish(), and even shareReplayLatestWhileConnected(). Which one to use?</p><p>Here’s a summary of the properties of these operators:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-1YisI9nVIFSqLade2Hk8Q.png" /><figcaption>1 Replays at most bufferSize events 2 Replays one event while reference count of subscribers &gt; 0</figcaption></figure><p><strong>Shared subscription: </strong>The returned observable shares a single underlying subscription to the source observable. This is the case for all of these operators.</p><p><strong>Connectable:</strong> The returned observable does not push events until the connect() function is called. This allows multiple subscribers to subscribe before any events are sent.</p><p><strong>Reference counting:</strong> The returned observable reference counts the number of subscribers it has. When the number of subscribers goes from zero to one, subscribes to the source observable. When the number goes from one to zero, unsubscribe and dispose the source observable. <strong>Note:</strong> <strong>the underlying observable will be re-subscribed each time the number of subscribers goes to zero to one. </strong>However there will never be more than one subscription to the source observable at a time, and all concurrent subscribers will share the same subscription to the source observable. If the underlying observable completes or send an error, the underlying observable might not be re-subscribed. This is a gray area I suggest avoiding, which can be done by ensuring there are no further subscribers after the reference count drops to zero.</p><p><strong>Replays events: </strong>The operator will replay events from the source observable to subscribers who after those events are sent. For replay(bufferSize) and shareReplay(bufferSize), the number of events is at most bufferSize. For shareReplayLatestWhileConnected(), at most one event is replayed. When the reference count of subscribers drops to zero, the replay buffer is cleared. Therefore subscribers that cause the reference count to go from zero to one do not get an event replayed.</p><p>There are also multicast operators which can be used if you have Subjects. But the operators described here are the most popular and most likely to be useful for most applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bea99ac42168" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[One Weird Trick to Fix Autocomplete in Xcode]]></title>
            <link>https://medium.com/@_achou/one-weird-trick-to-fix-autocomplete-in-xcode-9f19dc856944?source=rss-7180499745ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f19dc856944</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Andy Chou]]></dc:creator>
            <pubDate>Thu, 13 Oct 2016 00:53:16 GMT</pubDate>
            <atom:updated>2016-10-13T00:56:42.338Z</atom:updated>
            <content:encoded><![CDATA[<p>Here’s the problem: your project compiles fine, but autocomplete always seems to have this issue:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*MJmbdpG_o_eqnvNXzy3qpQ.png" /></figure><p>Why does autocomplete have an error type for my correctly compiling project?!?</p><p><strong>Answer: You probably have your framework search path set in the wrong place.</strong></p><p>Most likely, you have set your framework search path on the target (the “A”) below. But you haven’t set it for the <strong>project </strong>(which is empty):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V5sHdQMTOFyPdpOzlJn92A.png" /></figure><p>To fix it, add the search path to the project, and remove it from the target. The target will automatically inherit the search path from the project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I7MH_WhbzI-hA8yX3CXEpQ.png" /></figure><p>See the difference? The problem was that Xcode has a separate compilation process for computing autocomplete references, from the compilation process for actually building your project. If you accidentally set the framework search path for your target instead of the project as a whole, your project will seem to build fine. You’ll even get autocomplete for symbols that are not in the missing frameworks. But you’ll get the dreaded error type for anything in the frameworks you’ve misconfigured.</p><p>Fortunately, there’s a simple fix. Autocomplete is <em>beautiful</em> when your project is configured properly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*16bQ2LR3nbKVLlB9YSnA3g.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f19dc856944" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making a playground using RxSwift]]></title>
            <link>https://medium.com/@_achou/making-a-playground-using-rxswift-81d8377bd239?source=rss-7180499745ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/81d8377bd239</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[rxswift]]></category>
            <dc:creator><![CDATA[Andy Chou]]></dc:creator>
            <pubDate>Tue, 24 May 2016 23:34:58 GMT</pubDate>
            <atom:updated>2017-03-11T17:03:02.795Z</atom:updated>
            <content:encoded><![CDATA[<p>Note: Updated on 3/10/2017 to use Xcode 8.2.1 and CocoaPods 1.2.0.</p><p><strong>Situation</strong>: You’re using <a href="https://github.com/ReactiveX/RxSwift">RxSwift</a> in a project. You realize that it would be really nice to have a playground to test out ideas.</p><p><strong>Problem</strong>: You can’t import RxSwift (or RxCocoa) into your playground. Apple’s <a href="https://developer.apple.com/library/ios/recipes/Playground_Help/Chapters/ImportFramework.html">instructions</a> are opaque.</p><p><strong>Solution</strong>: Use CocoaPods to install RxSwift. CocoaPods will create a workspace that includes RxSwift and your existing project. A workspace created within the project will be able to import RxSwift.</p><p><strong>Detailed instructions</strong>: If you’re not already using CocoaPods, initialize a new Podfile:</p><pre>$ pod init</pre><p>Now you should have a Podfile in your project directory. Add the following segment in your Podfile:</p><pre># Pods for Project<br>pod &#39;RxSwift&#39;, &#39;~&gt; 3.2&#39;</pre><p>Then install this pod:</p><pre>$ pod install</pre><p>Now you should have a workspace (with .xcworkspace extension). Open this file with Xcode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*CP-oIhh9uPTPNUh0wvTfsA.png" /><figcaption>After installing your pods, make sure you open the workspace (on the right), not the project (on the left).</figcaption></figure><p>Now you can create your playground.</p><p>Go to the project navigator and unselect everything (⌘-click any selected items). This is important because when you add a project in the next step, you want it to be under the workspace and not the existing project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*-RdULTc551Hs6zEYg9Pjyw.png" /><figcaption>Make sure nothing is selected.</figcaption></figure><p>Add a new Playground to the workspace using the “+” button in the project navigator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*A0gXjmtWaA-0TcsePJonKA.png" /></figure><p>Give your playground a name. I suggest putting it in the same directory as your Project.xcodeproj and Project.xcworkspace file. Keep things together.</p><p>At this point you should have a workspace with your project, playground, and pods all at the toplevel of the workspace:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*BTg0-dIZxBGNK68s6wE6zg.png" /></figure><p>Now build your project. This should also build the Rx target you selected in the previous step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/1*N5DAzWYVbQWq33bUC5CG0Q.png" /></figure><p>Your build should succeed!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/208/1*aG5IeNX2oESvNUri_DF9UQ.png" /><figcaption>Yay!</figcaption></figure><p>Now go to your playground and import RxSwift.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*Mzv-SPRzyQwDrc2spKLDjQ.png" /></figure><h4>Using your RxSwift Playground</h4><p>You may notice that RxSwift behaves a little differently in a playground. Specifically, if you create an Observable.interval, it doesn’t seem to do anything. That’s because you need to tell Xcode to keep running your playground continuously, otherwise it simply runs every statement and immediately stops execution before the interval observable can send events.</p><p>This can be solved by adding this SupportCode.swift file under Playground / Sources.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a8de23cae4eb6dd9824ce0356c04de9/href">https://medium.com/media/3a8de23cae4eb6dd9824ce0356c04de9/href</a></iframe><p>At the end of your playground, add a call to playgroundTimeLimit to specify how long you want the playground to run. For example:</p><pre>playgroundTimeLimit(10)</pre><p>This will keep the playground running for 10 seconds, after which the playground will automatically stop, saving your laptop battery and limiting the output buffer size.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81d8377bd239" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Newbie Xcode Tips: Projects and Targets]]></title>
            <link>https://medium.com/@_achou/newbie-xcode-tips-projects-and-targets-23f535cb1b63?source=rss-7180499745ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/23f535cb1b63</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[prototyping]]></category>
            <dc:creator><![CDATA[Andy Chou]]></dc:creator>
            <pubDate>Thu, 07 Apr 2016 18:58:03 GMT</pubDate>
            <atom:updated>2016-04-07T18:58:03.743Z</atom:updated>
            <content:encoded><![CDATA[<p>Xcode is Apple’s Integrated Development Environment (IDE) for developing iOS and Mac apps. The main languages supported are Swift and Objective-C. Xcode can seem complex at first. The best way to get into it is to peel back the onion one layer at a time. <em>(NOTE: I’m using Xcode 7.3 released in March 2016)</em>.</p><p>The first layer is the surface of the IDE. Apple’s <a href="https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/index.html#//apple_ref/doc/uid/TP40010215">Xcode guide</a> is highly recommended to get oriented, and especially the <a href="https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214">Food Tracker app</a>. It walks you through the basic functionality of what all of the layers of UI do. But after going through this tutorial and starting my own project, I realized that it doesn’t provide enough conceptual understanding of Xcode’s model of your project. That’s what this post is mostly about: some things about Xcode I wish someone could have told me when I started.</p><h4>Projects != Folders</h4><p>The Project Navigator shows you the contents of your project, in what appears to be organized by the folders you have on disk. But that’s just an illusion. Instead, think of projects as managing a flat set of absolute file paths. Each absolute path is represented by a file in Xcode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*PnNCZRtmvDQCabb9m9ZGqw.png" /><figcaption>Xcode Project Navigator</figcaption></figure><p>What look like yellow folders in the Project Navigator are actually Groups. Groups don’t exist on disk, they are virtual folders that only exist in the context of Xcode. Groups might correspond to a folders on disk but do not need to. In fact, the organization of files in the Project Navigator can be completely unrelated to the on-disk structure. They are there to help you, as a developer, organize stuff. But Xcode basically ignores group structure and treats your project as a single flat set of files with no hierarchical organization.</p><p>There is also no magic to the naming of groups. FoodTrackerTests is just an ordinary group, there is no special meaning ascribed to it by having the word “Test” in its name. The group structure is totally up to you to define as you please.</p><p>Ok, I lied. There is one magic group — Products. It’s the place where Xcode will put the final build outputs such as your application binary. It’s also where Xcode will place your test bundle (basically your tests are a library linked against the XCTest framework). As far as I can tell, the Products group is special because you can’t move any files out of it into a different location like you can with other files.</p><p>To see the physical location of a file, select it in the Project Navigator and show the Utilities panel (the panel on the right hand side — you did go through the <a href="https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html#//apple_ref/doc/uid/TP40015214">Xcode guide</a>, didn’t you?), and select the File Inspector:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/428/1*lGZiCb8ZjBWxHKIAFdzY0A.png" /><figcaption>File Inspector shows the location of files on disk</figcaption></figure><p>The New Project template (I can’t help calling it a “wizard”) creates groups that are reasonable defaults. Xcode will automatically create folders in your project directory on disk that look like the group structure in the Project Navigator. I think it is sensible to keep the on-disk folder structure and the group structure in sync, but it requires updating both parts — the filesystem and Xcode’s project structure — independently.</p><p>Personally I think it is sensible to have a project layout that basically mirrors your project’s structure. It requires a little more maintenance when moving things around, but it’s worth the trouble to keep future developers on the project sane.</p><p><strong>Test your understanding.</strong> Try creating a project with two files with the name <strong>foo.swift</strong> in the same group.</p><h4>Targets define the output files</h4><p>Another thing I found initially confusing was targets. If you’re familiar with <strong>make</strong> and other similar build tools, targets should be familiar to you. A target is an output file; all of the settings for targets are around defining what files get compiled and otherwise processed to produce that target file. There are different types of targets: apps, frameworks, etc. But in essence each is a single output file.</p><p>There is a special file <strong>Info.plist</strong> that specifies some of the settings for each target. The Info.plist for each target is specified in the target settings; like most other files in a project, it can be moved around in the Project Navigator without changing its on-disk location. Most of the target’s settings are in the Project settings, not in Info.plist. I have no idea why some settings are in Info.plist and others are in the project settings.</p><p>It is very common to have a group for each target. That’s what we have in our example: a group for the FoodTracker app target, and a group for the FoodTrackerTests test bundle target. The files in the FoodTracker group are pretty much the files that are in the app. But FoodTrackerTests group is a bit different. It contains files such as <strong>FoodTrackerTests.swift</strong> that are unique to the test target and not compiled into the app. This file contains the test code. But as we’ll see, the test target also includes some of the files from the app, which are the files containing the code to be tested.</p><h4>Test targets are just targets</h4><p>A test target is a target that happens to be set up to produce a bundle that runs tests. This diagram shows how the test target is typically setup:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h5rixZw1YR07nKzfRcUGwQ.png" /></figure><p>A test target compiles the code you want to test along with tests themselves. Selecting which files to compile as part of a test target works the same way as for any target: through the Compile Sources dialog in the target settings:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*BITnMqiHolVh1LSFyX07jg.png" /><figcaption>Selecting which files to compile as part of a test target</figcaption></figure><p>When you create a test target using Xcode’s new target template, it automatically creates:</p><ol><li>A folder on disk for your test target</li><li>An Info.plist file in the test directory. Xcode does this for all targets, and a test target is just another target.</li><li>A simple test file that contains a template for what tests to run</li></ol><p>That’s all for now. I’m pretty sure my understand of things is incomplete, but writing down what I do know has helped me and I hope it can help you too.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23f535cb1b63" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>