<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>GregHeo.com</title>
  <subtitle>Nerdy tech posts</subtitle>
  <id>http://gregheo.com/</id>
  <link href="http://gregheo.com/"/>
  <link href="http://gregheo.com/feed.xml" rel="self"/>
  <updated>2019-01-15T14:00:00-08:00</updated>
  <author>
    <name>Greg Heo</name>
  </author>
  <entry>
    <title>Fun With Flags</title>
    <link rel="alternate" href="/blog/fun-with-flags/"/>
    <id>/blog/fun-with-flags/</id>
    <published>2019-01-15T14:00:00-08:00</published>
    <updated>2019-01-15T14:00:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;Based on a talk presented at the &lt;a href="https://www.meetup.com/tacow-org/events/257411395/"&gt;Toronto Area Cocoa and WebObjects Developers Group (tacow)&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The word &lt;em&gt;semaphore&lt;/em&gt; has two parts: &lt;em&gt;sema&lt;/em&gt; for &amp;ldquo;meaning&amp;rdquo; (like the word &lt;em&gt;semantic&lt;/em&gt;) and &lt;em&gt;phoros&lt;/em&gt; for &amp;ldquo;carry&amp;rdquo; (like a &lt;em&gt;pheremone&lt;/em&gt; carrying a chemical signal).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Flag_semaphore"&gt;Flag semaphores&lt;/a&gt; are a communication tool, carrying meaning across some distance.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/semaphore-alphabet.jpg" class="bordered" width="512" height="362" alt="Semaphore alphabet"&gt;
&lt;/div&gt;

&lt;p&gt;You have to communicate one letter at a time which can be slow, but communication is silent and can be done so long as the sender and receiver are in visible range.&lt;/p&gt;

&lt;p&gt;But what does communicating with flags have to do with concurrency?&lt;/p&gt;

&lt;h2&gt;Concurrency vs Synchronization&lt;/h2&gt;

&lt;p&gt;Concurrency is easy: you have multiple sequential processes, and they run independently of each other. In what order? Who cares, it&amp;rsquo;s concurrent!&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/semaphore-sequential.png" class="" width="472" height="484" alt="Concurrent sequential processes"&gt;
&lt;/div&gt;

&lt;p&gt;If you &lt;em&gt;do&lt;/em&gt; care about the order across processes, you need to introduce &lt;em&gt;synchronization&lt;/em&gt;. And it turns out, synchronization is all about communication between these processes (or threads, or queues, etc.)&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/semaphore-synchronized.png" class="" width="472" height="442" alt="Concurrent sequential processes, synchronized"&gt;
&lt;/div&gt;

&lt;p&gt;By communicating, we can create synchronization points to help us line up events on one process in relation to events in the other process.&lt;/p&gt;

&lt;p&gt;What kind of communication do we need to send to manage multi-threaded computing and concurrent processes?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Locks, for example, can enforce mutual exclusion. Each process communicates with the lock to check whether it&amp;rsquo;s OK to continue.&lt;/li&gt;
&lt;li&gt;Dispatch queues on Apple platforms manage sibling tasks. You can enqueue multiple things on a serial queue and it manages running them in order.&lt;/li&gt;
&lt;li&gt;Operation queues are similar and let you specify dependencies. The operation manages the signaling needed to start task B when task A completes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Semaphores offer another way to do this kind of communication and coordination.&lt;/p&gt;

&lt;h2&gt;Semaphores&lt;/h2&gt;

&lt;p&gt;A semaphore is like a lock with a counter. The simplest kind of semaphore is a &lt;em&gt;binary semaphore&lt;/em&gt;, which acts like a lock. It&amp;rsquo;s &amp;ldquo;binary&amp;rdquo; since (if used properly) the value is either 0 or 1.&lt;/p&gt;

&lt;p&gt;If the value is 1, the resource protected by the semaphore is available. If a task &lt;code&gt;waits&lt;/code&gt; for the semaphore, the value goes down to 0.&lt;/p&gt;

&lt;p&gt;If another task comes by and &lt;code&gt;waits&lt;/code&gt; for the semaphore, it’s blocked and can&amp;rsquo;t continue since the value is 0.&lt;/p&gt;

&lt;p&gt;When the first task completes it &lt;code&gt;signals&lt;/code&gt; the semaphore, which increments the integer from 0 to 1. Then that waiting task wakes up, &amp;ldquo;acquires the lock&amp;rdquo; so to speak, and the semaphore value goes down to 0 again.&lt;/p&gt;

&lt;h3&gt;Bigger values&lt;/h3&gt;

&lt;p&gt;Simple locks and binary semaphores are on or off, but you can imagine initializing a semaphore with a value greater than 1.&lt;/p&gt;

&lt;p&gt;Say you had a queue of URLs to download things from the internet. To avoid overwhelming the connection, you only want three active downloads at a time.&lt;/p&gt;

&lt;p&gt;You could initialize a semaphore with a value of 3:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;allowDownload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;DispatchSemaphore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then on a background queue, start a download:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;queue1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeLast&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;allowDownload&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="c1"&gt;// start the download here&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="c1"&gt;// download completed&lt;/span&gt;
  &lt;span class="n"&gt;allowDownload&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You could create any number of background queues or concurrent queues with hundreds of downloads but if they all use the semaphore properly, only three downloads at most will happen at one time.&lt;/p&gt;

&lt;h2&gt;Vs Locks&lt;/h2&gt;

&lt;p&gt;Semaphores are conceptually similar to locks with a counter, but there are some important differences too.&lt;/p&gt;

&lt;h3&gt;Thread safety&lt;/h3&gt;

&lt;p&gt;With most implementation of locks, you must acquire and then release the lock on the same thread or bad things might happen.&lt;/p&gt;

&lt;p&gt;Semaphores have no such restriction — the atomicity of accessing and changing that integer value inside is part of the semaphore itself. That means you can &lt;code&gt;wait&lt;/code&gt; and &lt;code&gt;signal&lt;/code&gt; from different threads if that makes sense in your code.&lt;/p&gt;

&lt;h3&gt;Accessors&lt;/h3&gt;

&lt;p&gt;Locks often include API for &lt;em&gt;peeking&lt;/em&gt;, or checking if the lock is available or not.&lt;/p&gt;

&lt;p&gt;Semaphores usually have no way to read the value of the integer stored inside; they only offer the &lt;code&gt;wait()&lt;/code&gt; and &lt;code&gt;signal()&lt;/code&gt; functions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://linux.die.net/man/7/sem_overview"&gt;POSIX semaphores&lt;/a&gt; are a notable exception here, as they include a &lt;code&gt;sem_getvalue&lt;/code&gt; function to read the value. Semaphores in libdispatch have no way to do this.&lt;/p&gt;

&lt;h2&gt;Closing Words&lt;/h2&gt;

&lt;p&gt;Semaphores offer an interesting way to control access to a limited resource in a thread-safe way.&lt;/p&gt;

&lt;p&gt;Compared to other techniques such as delegation, semaphores are decoupled. The waiting task and signaling task know nothing about each other since the messaging happens through the semaphore.&lt;/p&gt;

&lt;p&gt;The communication happens just like a flag semaphore: signaling out there to the world, and waiting patiently for a response back.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/semaphore-a.png" class="" width="192" height="168" alt="Example flag semaphore"&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;I&amp;rsquo;m going into meetup and conference semi-retirement, so this will be my last public talk for a while. Thanks to the organizers (past and present) of tacow for the amazing community they&amp;rsquo;ve shepherded, and the always wonderful attendees for listening and making me miss being back home.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://greenteapress.com/wp/semaphores/"&gt;Allen B. Downey, &amp;ldquo;The Little Book of Semaphores&amp;rdquo;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.cs.utexas.edu/~EWD/transcriptions/EWD01xx/EWD123.html#4.%20The%20General%20Semaphore."&gt;Edsger W. Dijkstra, &amp;ldquo;Cooperating sequential processes&amp;rdquo;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/dispatch/dispatchsemaphore"&gt;Apple Developer Documentation: DispatchSemaphore&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
  <entry>
    <title>Concurrency From the Ground Up</title>
    <link rel="alternate" href="/blog/360idev-2018-concurrency/"/>
    <id>/blog/360idev-2018-concurrency/</id>
    <published>2018-08-27T15:30:00-07:00</published>
    <updated>2018-08-27T15:30:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;div class="captioned"&gt;
&lt;img src="/images/blog/360c-title.jpg" class="bordered" width="720" height="405" alt="Concurrency from the ground up"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;As the title suggests, I want to look at concurrency from the ground up. Start from the basics: rather than queues and groups, pthreads and locks.&lt;/p&gt;

&lt;p&gt;Why? GCD queues are complicated things, and I think understanding how the simpler, smaller pieces work can help.&lt;/p&gt;

&lt;h2&gt;Overall Goals&lt;/h2&gt;

&lt;p&gt;Why do we even care about concurrency? I think it&amp;rsquo;s an important tool in the toolbox to becoming better programmers and writing better programs.&lt;/p&gt;

&lt;p&gt;And by writing better programs, I&amp;rsquo;m thinking of two things in particular:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Programs that seem faster to the user&lt;/li&gt;
&lt;li&gt;Programs that are easy to understand for the programmer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Programs only need to &lt;em&gt;seem&lt;/em&gt; faster to users, but they really do need to &lt;em&gt;be&lt;/em&gt; easier to understand for programmers.&lt;/p&gt;

&lt;h2&gt;The How&lt;/h2&gt;

&lt;p&gt;Next, &lt;em&gt;how&lt;/em&gt; are we going to do these things? With concurrency, we hope. We can look at three aspects in particular:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Structuring our programs better.&lt;/strong&gt; Programming as all about bringing structure to our ideas—that&amp;rsquo;s literally what code is. And concurrency as all about structures too, as we&amp;rsquo;ll discover.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thinking in terms of tasks.&lt;/strong&gt; Once our programs are well-structured, we can get down to organizing on the code level. This means thinking about smaller tasks, threads of execution, and getting speed-ups thanks to multi-core hardware.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Protecting resources.&lt;/strong&gt; Finally, we need a mechanism for managing and protecting resources. We&amp;rsquo;re dealing with systems with limits—memory, CPU time, etc.—and we can look at &lt;strong&gt;locks&lt;/strong&gt; as the concurrency primitive that helps us with resource management.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Concurrency &amp;amp; Structure&lt;/h2&gt;

&lt;p&gt;We use the word &amp;ldquo;concurrency&amp;rdquo; a lot when discussing things ranging from general performance, to doing more than one thing at once, to where to do UI work, to multi-core processors, and so on.&lt;/p&gt;

&lt;p&gt;We have two words that we should try to understand better: &lt;em&gt;concurrency&lt;/em&gt; and &lt;em&gt;parallelism&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here are some great quotes from &lt;a href="https://blog.golang.org/concurrency-is-not-parallelism"&gt;a talk by Rob Pike&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;concurrency is the composition of independently executing processes&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So concurrency is about &lt;em&gt;composition&lt;/em&gt; — composing things, these independently executing process things, into something else. It&amp;rsquo;s about organization and structure: how can we organize these little bits of work together into some cohesive whole?&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the quote about parallelism:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;parallelism is the simultaneous execution of (possibly related) computations&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That seems simpler to me. It&amp;rsquo;s about simultaneous execution. Doing more than one thing at the same time.&lt;/p&gt;

&lt;p&gt;To sum up:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;Concurrency is about dealing with lots of things at once.
Parallelism is about doing lots of things at once.&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Within this context, parallelism is outside the scope of things I need to do. I&amp;rsquo;m not the one doing lots of things at once. That&amp;rsquo;s the computer&amp;rsquo;s job, or the CPU, or the scheduler, or the operating system, or whatever.&lt;/p&gt;

&lt;p&gt;Our job as programmers is the first part: making sure programs are structured in a way that lots of things at once are &lt;em&gt;dealt with&lt;/em&gt; properly. We write programs with proper concurrency so the system can run things in parallel.&lt;/p&gt;

&lt;h3&gt;Decomposability&lt;/h3&gt;

&lt;p&gt;Another way to think about concurrency involves &lt;em&gt;decomposability&lt;/em&gt; — having a large operation, and then breaking it up into pieces that are order-independent or partially ordered.&lt;/p&gt;

&lt;p&gt;Maybe you&amp;rsquo;ve done this kind of refactoring before, where you have a lot of lines of code:&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/360c-code.jpg" class="bordered" width="360" height="255" alt="Lots of lines of code"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;and you break them down into smaller functions:&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/360c-functions.jpg" class="" width="537" height="414" alt="Code split up into functions"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;Then you can replace the original code with a call to each function in turn.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kt"&gt;C&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the easiest case, this is literally a cut and paste job. When we think about decomposability, the easy case is if each of these new sub-functions is completely independent.&lt;/p&gt;

&lt;p&gt;What do we mean by independent?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;There&amp;rsquo;s a question of order: if we don&amp;rsquo;t care which of two tasks happens first, then maybe they&amp;rsquo;re independent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There&amp;rsquo;s shared state: if two tasks have no shared state, then maybe they&amp;rsquo;re independent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mutable state throws a wrench into things: if a task mutates or generates some data, then we might need to start worrying about order.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If two units of work are order-independent then just throw them into the hopper and who cares when they happen?&lt;/p&gt;

&lt;p&gt;If the units are &lt;strong&gt;order-dependent&lt;/strong&gt;, then what? How do we ensure they&amp;rsquo;ll execute in the correct order?&lt;/p&gt;

&lt;h2&gt;Tasks &amp;amp; Threads&lt;/h2&gt;

&lt;p&gt;So we&amp;rsquo;ve broken our program down into smaller pieces. We&amp;rsquo;re thinking about tasks, and ordering.&lt;/p&gt;

&lt;p&gt;And now we&amp;rsquo;re wondering, what mechanisms are there for us to handle these things? Once I&amp;rsquo;ve modeled my program into these small functions, how do I model the dependencies or the independence and parallelizeability between them?&lt;/p&gt;

&lt;p&gt;We have threads and the pthread library available on Apple platforms to help us with this. The pthread library is a C library and is pretty low level; we probably deal with higher-level abstractions in our day-to-day lives but since we&amp;rsquo;re looking at concurrency from the ground up, let&amp;rsquo;s look at pthreads.&lt;/p&gt;

&lt;h3&gt;Live Coding Notes&lt;/h3&gt;

&lt;p&gt;The pthreads API needs functions with a certain signature:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;unused&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UnsafeMutableRawPointer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;UnsafeMutableRawPointer&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The function needs to take what would be a  &lt;code&gt;void *&lt;/code&gt; in C, and return one as well. In Swift that&amp;rsquo;s represented by &lt;code&gt;UnsafeMutableRawPointer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our plan is to run two things in parallel, by setting up two tasks: one to show an activity indicator, and one to do the actual work.&lt;/p&gt;

&lt;p&gt;Within the work thread, we can spin off additional threads to call our more specific functions.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/360c-threads.png" class="" width="579" height="303" alt="Threads with functions, spawning other threads with functions"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;The basics of pthreads:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You create a thread and pass it a function pointer. The thread begins executing right away.&lt;/li&gt;
&lt;li&gt;You &lt;em&gt;join&lt;/em&gt; a thread with &lt;code&gt;pthread_join&lt;/code&gt;, which is a blocking call that will wait for the thread to complete.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Live coding is hard to reproduce here; you should check out the video when it becomes available.&lt;/em&gt; 😆&lt;/p&gt;

&lt;p&gt;You can &lt;a href="https://gist.github.com/gregheo/916c125efcc64df387a372b517555d9d"&gt;browse a gist with the final code from this demo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Locks &amp;amp; Resources&lt;/h2&gt;

&lt;p&gt;So we started with thinking about structure, and then breaking down code into functions.&lt;/p&gt;

&lt;p&gt;If we use threads to model tasks, and we have tools like &lt;em&gt;joining&lt;/em&gt; a thread to wait for it to complete, and we can cancel threads, then how do we model our concurrency needs around data and resources?&lt;/p&gt;

&lt;p&gt;Locks are a super simple general-purpose thing for protecting resources. They&amp;rsquo;re like binary on/off switches that you use to ensure you&amp;rsquo;re not stepping on anyone else&amp;rsquo;s toes.&lt;/p&gt;

&lt;p&gt;Locks are also cooperative — they&amp;rsquo;re like leaving a note somewhere saying &amp;ldquo;please don&amp;rsquo;t touch this until I say so&amp;rdquo; but the API only covers leaving that note and then removing it. It&amp;rsquo;s your responsibility to ensure the code takes heed of these notes.&lt;/p&gt;

&lt;p&gt;Or, you can think of them like having a room full of people but you only want one person to speak at one time. So you have some kind of token, and you have to first acquire the token, speak, and then release or relinquish the token.&lt;/p&gt;

&lt;h3&gt;Live Coding Notes&lt;/h3&gt;

&lt;p&gt;You have the concept of &lt;em&gt;acquiring&lt;/em&gt; a lock. That&amp;rsquo;s like leaving the note or acquiring the token.&lt;/p&gt;

&lt;p&gt;Then you do whatever work you need to do.&lt;/p&gt;

&lt;p&gt;Then you release, or unlock the lock.&lt;/p&gt;

&lt;p&gt;That&amp;rsquo;s it!&lt;/p&gt;

&lt;p&gt;Thinking about usage, you can imagine several ways to use locks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Locks as signals&lt;/li&gt;
&lt;li&gt;Locks as mutual exclusion&lt;/li&gt;
&lt;li&gt;Stringing together multiple locks to make a semaphore&lt;/li&gt;
&lt;li&gt;Using locks to sequence code together into a quasi-queue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can &lt;a href="https://gist.github.com/gregheo/25b7fc0ba68020078cff926ed59079dc"&gt;browse a gist with the final code from this demo&lt;/a&gt;, where we looked at locks as signals and locks as mutexes.&lt;/p&gt;

&lt;h2&gt;Concluding Thoughts&lt;/h2&gt;

&lt;p&gt;Locks and threads are the concurrency primitives I think are most useful to understand how higher level concepts and APIs work.&lt;/p&gt;

&lt;p&gt;But understanding and applying the concepts of concurrency means pulling together the specific needs of your application, structuring it correctly, and using the correct means to enable high performance.&lt;/p&gt;

&lt;p&gt;There are a lot of APIs around locks, threads, queues, etc. available to us on the platform.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/360c-apis.png" class="" width="686" height="360" alt="Some APIs around concurrency"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;I hope this talk and article gives you some new things to research, and helps make you more thoughtful when thinking about structure and concurrency primitives when using those higher-level APIs. 😄&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Getting Under Swift's Skin @ NSMeetup</title>
    <link rel="alternate" href="/blog/nsmeetup-3swift/"/>
    <id>/blog/nsmeetup-3swift/</id>
    <published>2018-02-07T17:00:00-08:00</published>
    <updated>2018-02-07T17:00:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;I gave a talk &lt;em&gt;Getting Under Swift’s Skin&lt;/em&gt; at &lt;a href="https://www.meetup.com/nsmeetup/events/247094493/"&gt;NSMeetup&lt;/a&gt;, where I went over three ways to go a bit deeper in working with Swift:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Swift language itself, lowered to SIL&lt;/li&gt;
&lt;li&gt;Looking at the Swift standard library in the debugger and with custom toolchains&lt;/li&gt;
&lt;li&gt;Examining runtime instances with reflection&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Why would you want to do these things? It can be fun to go below the surface and see how things are made. You can improve your debugging skills and get a greater understanding of the system and runtime for your apps.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/nsmeetup-2018-jsq.jpg" width="600" height="414" alt="Let's go one more level down: wise words from a sometimes wise man"&gt;&lt;/a&gt;
&lt;p class="caption"&gt;Wise words from a sometimes wise man&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;As a wise man once said: let&amp;rsquo;s go one more level down.&lt;/p&gt;

&lt;p&gt;The following are some notes and links for more information on things mentioned in the talk.&lt;/p&gt;

&lt;h2&gt;Swift and SIL&lt;/h2&gt;

&lt;p&gt;Basic SIL characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Locals start with &lt;code&gt;%&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Globals start with &lt;code&gt;@&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Static_single_assignment_form"&gt;Static single assignment&lt;/a&gt;, where variables are named with incrementing numbers and are set only once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To see the SIL representation of your Swift source:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;swiftc &lt;span class="nt"&gt;-emit-sil&lt;/span&gt; source.swift
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To demangle symbols, run the &lt;code&gt;swift-demangle&lt;/code&gt; utility:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;xcrun swift-demangle
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;More reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://github.com/apple/swift/blob/master/docs/SIL.rst"&gt;reference guide to SIL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=Ntj8ab-5cvE"&gt;Swift&amp;rsquo;s High-Level IR: A Case Study of Complementing LLVM IR with Language-Specific Optimization&lt;/a&gt; — talk by &lt;a href="https://twitter.com/jckarter"&gt;Joe Groff&lt;/a&gt; and &lt;a href="https://twitter.com/clattner_llvm"&gt;Chris Lattner&lt;/a&gt; at the 2015 LLVM Developers’ Meeting&lt;/li&gt;
&lt;li&gt;&lt;a href="https://godbolt.org"&gt;Compiler explorer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Standard Library&lt;/h2&gt;

&lt;p&gt;More reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/apple/swift/tree/master/stdlib"&gt;Swift Standard library source code&lt;/a&gt; on GitHub&lt;/li&gt;
&lt;li&gt;Ole Begemann’s guide to &lt;a href="https://oleb.net/blog/2016/10/swift-stdlib-source/"&gt;working with the standard library source&lt;/a&gt;, including how to build it&lt;/li&gt;
&lt;li&gt;Greg’s articles on &lt;a href="https://swiftunboxed.com"&gt;swiftunboxed.com&lt;/a&gt;, including ones about the standard library&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Reflection&lt;/h2&gt;

&lt;p&gt;More reading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/swift/debugging_and_reflection"&gt;Debugging and Reflection&lt;/a&gt; reference&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Zewo/Reflection"&gt;Advanced Swift reflection from Zewo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/objectivec/objective_c_runtime?language=objc"&gt;Objective-C Runtime documentation&lt;/a&gt;: function reference for things including method swizzling, direct ivar access, querying the class of an object, setting the class of an object, etc.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.skilled.io/u/swiftsummit/swift-s-reflective-underpinnings-joe-groff"&gt;Swift’s Reflective Underpinnings&lt;/a&gt; — talk by &lt;a href="https://twitter.com/jckarter"&gt;Joe Groff&lt;/a&gt; at Swift Summit 2017.&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
  <entry>
    <title>Five Unbelievable Secrets of Reactive Programming the Experts Don't Want You to Know!</title>
    <link rel="alternate" href="/blog/reactive-programming-basics/"/>
    <id>/blog/reactive-programming-basics/</id>
    <published>2017-02-23T15:00:00-08:00</published>
    <updated>2017-02-23T15:00:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;&lt;em&gt;Based on a talk given at &lt;a href="http://www.playgroundscon.com"&gt;Playgrounds&lt;/a&gt; in Melbourne, February 24, 2017.&lt;/em&gt;&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/playgrounds.png" class="" width="469" height="256"&gt;
&lt;/div&gt;

&lt;p&gt;I was asked to come up with a &lt;a href="https://twitter.com/playgroundscon/status/811685860110307328"&gt;super click-baity title&lt;/a&gt; for the talk and here it is: &lt;strong&gt;Five Unbelievable Secrets of Reactive Programming the Experts Don&amp;rsquo;t Want You to Know!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(Exactly &lt;em&gt;why&lt;/em&gt; the experts don&amp;rsquo;t want you to know these things is still unclear to me.)&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve heard it said about learning Lisp or Prolog or functional programming — even though you might never use Lisp again, learning the language and how it works will (supposedly) change the way you think about writing code and change the way you design programs.&lt;/p&gt;

&lt;p&gt;Does the same thing apply to reactive programming? It did for me; writing apps with RxSwift highlighted some best practices and architectural choices that I already knew about, but are much more at the forefront when thinking in the reactive style.&lt;/p&gt;

&lt;p&gt;These aren&amp;rsquo;t actually &amp;ldquo;secrets&amp;rdquo; per se, but more like &amp;ldquo;big picture ideas&amp;rdquo; that solidified in my head as I learned more about reactive programming. Read on to find out what they are!&lt;/p&gt;

&lt;h2&gt;1. You already know it&lt;/h2&gt;

&lt;p&gt;The good news is you already know the pieces that make up reactive programming.&lt;/p&gt;

&lt;p&gt;If you’re writing iOS apps in the &amp;ldquo;traditional&amp;rdquo; Cocoa style and you learn about functional programming, you already know what a function is. It&amp;rsquo;s more about learning a new style, a new way of thinking, and perhaps some new words such as everyone&amp;rsquo;s favorite, &amp;ldquo;monad&amp;rdquo;.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/rx-venn.png" class="" width="1280" height="720"&gt;
&lt;/div&gt;

&lt;p&gt;Same goes for reactive programming: you&amp;rsquo;ll see old concepts like sequences, closures, and &lt;code&gt;map&lt;/code&gt; used in familiar but novel ways. The way they&amp;rsquo;re put together, and the language used to describe it may be new but the building blocks are the same.&lt;/p&gt;

&lt;h2&gt;2. Everything is a sequence&lt;/h2&gt;

&lt;p&gt;The first thing I think about when designing an app in the reactive style is: what are the sequences? I like to split them into two big categories, inputs and outputs.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/rx-sequences.png" class="" width="1246" height="524"&gt;
&lt;/div&gt;

&lt;p&gt;Thanks to extensions on common classes provided by RxCocoa, you get many of these sequences, or &lt;strong&gt;observables&lt;/strong&gt; included out of the box.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;UISlider&lt;/code&gt; has an observable providing a sequence of &lt;code&gt;Float&lt;/code&gt; values. &lt;code&gt;UIButton&lt;/code&gt; has an observable that fires every time the button is tapped.&lt;/p&gt;

&lt;p&gt;You can make your own observables too. I wrote one for a Mac app, to keep track of mouse clicks. &lt;code&gt;NSViewController&lt;/code&gt; receives a &lt;code&gt;mouseDown(with:event)&lt;/code&gt;method call whenever there&amp;rsquo;s a mouse click on the view. You can have RxCocoa listen for that method call, and turn that into an observable:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sentMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;#selector(&lt;/span&gt;&lt;span class="nf"&gt;NSResponder.mouseDown(with:)&lt;/span&gt;&lt;span class="kd"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;NSPoint&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="k"&gt;as?&lt;/span&gt; &lt;span class="kt"&gt;NSEvent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;locationInWindow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case, the first line sets up an observable that produces a value every time &lt;code&gt;mouseDown(with:)&lt;/code&gt; gets called. Then we can &lt;code&gt;map&lt;/code&gt; over that observable (which remember, is just a sequence) and produce an output observable of &lt;code&gt;NSPoint&lt;/code&gt; values.&lt;/p&gt;

&lt;h2&gt;3. Small pieces of logic&lt;/h2&gt;

&lt;p&gt;In the example above, we had a sequence of events that needed to be transformed into a sequence of points. Each of these transformations pushes the data through your app.&lt;/p&gt;

&lt;p&gt;Within the big box that is your application, there are smaller sequences within.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/rx-subsequences.png" class="" width="1246" height="524"&gt;
&lt;/div&gt;

&lt;p&gt;Each step along the way is its own operation, looking something like another &lt;code&gt;map&lt;/code&gt; with a closure. This structure means your programs can be made of small, discrete pieces of logic rather than monolithic massive view controllers.&lt;/p&gt;

&lt;h2&gt;4. Declarative style&lt;/h2&gt;

&lt;p&gt;With all these sequences floating around, how do you get them connected together to perform useful work? Up until now, we’ve only seen sequences transformed into other sequences.&lt;/p&gt;

&lt;p&gt;Another useful thing you can do with observables is &lt;em&gt;subscribe&lt;/em&gt; to them. That means whenever there’s a new value available, or something else happens such as an error or the observable completes, you can provide a closure to react to it.&lt;/p&gt;

&lt;p&gt;In the simplest case, say you have an observable that produces strings coming in from some source such as the network. You could subscribe to this observable, and set the value of a label.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;someStringObservable&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;onNext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;stringValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stringValue&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As a shortcut, you could use the &lt;em&gt;bind&lt;/em&gt; function and take advantage of more RxCocoa extensions:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;someStringObservable&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bindTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The magic &lt;code&gt;rx&lt;/code&gt; property provides properties that can be read or written to. A text field, for instance, can be bound to but you could also subscribe to changes since they accept user input.&lt;/p&gt;

&lt;p&gt;The magic comes once you’ve set up all your observables, maps, subscriptions, and bindings: at that point, you can start up your application and all the pieces are magically connected together. Thanks to the declarative approach, you’ve set up the logic of all the transformations and relationships and can now wait for input to start flowing through.&lt;/p&gt;

&lt;h2&gt;5. Testable code&lt;/h2&gt;

&lt;p&gt;Having logic in small discrete pieces means they also become easy to test.&lt;/p&gt;

&lt;p&gt;Take a very simple example: a class that takes as its input an observable that produces integers:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SomeController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Input observable providing a sequence of integers&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;intObservable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;intObservable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;intObservable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;intObservable&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It has a computed property that transforms the input integers into strings:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;SomeController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Output observable that converts the integers to strings&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;stringConversionObservable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;intObservable&lt;/span&gt;
      &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;intValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;intValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You want to test this out. In your app, &lt;code&gt;intObservable&lt;/code&gt; comes from some place, maybe the network, maybe user input—it doesn’t really matter. All you want to test is the &lt;em&gt;conversion&lt;/em&gt; operation. In this case, the conversion logic is simple but you can imagine having a &lt;code&gt;map&lt;/code&gt; with more complex business logic in there.&lt;/p&gt;

&lt;p&gt;Between a sequence of integers and a sequence of strings, how are we going to mock these things? If only there were some easy way to mock an ordered, iterable sequences of integers, right? 🤔&lt;/p&gt;

&lt;p&gt;At this point, I hope you’re thinking of arrays. Yes, you could take an input &lt;em&gt;array&lt;/em&gt; of integers, push that through the observable, and get an output array of strings:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Test input data&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;// Turn the array into an observable&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;inputObservable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Initialize the controller with our test observable&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;SomeController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;intObservable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inputObservable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Observables are usually asynchronous since you can’t tell when a value will be available ahead of time. Since this is a test with a fixed array, we can turn this into a &lt;em&gt;blocking observable&lt;/em&gt; so it waits for the &lt;code&gt;map&lt;/code&gt; to complete synchronously.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Thanks to the toArray() call, this will be an regular array of strings&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try!&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stringConversionObservable&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBlocking&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then it’s a simple matter of making sure the output matches what you expected.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;expectedOutput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"42"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kt"&gt;XCTAssert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;expectedOutput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Done! The nice thing about having inputs and outputs modeled as sequences is that when we need to mock them in tests, it’s very easy to do so thanks to arrays.&lt;/p&gt;

&lt;h2&gt;Beyond the five secrets&lt;/h2&gt;

&lt;p&gt;After learning the basics of reactive programming, these &amp;ldquo;five secrets&amp;rdquo; encapsulate some big ideas about software architecture and how I want to structure my code. I’m thinking more in terms of streams of data and small blocks of testable code.&lt;/p&gt;

&lt;p&gt;If you want to learn more about RxSwift, I&amp;rsquo;d suggest the following resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md"&gt;RxSwift Getting Started page&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://rx-marin.com"&gt;rx-marin&lt;/a&gt; — excellent articles with lots of RxSwift example code&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://reactivex.io"&gt;ReactiveX.io&lt;/a&gt; — language- and platform-neutral guide to the Rx style of programming&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you were at the Playgrounds talk and want to check out some resources from the talk:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sample Noughts &amp;amp; Crosses game &lt;a href="https://github.com/gregheo/NoughtsAndCrosses"&gt;source code&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://speakerdeck.com/gregheo/five-unbelievable-secrets-of-reactive-programming-the-experts-dont-want-you-to-know"&gt;Slides from the talk&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
  <entry>
    <title>RxSwift @ NSMeetup</title>
    <link rel="alternate" href="/blog/nsmeetup-rx/"/>
    <id>/blog/nsmeetup-rx/</id>
    <published>2017-02-02T06:00:00-08:00</published>
    <updated>2017-02-02T06:00:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;As someone formerly in the training business, I&amp;rsquo;m often thinking about the best way to explain things. Something I really enjoy doing is attending talks from beginners — it&amp;rsquo;s always interesting to hear something you already know (or think you already know!) explained by someone coming into it with fresh eyes.&lt;/p&gt;

&lt;p&gt;I started looking into reactive programming and RxSwift late last year, and now it was my turn to be the noob explaining something at a meetup.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/rx-nsmeetup.jpg" class="bordered" width="800" height="400"&gt;
&lt;/div&gt;

&lt;p&gt;Reactive programming is one more thing in the development toolbox. It&amp;rsquo;s not necessarily something that takes over an entire project; you can use reactive programming elements in just parts of an application if you want.&lt;/p&gt;

&lt;p&gt;What are the big ideas of reactive programming? We can turn to Wikipedia for the answer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data flows&lt;/li&gt;
&lt;li&gt;Propagation of change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thinking about your app in terms of &lt;em&gt;data&lt;/em&gt; — what&amp;rsquo;s coming in, what&amp;rsquo;s going out, how do you manage it going from one place to the other — is the core of it for me. Then it&amp;rsquo;s a matter of pretending you&amp;rsquo;re a series of tubes, like the Internet, to manage, tranform, and push the data along.&lt;/p&gt;

&lt;h2&gt;Resources&lt;/h2&gt;

&lt;p&gt;For the talk, I used a simple app where you provide the angles, and it draws triangles.&lt;/p&gt;

&lt;p&gt;You can see the &lt;a href="https://github.com/gregheo/Triangles"&gt;code from the demo on GitHub&lt;/a&gt;; there&amp;rsquo;s one folder with the classic Cocoa UIKit version, and one with the RxSwift version coded up during the talk.&lt;/p&gt;

&lt;p&gt;Here are the slides:&lt;/p&gt;

&lt;script async class="speakerdeck-embed" data-id="5096ed191d0b41368a4b75563cf632aa" data-ratio="1.77777777777778" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;

&lt;p&gt;And finally, if you&amp;rsquo;re interested in reading more there are a ton of places to look:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For general theory on Rx: &lt;a href="http://reactivex.io"&gt;ReactiveX.io&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Why.md"&gt;Why use Rx?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md"&gt;The official RxSwift Getting Started guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;There were a lot of questions about dispose bags that I couldn&amp;rsquo;t answer 😇 — you can read more about them in the &lt;a href="https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md#disposing"&gt;Disposing section of the guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;One of the best Rx blogs with lots of useful code examples: &lt;a href="http://rx-marin.com"&gt;rx_marin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Finally, right in the RxSwift repository is the &lt;a href="https://github.com/ReactiveX/RxSwift/tree/master/RxExample"&gt;RxExample&lt;/a&gt; folder. Don&amp;rsquo;t miss it when you clone the repository locally and start exploring!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I really enjoy the data flow model and declarative nature of Rx, and I hope to continue poking around and learning. Happy reactive programming adventures to you!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>The Year of Travel</title>
    <link rel="alternate" href="/blog/year-of-travel-2016/"/>
    <id>/blog/year-of-travel-2016/</id>
    <published>2017-01-16T17:00:00-08:00</published>
    <updated>2017-01-16T17:00:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;I haven&amp;rsquo;t done much travelling in my life, but 2016 turned out to be the Year of Travel for me. I&amp;rsquo;ve flown about 100,000 lifetime miles, and 56,000 of them were in 2016.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the year of travel, by the numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Miles flown: 56006&lt;/li&gt;
&lt;li&gt;Transcontinental flights: 11&lt;/li&gt;
&lt;li&gt;Transatlantic flights: 2&lt;/li&gt;
&lt;li&gt;Transpacific flights: 2&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="captioned"&gt;
&lt;img class="" src="/images/blog/2016-airplanes.jpg"&gt;
&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Number of times in Chicago without leaving the airport: 2&lt;/li&gt;
&lt;li&gt;Number of times in Houston without leaving the airport: 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sorry Chicago and Houston; some day I will step outside and visit!&lt;/p&gt;

&lt;h2&gt;Conference Travel&lt;/h2&gt;

&lt;p&gt;Pretty much all of my travel was thanks to speaking at conferences.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img class="" src="/images/blog/2016-new.jpg"&gt;
&lt;/div&gt;

&lt;p&gt;New countries visited:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🇨🇳 China&lt;/li&gt;
&lt;li&gt;🇮🇪 Ireland (airport only, sorry!)&lt;/li&gt;
&lt;li&gt;🇬🇧 U.K.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Exciting cities in the USA visited:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🇺🇸 D.C.&lt;/li&gt;
&lt;li&gt;🎸 Nashville&lt;/li&gt;
&lt;li&gt;👓 San Jose&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Relocation&lt;/h2&gt;

&lt;p&gt;The bigger bit of travel (and the reason I have an odd number of transcons) was moving to San Francisco in March.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img class="" src="/images/blog/2016-sf.jpg"&gt;
&lt;/div&gt;

&lt;p&gt;I keep a journal in Day One and it&amp;rsquo;s strange to me to write about being in Toronto and filing it under &amp;ldquo;Travel&amp;rdquo;. And when I write about some new thing in San Francisco, that&amp;rsquo;s just &amp;ldquo;Journal&amp;rdquo; I guess. 😄&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img class="" src="/images/blog/2016-toronto.jpg"&gt;
&lt;/div&gt;

&lt;p&gt;There were many, many &lt;a href="https://www.flickr.com/photos/gregheo/albums/72157677257307252"&gt;hills&lt;/a&gt; &amp;amp; &lt;a href="https://www.flickr.com/photos/gregheo/albums/72157665980027334"&gt;staircases&lt;/a&gt; climed in San Francisco in 2016. Luckily there are many more to visit in 2017 and beyond!&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img class="" src="/images/blog/2016-stairs.jpg"&gt;
&lt;/div&gt;
</content>
  </entry>
  <entry>
    <title>Swift Summit SF 2016</title>
    <link rel="alternate" href="/blog/swiftsummit-sf-2016/"/>
    <id>/blog/swiftsummit-sf-2016/</id>
    <published>2016-11-13T14:00:00-08:00</published>
    <updated>2016-11-13T14:00:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;I had another great time at Swift Summit in San Francisco this year. For a Swift nerd, there&amp;rsquo;s nothing like being surrounded by so many thoughtful speakers and attendees for an all-Swift single-track conference over two days.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/sssf2016.jpg" width="820" height="506"&gt;
&lt;p class="caption wp-caption-text"&gt;On stage at Swift Summit. Thanks to Erik Kerber for the photo!&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Thanks to everyone who stopped to say hello! And a pro tip for other conference speakers out there: have something interesting to say (perhaps one of those &amp;ldquo;elevator pitches&amp;rdquo;) about your talk since people will inevitably ask you what you’re going to be speaking about. Apologies to folks who asked me before I had a coherent answer.&lt;/p&gt;

&lt;p&gt;On a personal note, my Swift Summit trip last year was the start of talking to more folks at Facebook and led to me joining the company and the team at Instagram. Now I live in the city and work in the Bay Area and it seems like so much has changed in only a year.&lt;/p&gt;

&lt;p&gt;Last year I spoke about &lt;a href="https://swiftunboxed.com/protocols/swift-standard-library-protocols-lessons/"&gt;protocols in the standard library&lt;/a&gt; and how we could use them to guide us on using them in our own code. Looking back on it now, I can see that my talk this year had a similar theme: examining standard library source code (now that Swift is open-source!) to guide us toward better coding practices in general.&lt;/p&gt;

&lt;h2&gt;Open-Source Swift&lt;/h2&gt;

&lt;p&gt;After last year’s talk, I thought it would be fun to go through each protocol and write about it. I got through &lt;a href="https://swiftunboxed.com/protocols/"&gt;a grand total of two&lt;/a&gt; before life got in the way.&lt;/p&gt;

&lt;p&gt;Then Swift was open-sourced and there were even more distractions! So much code to read and commit history to browse through!&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img class="bordered" src="/images/blog/swift-commit.png" width="415" height="272"&gt;
&lt;p class="caption wp-caption-text"&gt;First commit to the Swift repository&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;I started reading through bit of code at random, mostly at parts of the standard library. I remember checking out the &lt;a href="https://opensource.apple.com/source/CF/"&gt;CFLite&lt;/a&gt; project and the &lt;a href="https://github.com/gnustep/base/tree/master/Source"&gt;GNUStep source code&lt;/a&gt; back in the day for something similar — GNUStep includes their own implementations of classes you know and love such as &lt;a href="https://github.com/gnustep/base/blob/master/Source/NSDate.m"&gt;NSDate&lt;/a&gt; and &lt;a href="https://github.com/gnustep/base/blob/master/Source/NSIndexPath.m"&gt;NSIndexPath&lt;/a&gt; and many others. Although this isn’t Apple source code here, it’s still an implementation of the same set of APIs.&lt;/p&gt;

&lt;h2&gt;On Reading&lt;/h2&gt;

&lt;p&gt;While reading all this Swift, I found myself thinking more about how I was writing code and also picking up small tricks from what I was reading. One benefit of standard library code is it comes from people who have the most experience with such a young language. You know, that exclusive club of folks who have the &amp;ldquo;3–5 years of experience with Swift&amp;rdquo; all the recruiters are looking for. 😉&lt;/p&gt;

&lt;p&gt;Again, I managed to get &lt;a href="https://swiftunboxed.com/open-source/Bool/"&gt;one thing written about Bool.swift&lt;/a&gt; before regular life got in the way again. I really enjoyed doing that kind of close reading of a simple-looking bit of source code and writing about it. I did more research and wrote some drafts on optionals and collections and strings over the last few months, but never managed to finish anything.&lt;/p&gt;

&lt;p&gt;And that’s where this year’s Swift Summit talk came from. I learned so much from reading more code and making that a habit, and I wanted to share that experience.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img class="bordered" src="/images/blog/sssf-think.png" width="700" height="250"&gt;
&lt;/div&gt;

&lt;p&gt;If you were at the conference and attended my talk, I hope you enjoyed it! If you weren’t there, there will be a video posted soon enough. And if you &lt;em&gt;were&lt;/em&gt; there and didn’t enjoy it, I’m always looking for feedback if you’re willing to take the time to offer it.&lt;/p&gt;

&lt;p&gt;I’m hoping to get back to writing more often as all my Swift reading notes continue to pile up. If you’ve started reading more code too, I encourage you to write about it as well!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>The Case of the Disappearing Apps</title>
    <link rel="alternate" href="/blog/disappearing-apps/"/>
    <id>/blog/disappearing-apps/</id>
    <published>2016-06-19T10:14:00-07:00</published>
    <updated>2016-06-19T10:14:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;For me, the killer app of the Apple Watch has always been notifications. A well-designed and usable dynamic notification is worth its weight in (rose) gold. Even if the app doesn’t provide a custom-built notification, the system-provided one is often good enough to get the information at a glance.&lt;/p&gt;

&lt;p&gt;Apps on the watch are effectively &lt;strong&gt;invisible&lt;/strong&gt; — I never launch them, but use them indirectly for their notifications and whatever work they need to do in the background.&lt;/p&gt;

&lt;p&gt;On all platforms — iOS, macOS, and watchOS — I often interact directly with the rich notification. This is where you can reply or perform some other action without having to launch the full app.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/message-notification.png" width="358" height="162"&gt;
&lt;/div&gt;

&lt;p&gt;It&amp;rsquo;s like the app disappears. I get notified, then perform an action. In many cases, with iMessage as the obvious example, the notification &lt;strong&gt;is&lt;/strong&gt; the app.&lt;/p&gt;

&lt;h2&gt;Conservation of energy&lt;/h2&gt;

&lt;p&gt;The app is still there, of course, doing its work in the background. It can’t &lt;em&gt;completely&lt;/em&gt; disappear; it just appears to. But the interaction is through the notification, rather than some UI the developer came up with. What if we continue down that path, to a brave new world of almost appless apps?&lt;/p&gt;

&lt;p&gt;Then my mind turns to custom keyboards and content blockers on iOS, which for seemingly legacy reasons still need to be inside an app bundle. This seems totally unnecessary — they should just install and show up in the Settings app if they have settings to tweak. No app on the home screen needed.&lt;/p&gt;

&lt;p&gt;Now that Apple has even &lt;em&gt;more&lt;/em&gt; extension points into the operating system, how many more apps can disappear?&lt;/p&gt;

&lt;h2&gt;Using the app without using the app&lt;/h2&gt;

&lt;p&gt;With widgets (formerly known as &amp;ldquo;Today extensions&amp;rdquo;) now appearing on the lock screen and by force touching the app icon, you’ll be able to &lt;a href="http://appleinsider.com/articles/16/06/13/first-look-widgets-escape-notification-center-arrive-on-lock-home-screens-in-ios-10"&gt;provide some information at a glance to your users&lt;/a&gt;. That covers the &amp;ldquo;pull&amp;rdquo; half of things, where users reach out and ask for information.&lt;/p&gt;

&lt;p&gt;On the &amp;ldquo;push&amp;rdquo; half, there are the already-discussed notifications. New in iOS 10 are &lt;a href="https://techcrunch.com/2016/06/15/our-ios-10-notifications-will-be-a-lot-more-useful-and-relevant/"&gt;custom views&lt;/a&gt; as part of notifications. That means you can move beyond a plain text message and show some custom UI here. The UI is non-interactive though, so you still need to provide those buttons and actions as before.&lt;/p&gt;

&lt;p&gt;Still, that&amp;rsquo;s push and pull on your app without even launching it, not to mention possible Siri integration. That leads to a fundamental usability question for me: &lt;strong&gt;how much can users get out of this app without ever launching it?&lt;/strong&gt;&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/unlaunched-apps.jpg" width="493" height="270"&gt;
&lt;p class="caption wp-caption-text"&gt;Updated but unlaunched apps.&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;One one end are the keyboard apps that I never need to launch. On the other end are apps like Tweetbot that I launch and use all the time. I think the middle of that spectrum will quickly fill up with apps that are totally useful and usable — just usable indirectly via Siri, notifications, and widgets.&lt;/p&gt;

&lt;p&gt;What does that mean for us as app developers? I’m going to start thinking post-Internet humans with short attention spans: provide quick, glanceable, useful bits of information in widgets; take full advantage of &lt;a href="https://developer.apple.com/reference/usernotificationsui"&gt;custom notification views&lt;/a&gt;; and definitely implement &lt;a href="https://developer.apple.com/sirikit/"&gt;SiriKit&lt;/a&gt; if it fits into one of the available categories.&lt;/p&gt;

&lt;p&gt;And the app itself, with its storyboards and view controllers? There’s still a place for that, but it’s now a shrinking space.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Swift Summit SF 2015</title>
    <link rel="alternate" href="/blog/swiftsummit-sf-2015/"/>
    <id>/blog/swiftsummit-sf-2015/</id>
    <published>2015-11-03T13:30:00-08:00</published>
    <updated>2015-11-03T13:30:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;I&amp;rsquo;m back from &lt;a href="https://www.swiftsummit.com/"&gt;Swift Summit SF 2015&lt;/a&gt; with many more Swifty ideas in my head. You can read my recap article about the conference here: &lt;a href="http://www.raywenderlich.com/120096/swift-summit-sf-2015-highlights"&gt;Swift Summit SF 2015 Highlights&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve written up a summary of my talk, &lt;a href="http://swiftunboxed.com/protocols/swift-standard-library-protocols-lessons/"&gt;Lessons From the Swift Standard Library Protocols&lt;/a&gt;, if you&amp;rsquo;d like to read it. Or, just go &lt;a href="http://www.skilled.io/gregheo/what-the-55-swift-standard-library-protocols-taught-me"&gt;straight to the video&lt;/a&gt;!&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;a href="http://swiftunboxed.com/protocols/swift-standard-library-protocols-lessons/"&gt;
&lt;img src="/images/blog/55-title.jpg" width="640" height="360"&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;I always enjoy hanging around the bay area and hearing about all those crazy startups, being shouted at in the mission, and mostly finding raywenderlich.com team members in every corner.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/sssf2015.jpg" width="640" height="360"&gt;
&lt;/div&gt;
</content>
  </entry>
  <entry>
    <title>Audience Interaction in your Conference Talks</title>
    <link rel="alternate" href="/blog/speaking-audience-interaction-conference-talks/"/>
    <id>/blog/speaking-audience-interaction-conference-talks/</id>
    <published>2015-10-23T08:30:00-07:00</published>
    <updated>2015-10-23T08:30:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;Somewhere on the web is the advice to make your talk &amp;ldquo;interactive&amp;rdquo; and get the audience involved. And maybe to take questions too.&lt;/p&gt;

&lt;p&gt;Asking questions and taking questions can be good feedback&amp;hellip;as long as you do it right.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: This applies to technical conference talks, mostly, since that&amp;rsquo;s the kind that I attend; YMMV.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Polling the Audience&lt;/h2&gt;

&lt;p&gt;Here&amp;rsquo;s a tip for any audience interaction: if the talk were recorded, does it still work? With just audio, how does it sound? How does it look on video?&lt;/p&gt;

&lt;p&gt;&lt;img src="/images/blog/mixer.jpg" alt="mixer" /&gt;&lt;/p&gt;

&lt;p&gt;I was at a talk once that felt like a one-way survey: Who&amp;rsquo;s heard of this brand? Who likes this product? Who&amp;rsquo;s used this API? And then who&amp;rsquo;s used that in combination with this other thing? After question five, people stopped putting their hands up.&lt;/p&gt;

&lt;p&gt;In moderation, polling the audience is OK. Here&amp;rsquo;s how you do it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Ask the question. &lt;em&gt;&amp;ldquo;Who here has been on a llama ride in the desert?&amp;rdquo;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Raise your own hand. Pause for people to raise their hands too.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Look around and do a quick count&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Give the report.  &lt;em&gt;&amp;ldquo;OK, thank you. Around ten people, which is average for a group this size&amp;hellip;&amp;rdquo;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;See what I did there? Ask a clear question. Wait. Tell the audience the results.&lt;/p&gt;

&lt;p&gt;&lt;img src="/images/blog/audience-hands.jpg" alt="Hands up" /&gt;&lt;/p&gt;

&lt;p&gt;This looks good on video and also makes sense in an audio-only recording.&lt;/p&gt;

&lt;p&gt;Why raise your own hand? If you&amp;rsquo;re asking an even slightly embarrassing question (&amp;ldquo;who hasn&amp;rsquo;t heard of &lt;em&gt;latest technology X&lt;/em&gt;?&amp;rdquo;), raising your hand—even if that&amp;rsquo;s not your actual answer—makes people less shy.&lt;/p&gt;

&lt;p&gt;And remember, your audience is interested in results too; you&amp;rsquo;re the one at the front of the room who can see all the hands! They&amp;rsquo;re counting on you to report the numbers.&lt;/p&gt;

&lt;h2&gt;Questions Done Right&lt;/h2&gt;

&lt;p&gt;I&amp;rsquo;ll be honest: I usually hate questions at the end of talks.&lt;/p&gt;

&lt;p&gt;Most of the time, it becomes a conversation about some arcane specific thing between the speaker and some random audience member. It&amp;rsquo;s like being forced to sit and listen quietly to a private conversation between two people. The audience starts dozing off or checking Twitter.&lt;/p&gt;

&lt;p&gt;&lt;img src="/images/blog/sleepy-puppy.jpg" alt="Sleepy puppy" /&gt;&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;d rather say, &amp;ldquo;I&amp;rsquo;d love to chat with you outside if you have questions for me, or I&amp;rsquo;ll be here for the rest of the conference. I&amp;rsquo;m very friendly, so please come and say hello and we can talk!&amp;rdquo;&lt;/p&gt;

&lt;p&gt;You&amp;rsquo;ll wrap up a bit early and the audience will appreciate the time to go to the bathroom, get some coffee, check email; the next speaker can start getting set up; those who really have a pressing question will rush to the front to ask you. Everyone wins!&lt;/p&gt;

&lt;p&gt;If you must have questions for whatever reason — the conference requires it, you&amp;rsquo;re really short on material — say something like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;ldquo;Are there any questions that would be interesting for the entire group?&amp;rdquo;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Right there, those magic words will make your audience filter their set of questions. &amp;ldquo;No,&amp;rdquo; they&amp;rsquo;ll think, &amp;ldquo;this question is in fact &lt;em&gt;not&lt;/em&gt; interesting to a wide audience. I will not ask it and make everyone in this room hear about this.&amp;rdquo;&lt;/p&gt;

&lt;p&gt;&lt;img src="/images/blog/audience-crowd.jpg" alt="Big crowd" /&gt;&lt;/p&gt;

&lt;p&gt;If someone still starts out like, &amp;ldquo;in my app &lt;em&gt;Foo&lt;/em&gt;, I have a class &lt;code&gt;Bar&lt;/code&gt; where I sometimes use its convenience initializer, and then in low-memory conditions when it calls my private API, it&amp;hellip;&amp;rdquo; — oh my goodness, please stop them there. Then you say: &amp;ldquo;that sounds like an interesting case specific to your app; how about we step outside and chat for a few minutes about it after this session? Yes? Good. Any other questions that would be interesting for the entire group?&amp;rdquo;&lt;/p&gt;

&lt;h2&gt;Answers Done Right&lt;/h2&gt;

&lt;p&gt;Aim to answer the question in 30 seconds. That&amp;rsquo;s a pretty long time — if you don&amp;rsquo;t believe me, set a timer for 30 seconds and read some documentation out loud.&lt;/p&gt;

&lt;p&gt;When you&amp;rsquo;re up on stage and under pressure to answer, it&amp;rsquo;s easy to ramble. If you practice 30-second answers and get a feel for how much content fits within that time, it&amp;rsquo;ll help you keep your answers to-the-point and concise even if you take a bit longer to answer.&lt;/p&gt;

&lt;p&gt;If you find you have a ton to say based on a single question, make a note and then turn that into another talk or blog post. More content!&lt;/p&gt;

&lt;p&gt;OK, here&amp;rsquo;s what you do to answer a question:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Repeat the question&lt;/strong&gt; so everyone can hear and it makes it onto the recording. You can rephrase the question slightly too, to make it clearer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Answer the question&lt;/strong&gt;. If you don&amp;rsquo;t know the answer, just say you don&amp;rsquo;t know and move on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you stop talking, &lt;strong&gt;take a moment and think about the original question&lt;/strong&gt;. Do you even remember the question? Did you answer it? If not, get clarification if required and answer it in ten seconds, then immediately shut up.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nothing is worse than being in the audience and hearing an answer without having heard the question first. Please please please, don&amp;rsquo;t forget to repeat the question. The audience and conference organizers will love you for it.&lt;/p&gt;

&lt;p&gt;As for step 3: think of politicians who have answers in mind and will make talking-point statements when asked a question. Don&amp;rsquo;t do this—keep focused on the question and make sure you&amp;rsquo;ve answered it.&lt;/p&gt;

&lt;h2&gt;Interactivity&lt;/h2&gt;

&lt;p&gt;Ultimately, technical talks aren&amp;rsquo;t theatre and breaking the fourth wall isn&amp;rsquo;t some shocking thing. Go ahead and bring the audience into your talk.&lt;/p&gt;

&lt;p&gt;Just remember: content is king! People chose to come to your talk; the best audience interaction you could hope for is to prepare some amazing content, blow their minds, and let them tweet up a storm to make their followers jealous they weren&amp;rsquo;t there to experience your brilliance first-hand. 💥&lt;/p&gt;

&lt;h3&gt;Photo Credits&lt;/h3&gt;

&lt;p&gt;&lt;a href="http://www.splitshire.com/sound-mixing-console/"&gt;Mixer&lt;/a&gt;; &lt;a href="https://pixabay.com/en/audience-crowd-event-cheer-945449/"&gt;hands up&lt;/a&gt;; &lt;a href="https://www.flickr.com/photos/gregheo/5312637547/in/album-72157625721681408/"&gt;sleepy puppy&lt;/a&gt;; &lt;a href="https://pixabay.com/en/audience-crowd-people-persons-828584/"&gt;audience&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Outlining Your Conference Talk Pixar-style</title>
    <link rel="alternate" href="/blog/speaking-outlining-like-pixar/"/>
    <id>/blog/speaking-outlining-like-pixar/</id>
    <published>2015-10-15T06:00:00-07:00</published>
    <updated>2015-10-15T06:00:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;You&amp;rsquo;re thinking of submitting a proposal to speak at a conference. Or you&amp;rsquo;ve already committed to speak, and you want to start getting your talk together.&lt;/p&gt;

&lt;p&gt;One tip you&amp;rsquo;ll hear a lot is to structure your talk with a narrative. People have been telling stories since the dawn of language; it&amp;rsquo;s practically in our DNA. With a strong &lt;strong&gt;narrative&lt;/strong&gt; and &lt;strong&gt;call to action&lt;/strong&gt;, your audience will take something of value away with them.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/call-to-action.jpg" alt="Call to action" width="355" height="213"&gt;
&lt;p class="caption wp-caption-text"&gt;&lt;a href="https://www.flickr.com/photos/melenita/15166203826"&gt;Call to action!&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;For technical talks, that doesn&amp;rsquo;t always work. Having a story and characters can get a little distracting or cheesy when you&amp;rsquo;re trying to explain the intricacies of multithreading and semaphores.&lt;/p&gt;

&lt;p&gt;But when you&amp;rsquo;re at the outline phase, thinking of the story &lt;em&gt;behind&lt;/em&gt; your talk can be a useful thing.&lt;/p&gt;

&lt;h2&gt;The Pixar Plot&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://twitter.com/lawnrocket"&gt;Emma Coats&lt;/a&gt;, a freelance director formerly at Pixar, once tweeted &lt;a href="http://storyshots.tumblr.com/post/25032057278/22-storybasics-ive-picked-up-in-my-time-at-pixar"&gt;a list of storytelling basics&lt;/a&gt;. Things that she saw made Pixar films so successful.&lt;/p&gt;

&lt;p&gt;One of the items is this storytelling sequence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Once upon a time there was &amp;hellip;&lt;br&gt;
Every day, &amp;hellip;&lt;br&gt;
One day &amp;hellip;&lt;br&gt;
Because of that, &amp;hellip;&lt;br&gt;
Because of that, &amp;hellip;&lt;br&gt;
Until finally &amp;hellip;  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It&amp;rsquo;s the classic narrative structure here: exposition, rising action, conflict / climax, resolution.&lt;/p&gt;

&lt;p&gt;If you&amp;rsquo;ve watched Nancy Duarte&amp;rsquo;s excellent TEDx talk &amp;ldquo;&lt;a href="http://www.ted.com/talks/nancy_duarte_the_secret_structure_of_great_talks"&gt;The secret structure of great talks&lt;/a&gt;&amp;rdquo;, there&amp;rsquo;s a similar structure at work in her model: toggling between what was, and what could be. It&amp;rsquo;s like a set of mini-stories in the Pixar model: the &amp;ldquo;before&amp;rdquo; (once upon a time, every day) and the &amp;ldquo;after&amp;rdquo; (until finally).&lt;/p&gt;

&lt;h2&gt;Tech Talks&lt;/h2&gt;

&lt;p&gt;This technique works just as well for technical talks. You want to tell your audience about some new API or design pattern or coding technique. But telling them about it isn&amp;rsquo;t enough: you want to &lt;em&gt;convince&lt;/em&gt; them to use the technique, or follow the design pattern, or import that framework.&lt;/p&gt;

&lt;p&gt;Tell them why their current world is horrible, what can fix it, and have a clear call to action.&lt;/p&gt;

&lt;h3&gt;Swift&lt;/h3&gt;

&lt;p&gt;I gave a talk &lt;a href="http://gregheo.com/blog/switching-your-brain-to-swift/"&gt;Switching Your Brain to Swift&lt;/a&gt; at &lt;a href="http://360idev.com/"&gt;360iDev 2015&lt;/a&gt;. You can check out the &lt;a href="http://gregheo.com/blog/switching-your-brain-to-swift/"&gt;blog post&lt;/a&gt; and/or &lt;a href="https://vimeopro.com/360conferences/360idev-2015/video/137530879"&gt;video&lt;/a&gt; if you&amp;rsquo;d like. As I was preparing the talk, I used the Pixar story structure; here&amp;rsquo;s what I came up with:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Once upon a time&lt;/strong&gt; there was Objective-C code and Objective-C apps.&lt;br&gt;
&lt;strong&gt;Every day&lt;/strong&gt;, developers wrote enough square brackets to encircle the globe!&lt;br&gt;
Then &lt;strong&gt;one day&lt;/strong&gt;, Apple unveiled their latest secret project: Swift!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sets the stage of the &amp;ldquo;before&amp;rdquo; state. Then what happened?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Because of that&lt;/strong&gt;, developers could write simpler code, safer code, and still integrate with existing codebases.&lt;br&gt;
And &lt;strong&gt;because of that&lt;/strong&gt;, developers needed some way to make the transition from Objective-C to Swift.&lt;br&gt;
&lt;strong&gt;Until finally&lt;/strong&gt;, Swift took over the world, from operating system kernels to command-line scripts. There were fewer crashes, and more straightforward, easily understandable code in the world.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There&amp;rsquo;s the glorious future of what could be, the world we&amp;rsquo;re striving to create.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/objs-swift-awesomeness.png" alt="Swift awesomeness over time" width="660" height="318"&gt;
&lt;/div&gt;

&lt;h2&gt;So What?&lt;/h2&gt;

&lt;p&gt;In my case, I didn&amp;rsquo;t actually &amp;ldquo;tell a story&amp;rdquo; per se during the talk, but I had this narrative in mind as I prepared. The Pixar plot is useful for finding the core &amp;ldquo;so what?&amp;rdquo; kernel of your talk — the direction where you want to orient your audience.&lt;/p&gt;

&lt;p&gt;The end game I was trying to lead people towards was an all-Swift future. How could I help them get there? That&amp;rsquo;s what I focused on in my talk: Swift&amp;rsquo;s safety features, code simplicity, and bridging.&lt;/p&gt;

&lt;p&gt;Information is nice and stories are fun, but the best talks will inspire the audience to &lt;em&gt;do&lt;/em&gt; something. Thinking of the story behind your talk will remind you to keep your audience&amp;rsquo;s goals and your overall story in mind.&lt;/p&gt;

&lt;p&gt;Why not give it a try next time you&amp;rsquo;re about to go out and inspire the world with your talk?&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Switching Your Brain to Swift</title>
    <link rel="alternate" href="/blog/switching-your-brain-to-swift/"/>
    <id>/blog/switching-your-brain-to-swift/</id>
    <published>2015-08-17T08:00:00-07:00</published>
    <updated>2015-08-17T08:00:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;&lt;i&gt;Based on a talk given at &lt;a href="http://360idev.com/"&gt;360iDev 2015&lt;/a&gt;. Check out &lt;a href="https://vimeopro.com/360conferences/360idev-2015/video/137530879"&gt;the video&lt;/a&gt; too!
&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;The gold standard is, of course, a 100% Swift project started from scratch. If you can do it: great!&lt;/p&gt;

&lt;p&gt;But for those of us with existing codebases and an itch to start with Swift, where can we get started?&lt;/p&gt;

&lt;h2&gt;Why?&lt;/h2&gt;

&lt;p&gt;To take a step back: why would you even want to write in Swift? Plenty of reasons: Swift is the new shiny; Swift has nicer syntax (begin flame wars); Swift is the direction Apple is steering us in.&lt;/p&gt;

&lt;p&gt;In the future, Swift awesomeness will continue to increase and Swift will be the preferred, best-supported, and easiest way to write code for OS X and iOS.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/objs-swift-awesomeness.png" alt="Swift awesomeness over time"&gt;
&lt;/div&gt;

&lt;p&gt;It&amp;rsquo;s the way of the future; simple as that. What can you do to start thinking in the Swift way?&lt;/p&gt;

&lt;h2&gt;The Swift Way&lt;/h2&gt;

&lt;p&gt;There are many things to consider, but let&amp;rsquo;s discuss two big-picture items: safety and value semantics.&lt;/p&gt;

&lt;h3&gt;Safety&lt;/h3&gt;

&lt;p&gt;Nil in Objective-C is awesome. You can message that thing like there&amp;rsquo;s no tomorrow and the runtime will keep providing responses.&lt;/p&gt;

&lt;p&gt;Nil in Swift, however, is quite a different beast. Generally the type system will save you and prevent you from trying to call a method or access a property on &lt;code&gt;nil&lt;/code&gt;. But you can get around the type system, and doing so is as bad as dereferencing a null pointer in C: you&amp;rsquo;ll get friendly trap at runtime and your app will crash.&lt;/p&gt;

&lt;p&gt;In Swift, it&amp;rsquo;s all about type safety. A &lt;code&gt;String&lt;/code&gt; is a &lt;code&gt;String&lt;/code&gt; is a &lt;code&gt;String&lt;/code&gt;. In that case, &lt;code&gt;nil&lt;/code&gt; doesn&amp;rsquo;t even come into the picture. Think more like C++ references rather than C pointers, in that these things will never be nil.&lt;/p&gt;

&lt;h3&gt;Optionals&lt;/h3&gt;

&lt;p&gt;With optionals, &lt;code&gt;nil&lt;/code&gt; is back in the picture. An &lt;em&gt;optional&lt;/em&gt; &lt;code&gt;String&lt;/code&gt; could indeed be a &lt;code&gt;String&lt;/code&gt; or it could be &lt;code&gt;nil&lt;/code&gt;. You have to check. Every time.&lt;/p&gt;

&lt;p&gt;Or not: you can just force unwrap the optional. Or change it to be an &lt;em&gt;implicitly unwrapped optional&lt;/em&gt;, which means it&amp;rsquo;s an optional with an inferiority complex and acts like a real value – except when it&amp;rsquo;s nil, and the app comes crashing down.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/objs-unwrap.png" alt="Unwrap all the things!" width="582" height="335"&gt;
&lt;/div&gt;

&lt;p&gt;Cocoa is full of optionals. That means every time you have one, you have to check what&amp;rsquo;s inside.&lt;/p&gt;

&lt;p&gt;That&amp;rsquo;s a big shift in thinking. The idea is, you shouldn&amp;rsquo;t leave messaging nil up to chance. You should know, with a strong type system, whether something is nil or whether it&amp;rsquo;s a value.&lt;/p&gt;

&lt;p&gt;And if it&amp;rsquo;s uncertain at run time, check. Don&amp;rsquo;t force unwrap.&lt;/p&gt;

&lt;p&gt;Think of optionals as a box: the box can either have nothing (&lt;code&gt;nil&lt;/code&gt;) or a value. But you always have to check before you unwrap or &amp;ldquo;unbox&amp;rdquo; the thing.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/objs-box.jpg" alt="Open the box" width="800" height="533"&gt;
&lt;p class="caption wp-caption-text"&gt;What&amp;rsquo;s inside the optional box, you ask?&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;There are plenty of other examples demonstrating safety in Swift: initializers, less undefined behavior, memory safety. &lt;code&gt;Nil&lt;/code&gt; safety is a pretty common one that comes up all the time.&lt;/p&gt;

&lt;p&gt;&lt;a name="value-types"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Value types&lt;/h3&gt;

&lt;p&gt;Value types are everywhere in Swift. That&amp;rsquo;s nothing new – Objective-C has primitives like &lt;code&gt;NSInteger&lt;/code&gt; and structs like &lt;code&gt;CGRect&lt;/code&gt;. But the vast majority of things – &lt;code&gt;NSString&lt;/code&gt;, &lt;code&gt;NSArray&lt;/code&gt;, and so on – are classes and thus reference types.&lt;/p&gt;

&lt;p&gt;In Swift, it&amp;rsquo;s the complete opposite where the standard library has over 80 structs and only 4 classes if you scan through the headers.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/stdlib-population.png" alt="Structs, classes, and enums" width="800" height="388"&gt;
&lt;/div&gt;

&lt;p&gt;Strings and numbers and collection types are value types in Swift. That means if you have a mutable Swift &lt;code&gt;String&lt;/code&gt; (which is a struct) and pass it into a function, you get a copy. Again, this isn&amp;rsquo;t a terribly new concept: we&amp;rsquo;ve had &lt;code&gt;copy&lt;/code&gt; and &lt;code&gt;mutableCopy&lt;/code&gt; in Objective-C for a long time. The big shift here is that it&amp;rsquo;s the new default behavior for many common types.&lt;/p&gt;

&lt;p&gt;Unfortunately, if you do something awesome with structs in Swift; you won&amp;rsquo;t be able to access them back in Objective-C. That takes us to our next big topic: Bridging.&lt;/p&gt;

&lt;h2&gt;Bridging&lt;/h2&gt;

&lt;p&gt;Swift was of course designed to work well with Objective-C. This is almost a necessity and a given since Cocoa was built for Objective-C. All those Cocoa APIs absolutely have to be callable from Swift, which means &lt;em&gt;your own&lt;/em&gt; Objective-C classes also bridge nicely to Swift.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the problem: &lt;em&gt;starting&lt;/em&gt; with Swift, adding Objective-C, and then expecting to call your Swift stuff from Objective-C.&lt;/p&gt;

&lt;h3&gt;Swift to Objective-C&lt;/h3&gt;

&lt;p&gt;There are a ton of Swift features such as native structs and enhanced enumerations that don&amp;rsquo;t bridge over at all into Objective-C. That means if you write the latest and greatest framework in Swift using all its coolest features, you won&amp;rsquo;t be able to access a lot of this from Objective-C.&lt;/p&gt;

&lt;p&gt;Even if you limit yourself and you write things in Swift to use only compatible features, you can&amp;rsquo;t subclass a Swift class from Objective-C. You could follow the pattern of table views or collection views and use delegates and layout objects to get around the problem, but it&amp;rsquo;s something to keep in mind if your API is meant to be subclassed.&lt;/p&gt;

&lt;p&gt;The default is for nothing in Swift to be visible from Objective-C.&lt;/p&gt;

&lt;p&gt;If you mark your classes and protocols and such with &lt;code&gt;@objc&lt;/code&gt;, then they&amp;rsquo;ll be available on the other side. The &lt;code&gt;dynamic&lt;/code&gt; modifier also implies &lt;code&gt;@objc&lt;/code&gt; so it&amp;rsquo;ll make things available to Objective-C, but it also makes the property or method you&amp;rsquo;re marking dynamic use the Objective-C dynamic dispatch.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/objs-objc-dynamic.png" alt="@objc and dynamic" width="582" height="335"&gt;
&lt;/div&gt;

&lt;p&gt;If you want to swizzle or something, you&amp;rsquo;ll need to use &lt;code&gt;dynamic&lt;/code&gt;; just &lt;code&gt;@objc&lt;/code&gt; on its own isn&amp;rsquo;t enough to guarantee things will use &lt;code&gt;objc_msgSend()&lt;/code&gt; as it&amp;rsquo;s still possible that methods will be devirtualized or inlined.&lt;/p&gt;

&lt;p&gt;And again: this will only work for compatible features. If you have a method in your Swift enumeration, that won&amp;rsquo;t make it over. If you have an enumeration backed by something other than an Int, it won&amp;rsquo;t make it over.&lt;/p&gt;

&lt;h3&gt;Objective-C to Swift: Nullability&lt;/h3&gt;

&lt;p&gt;There&amp;rsquo;s a lot of good news going from Objective-C to Swift. To help this along, you add annotations to the types of your properties, arguments, and return values in Objective-C.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;_Null_unspecified&lt;/code&gt; (default) – bridges to a Swift implicitly-unwrapped optional.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;_Nonnull&lt;/code&gt; – the value won&amp;rsquo;t be nil; bridges to a regular reference.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;_Nullable&lt;/code&gt; – the value can be nil; bridges to an optional.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you annotate your Objective-C, you&amp;rsquo;ll get nice typed bridging over in Swift. Even if you never touch Swift, these annotations will show up in the code completion when you write Objective-C. And if you say a method parameter is &lt;code&gt;_Nonnull&lt;/code&gt; and you pass in nil, you&amp;rsquo;ll get a nice compiler warning.&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s good practice to start adding these annotations. They&amp;rsquo;ll help you when using existing API, and then get you up to speed when you start using Swift.&lt;/p&gt;

&lt;h3&gt;Objective-C to Swift: Lightweight Generics&lt;/h3&gt;

&lt;p&gt;Lightweight generics are new with Swift 2. Collection types &lt;code&gt;NSArray&lt;/code&gt;, &lt;code&gt;NSDictionary&lt;/code&gt; (for values), and &lt;code&gt;NSSet&lt;/code&gt; can contain any old &lt;code&gt;NSObject&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;That means a lot of casting. Not such a problem in Objective-C, but remember: Swift is all about safety. Casting done properly involves lots of checks. You shouldn&amp;rsquo;t just force cast, you should test first.&lt;/p&gt;

&lt;p&gt;Now with generics, that means you can write something like this in Objective-C:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NSArray&amp;lt;NSString *&amp;gt; * _Nonnull
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It&amp;rsquo;s an NSArray that will contain NSString objects. With nullability annotations, you can see it also says the array itself will not be nil; you&amp;rsquo;ll always get an array. The generics syntax here should be familiar if you&amp;rsquo;ve written Java or C++.&lt;/p&gt;

&lt;p&gt;This will then bridge over to Swift like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[String]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A nice, clean array of Swift strings.&lt;/p&gt;

&lt;p&gt;A small footnote: lightweight generics only work for the foundation collection classes: arrays, dictionaries, and sets.&lt;/p&gt;

&lt;h2&gt;Bridge over Troubled Water&lt;/h2&gt;

&lt;p&gt;I would suggest starting new projects in Swift – all, 100% Swift. If you have third-party frameworks, it won&amp;rsquo;t matter as much whether those are in Swift or Objective-C – you&amp;rsquo;ll be able to call through to either.&lt;/p&gt;

&lt;p&gt;If you have an existing codebase and you want to start introducing Swift into it: try to keep the bridging in the Objective-C to Swift direction. Actual instantiated view controllers and views, for example, work great in Swift; and they descend from &lt;code&gt;NSObject&lt;/code&gt; so you can access them from Objective-C if you have to.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/objs-bridge.jpg" alt="Bridge" width="800" height="533"&gt;
&lt;/div&gt;

&lt;p&gt;But for all those other things: full Swift generics, enumerations backed by things other than integers, nested types, structs, and so on – those will have to wait until the day you&amp;rsquo;re in the brave new world of 100% Swift. Don&amp;rsquo;t get left behind; the day will be here sooner than you think!&lt;/p&gt;

&lt;p&gt;Until then: keep writing bridge-friendly Objective-C and stay up to date on Swift. There are plenty of resources for helping you make the transition.&lt;/p&gt;

&lt;h2&gt;Resources&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://www.raywenderlich.com/108522/whats-new-in-swift-2"&gt;What&amp;rsquo;s New in Swift 2&lt;/a&gt; – get up to date on the latest stuff in Swift 2&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://ericcerney.com/swift-guard-statement/"&gt;Swift Guard Statement&lt;/a&gt; – someone asked a question about keeping code in the &amp;ldquo;happy path&amp;rdquo;, which the new &lt;code&gt;guard&lt;/code&gt; statement in Swift 2 helps with!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="http://www.raywenderlich.com/109156/introducing-protocol-oriented-programming-in-swift-2"&gt;Introducing Protocol-Oriented Programming in Swift 2&lt;/a&gt; – Protocol-oriented programming is the new hotness in Swift.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://geo.itunes.apple.com/ca/book/using-swift-cocoa-objective/id1002624212?mt=11&amp;amp;at=11l4G6"&gt;Using Swift with Cocoa and Objective-C (Swift 2 Prerelease)&lt;/a&gt; – Apple&amp;rsquo;s book on Swift + Objective-C + Cocoa&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
  <entry>
    <title>Watch-First Design</title>
    <link rel="alternate" href="/blog/watch-first-design/"/>
    <id>/blog/watch-first-design/</id>
    <published>2015-05-29T06:30:00-07:00</published>
    <updated>2015-05-29T06:30:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;Do you remember &lt;a href="http://en.wikipedia.org/wiki/Wireless_Application_Protocol"&gt;WAP sites&lt;/a&gt; from the days before the fully-functional mobile web?&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/wap-site.jpg" width="145" height="230" alt="A WAP site"&gt;
&lt;p class="caption"&gt;A WAP site&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;I liked using the WAP version of sites (if available) even after I got an iPhone. They were clean, they loaded quickly, and they had the important core parts of the site front and center.&lt;/p&gt;

&lt;p&gt;Planning out your next iOS app? Even if a WatchKit extension isn&amp;rsquo;t part of those plans, think about it — what is the essence of your app, distilled down to a single watch-sized interface?&lt;/p&gt;

&lt;p&gt;Designing with &lt;em&gt;constraints&lt;/em&gt; in this way can really help you get an idea of what&amp;rsquo;s really important.&lt;/p&gt;

&lt;h2&gt;Designing with constraints&lt;/h2&gt;

&lt;p&gt;When the responsive design era of the web came about, the new idea was to &lt;em&gt;start&lt;/em&gt; with mobile first. Think about the core of your site and design for small screens first, then expand out to bigger screens.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/responsive-design.png" width="569" height="640" alt="WAP to phone to desktop"&gt;
&lt;p class="caption"&gt;WAP to phone to desktop&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;That&amp;rsquo;s a pretty good tip if you&amp;rsquo;re feeling stuck: give yourself a constraint to work with to get yourself thinking differently. Stuck on design? Sketch out your site so it only uses three colors. Writer&amp;rsquo;s block? Try writing a few paragraphs with no adverbs. Write a few paragraphs with an adverb in &lt;em&gt;every&lt;/em&gt; sentence.&lt;/p&gt;

&lt;p&gt;In the case of the web, you need to strip away the fringes to get to the inner kernel. What is this web site really about? If someone coming to the site only took one glance and never scrolled, what would you want them to see? There isn&amp;rsquo;t too much room above the fold on mobile; what&amp;rsquo;s really important to your users?&lt;/p&gt;

&lt;h2&gt;Watch-first design&lt;/h2&gt;

&lt;p&gt;This is a good exercise to do when thinking about your iOS apps: determine what is the essence of your app, distilled down to a single watch-sized interface. Even if you&amp;rsquo;re not planning on building a companion watch app, think about it and see where it leads you.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/pitchx.png" width="272" height="340" alt="Pitch X on the Watch"&gt;

&lt;p class="caption"&gt;
Screen from &lt;a href="https://geo.itunes.apple.com/us/app/pitch-x-ultimate-pitch-counter/id973210199?mt=8&amp;uo=6&amp;at=11l4G6" target="itunes_store"&gt;Pitch X&lt;/a&gt;
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;What is the information your users will want to know about? What are the actions they will want to have closest at hand?&lt;/p&gt;

&lt;p&gt;The watch context menu can have up to four buttons. If each button presents a modal, imagine your app could only have four other screens. What would you put in those additional screens? Could you make do with fewer than four?&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/watch-first-context.png" width="272" height="340" alt="Pitch X on the Watch"&gt;
&lt;p class="caption"&gt;Button-mashers will wear out the lower-right corner&amp;hellip;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Now that you&amp;rsquo;ve designed with constraints and figured out what&amp;rsquo;s really important, expand out to the phone, tablet, and wherever else. Again, even if you never build the watch part, think of how much you&amp;rsquo;ve learned about the app!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Force Touch</title>
    <link rel="alternate" href="/blog/force-touch/"/>
    <id>/blog/force-touch/</id>
    <published>2015-04-08T12:30:00-07:00</published>
    <updated>2015-04-08T12:30:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;No, it&amp;rsquo;s not a &lt;em&gt;Star Wars&lt;/em&gt; thing; first spotted on Apple Watch, &lt;strong&gt;force touch&lt;/strong&gt; is the umbrella term for pressure sensitivity on touch devices.&lt;/p&gt;

&lt;p&gt;On the watch, a force touch (simulated with a long press on the simulator) opens up the context menu. Developers can add up to four menu items here for context-appropriate actions based on whatever&amp;rsquo;s on the watch display.&lt;/p&gt;

&lt;div class="captioned wp-caption aligncenter"&gt;
&lt;img src="/images/blog/watchkit-context-menu.png" alt="WatchKit context menu" width="168" height="207"&gt;
&lt;p class="caption wp-caption-text"&gt;Watch context menu&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Force touch on other touch devices — iPhones and iPads — seemed like a no-brainer.  But it turns out the Macs get this new technology next and the iPhone will have to wait a while longer.&lt;/p&gt;

&lt;div class="captioned aligncenter"&gt;
&lt;img src="/images/blog/no-force-touch.jpg" alt="No force touch?" width="396" height="253"&gt;
&lt;p class="caption wp-caption-text"&gt;Confirmed: Vader rocks a 5C&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;What do the force touch APIs look like on OS X? You can get the full details from the documentation linked on Apple&amp;rsquo;s &lt;a href="https://developer.apple.com/osx/force-touch/"&gt;Force Touch for Developers&lt;/a&gt; page, and I&amp;rsquo;ll hit some of the highlights below.&lt;/p&gt;

&lt;h2&gt;Gestures and events&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;NSResponder&lt;/code&gt; and &lt;code&gt;NSGestureRecognizer&lt;/code&gt; have &lt;code&gt;pressureChangeWithEvent:&lt;/code&gt; methods for you to override.&lt;/p&gt;

&lt;p&gt;The event passed in will be an &lt;code&gt;NSEvent&lt;/code&gt; object, which has had a &lt;code&gt;pressure&lt;/code&gt; property for a while now; you can get a &lt;code&gt;Float&lt;/code&gt; here with a value from 0–1.0 with the current pressure. Here&amp;rsquo;s the best line from the API documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pressure is not intended for measuring weight.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means you need to shelve that idea for a force-touch trackpad scale app, OK? ;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stage&lt;/code&gt; is a bit simpler and provides an integer property for gesture events to say whether nothing&amp;rsquo;s happening (0), it&amp;rsquo;s a click / mouse down (1) or &amp;ldquo;additional pressure&amp;rdquo; aka a force touch (2).&lt;/p&gt;

&lt;h2&gt;New buttons&lt;/h2&gt;

&lt;p&gt;OS X 10.10.3 introduces some new button types and functionality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Spring-loaded buttons&lt;/strong&gt;, which provide drag-and-drop on steroids. You can drag something onto a button and then force touch, as if literally drilling down the item into the button.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Accelerator buttons&lt;/strong&gt; can either fire events faster the more pressure the user places on the button (useful for media controls) or can be non-continuous and only fire an event when the pressure changes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-level accelerator buttons&lt;/strong&gt; are like sliders with stepped pressure levels. For example, you could set it for three pressure levels and then get an &lt;code&gt;integerValue&lt;/code&gt; from 1–3. You get a little haptic feedback with each step too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There&amp;rsquo;s always a fallback for non-force touch devices: drag-drop-and-wait for spring-loaded buttons, while accelerator buttons will just return a simple on/off or 0/1 value.&lt;/p&gt;

&lt;h2&gt;Force touch on iOS&lt;/h2&gt;

&lt;p&gt;What might this look like when force touch makes it to iOS, possibly later this year?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Haptic feedback&lt;/strong&gt; – we can make the phone vibrate, but what about a more precise &lt;code&gt;vibrateWithIntensity:&lt;/code&gt; method? ;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;New gesture recognizer&lt;/strong&gt; – we already have gesture recognizers for tap and long press, so maybe there will be a new one for force touch. Or, to follow the Watch simulator route, maybe force touch will be an option on the existing long press for backwards compatibility?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Accelerator buttons&lt;/strong&gt; – the API for these on OS X looks really neat and there&amp;rsquo;s a reasonably graceful fallback for non-force touch devices too. Some new properties on &lt;code&gt;UIButton&lt;/code&gt; or even new methods on &lt;code&gt;UIResponder&lt;/code&gt; would work.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pressure-related properties&lt;/strong&gt; on &lt;code&gt;UITouch&lt;/code&gt; – drawing apps could make good use of a new &lt;code&gt;pressure&lt;/code&gt; property in &lt;code&gt;UITouch&lt;/code&gt; objects. I&amp;rsquo;m sure the folks at Facebook are already looking to revive their &lt;em&gt;Poke&lt;/em&gt; app to support different levels of force to match the poke intensity.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="captioned aligncenter"&gt;
&lt;img src="/images/blog/facebook-poke.png" alt="POKE" width="180" height="180"&gt;
&lt;/div&gt;
</content>
  </entry>
  <entry>
    <title>RWDevCon 2015</title>
    <link rel="alternate" href="/blog/rwdevcon-2015/"/>
    <id>/blog/rwdevcon-2015/</id>
    <published>2015-02-13T05:30:00-08:00</published>
    <updated>2015-02-13T05:30:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;It&amp;rsquo;s a week after &lt;a href="http://rwdevcon.com/"&gt;RWDevCon&lt;/a&gt; and what&amp;rsquo;s on my mind as I write this post? &lt;a href="http://www.raywenderlich.com/95181/whats-new-in-swift-1-2"&gt;Swift 1.2&lt;/a&gt;? Collection view layouts? No, of course – it&amp;rsquo;s the people, and finally getting to meet my virtual colleagues!&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;a href="/images/blog/rwdevcon15-razeware.jpg"&gt;&lt;img src="http://gregheo.com/images/blog/rwdevcon15-razeware.jpg" width="722" height="453" alt="As if weekly Google Hangouts with this bunch wasn't enough"&gt;&lt;/a&gt;
&lt;p class="caption"&gt;As if weekly Google Hangouts with this bunch wasn&amp;rsquo;t enough (&lt;a href="https://www.flickr.com/photos/rwdevcon/16498234145/in/set-72157650690686066"&gt;source&lt;/a&gt;)&lt;/p&gt;
&lt;/div&gt;

&lt;h3&gt;Flashback time&lt;/h3&gt;

&lt;p&gt;Way back in 2005 when I was starting my freelance career, I worked with a &lt;a href="http://www.pectopah.com"&gt;fantastic group of colleagues&lt;/a&gt;. They were also here in Toronto, but it was four months before I met them. How strange, I thought! To go four months of working closely with people, talking on the phone almost every day, but never meeting in person.&lt;/p&gt;

&lt;h3&gt;Pixels and bits on the screen&lt;/h3&gt;

&lt;p&gt;Fast forward to more modern times. My closest colleagues are now best known to me as Twitter handles and IRC nicks and more recently, 36x36 &lt;a href="http://slackhq.com/"&gt;Slack&lt;/a&gt; avatars.&lt;/p&gt;

&lt;div class="captioned wp-caption aligncenter"&gt;
&lt;img src="http://gregheo.com/images/blog/rwdevcon15-slack-heads.jpg" title="Virtual water cooler inhabitants" alt="Virtual water cooler inhabitants"&gt;
&lt;p class="caption wp-caption-text"&gt;Virtual water cooler inhabitants&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;I always thought text-based conversation makes it hard to get a sense of people&amp;rsquo;s feelings and emotions – are they kidding? Serious? Did they mean to add a sarcastic smiley face on that? I still feel this way about email, which often reads as &lt;a href="http://www.fastcodesign.com/3036748/evidence/why-its-so-hard-to-detect-emotion-in-emails-and-texts"&gt;cold and terse&lt;/a&gt; to me.&lt;/p&gt;

&lt;p&gt;But spend enough time on IRC/Slack and throw the odd Google Hangout into the mix (with an upgrade to 100x100 pixels per person) and an amazing thing happens: you feel like you know these people!&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;a href="/images/blog/rwdevcon15-hangout-heads.jpg"&gt;&lt;img src="http://gregheo.com/images/blog/rwdevcon15-hangout-heads.jpg" alt="OMG talking heads!"&gt;&lt;/a&gt;
&lt;p class="caption"&gt;OMG talking heads!&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;I start thinking: I know exactly what Brian will say next! Tammy sounds super enthusiastic today! Where has Felipe been lately? That sounds just like a @cwagdev joke! How can I troll ecerney this week? And so on.&lt;/p&gt;

&lt;h3&gt;Meatspace&lt;/h3&gt;

&lt;p&gt;Turns out, these &lt;a href="http://www.terrybisson.com/page6/page6.html"&gt;people&lt;/a&gt; aren&amp;rsquo;t just bits over the air. Even in the early dawn of 6am on our first team outing and as the day went on, I had no problem recognizing voices and faces and personalities.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/rwdevcon15-group.jpg" width="900" height="450" alt="As always, people are taller than I expect"&gt;
&lt;p class="caption"&gt;As always, people are taller than I expect (&lt;a href="https://www.flickr.com/photos/gregheo/15841758323/in/set-72157650246222269"&gt;source&lt;/a&gt;)&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;There were no odd introductions or awkward moments. It was like I had been working with these people for many years – because I had! Seeing them in person and starting up a conversation from where we left off online was the most natural thing in the world.&lt;/p&gt;

&lt;h3&gt;Remote work&lt;/h3&gt;

&lt;p&gt;There&amp;rsquo;s been a lot of talk lately about &lt;a href="https://signalvnoise.com/posts/3657-37signals-works-remotely"&gt;remote work&lt;/a&gt; and how it&amp;rsquo;s the way of the future. Generally, I agree. Maybe not to the extent of wearing VR headsets in virtual meetings, but it&amp;rsquo;s not necessary to be in the same room as your co-workers for anywhere near 100% of the time.&lt;/p&gt;

&lt;p&gt;As much as I love working remotely from home, which I&amp;rsquo;ve done for ten years now, there&amp;rsquo;s something to be said for meeting in person. Having lunch, casual conversations, doing a walking tour of a city, and just hanging out with these ~40 people I work with were the highlights of the conference for me.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/rwdevcon15-team.jpg" width="900" height="374" alt="Team members at RWDevCon"&gt;
&lt;p class="caption"&gt;Team members at RWDevCon (&lt;a href="https://www.flickr.com/photos/rwdevcon/16496493231/in/set-72157650690686066"&gt;source&lt;/a&gt;)&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;Ultimately, we&amp;rsquo;re a distributed remote team and there&amp;rsquo;s no way around that. With tools such as &lt;a href="http://slackhq.com/"&gt;Slack&lt;/a&gt; and &lt;a href="https://trello.com/"&gt;Trello&lt;/a&gt; and &lt;a href="https://docs.google.com/"&gt;Google Docs&lt;/a&gt; and &lt;a href="https://tools.ietf.org/html/rfc5321"&gt;good old email&lt;/a&gt;, we make it work. Even so, there&amp;rsquo;s no substitute for hanging out in person at least once in a while.&lt;/p&gt;

&lt;p&gt;For everyone who was there this year – colleagues, people who said hello, attendees at my two sessions: it was a pleasure to meet you! For those who missed it: let&amp;rsquo;s meet up soon.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m on record as having suggested both fall and winter editions of RWDevCon. Hey, why not make it a travelling show like CocoaConf? ;]&lt;/p&gt;

&lt;h3&gt;Epilogue: the selfiepocalypse&lt;/h3&gt;

&lt;p&gt;In my non-work life, I&amp;rsquo;m the unofficial family photographer. That means I&amp;rsquo;m not in many of the photos, which some family members have commented on. My solution: I try to take lots of selfies at Christmas and Thanksgiving and other such family events.&lt;/p&gt;

&lt;p&gt;So here&amp;rsquo;s another thank you to the &lt;a href="https://www.flickr.com/photos/gregheo/sets/72157650688204062/"&gt;team members&lt;/a&gt; who put up with my new habit at the conference.&lt;/p&gt;

&lt;div class="captioned"&gt;
&lt;img src="/images/blog/rwdevcon15-selfie-mosaic.jpg" width="800" height="667" alt="Selfies at RWDevCon"&gt;
&lt;p class="caption"&gt;Selfies at RWDevCon. Yes, I only have three facial expressions. (&lt;a href="https://www.flickr.com/photos/gregheo/sets/72157650688204062/"&gt;view all&lt;/a&gt;)&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;I didn&amp;rsquo;t get to everyone, so my apologies if I missed you. Next time I&amp;rsquo;m bringing an attendance list with me!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>What's New in Swift 1.2</title>
    <link rel="alternate" href="/blog/swift-12/"/>
    <id>/blog/swift-12/</id>
    <published>2015-02-11T04:00:00-08:00</published>
    <updated>2015-02-11T04:00:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;Another linky post – a summary of &lt;a href="http://www.raywenderlich.com/95181/whats-new-in-swift-1-2"&gt;what&amp;rsquo;s new and notable in Swift 1.2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are some annoying things that will cause many compile errors, and some
cool things that add interesting features to the language. Check it out,
and get coding!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Four Things I Learned Making My First Swift App</title>
    <link rel="alternate" href="/blog/first-swift-app/"/>
    <id>/blog/first-swift-app/</id>
    <published>2014-10-21T17:00:00-07:00</published>
    <updated>2014-10-21T17:00:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;Swift is a whole lot of fun — fun to learn and fun to write.&lt;/p&gt;

&lt;p&gt;Being on the cutting edge of Apple technology has its ups and downs though;
check out my blog post on &lt;a href="http://www.raywenderlich.com/86278/four-things-learned-making-first-swift-app"&gt;raywenderlich.com&lt;/a&gt;
for all the details!&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Shipping Stuff</title>
    <link rel="alternate" href="/blog/ship/"/>
    <id>/blog/ship/</id>
    <published>2014-10-10T03:10:10-07:00</published>
    <updated>2014-10-10T03:10:10-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;Here&amp;rsquo;s an often frightening set of questions I need to ask myself
more often:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What have you shipped lately?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What are you waiting for?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m the kind of person who starts many projects and has trouble saying &amp;ldquo;no&amp;rdquo;
to things. Couple that with general procrastination and too much picking on
the little details and you get a lack of stuff shipped.&lt;/p&gt;

&lt;p&gt;What can I say, I&amp;rsquo;m working on it! I should print out those two questions
and keep them over my desk.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Transformable attributes in Core Data</title>
    <link rel="alternate" href="/blog/core-data-transformable/"/>
    <id>/blog/core-data-transformable/</id>
    <published>2014-01-27T16:00:00-08:00</published>
    <updated>2014-01-27T16:00:00-08:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;In addition to the usual types – string, float, boolean, date – you can
define core data entities with an attribute of type &lt;em&gt;Transformable&lt;/em&gt;.
What is this magic type, and what does it transform into?&lt;/p&gt;

&lt;p&gt;Add a Transformable property, generate an &lt;code&gt;NSManagedObject&lt;/code&gt; subclass,
and this is what you&amp;rsquo;ll see:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@property (nonatomic, retain) id myTransformable;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A plain old &lt;code&gt;id&lt;/code&gt;! So in theory, you could set any object there. On 10.9, I
can set my own custom &lt;code&gt;NSObject&lt;/code&gt; subclass object to the transformable property,
but when I save the context I get an exception for the unrecognized
&lt;code&gt;encodeWithCoder:&lt;/code&gt; message.&lt;/p&gt;

&lt;p&gt;There&amp;rsquo;s the clue that the object needs to conform to &lt;code&gt;NSCoding&lt;/code&gt;. So &lt;code&gt;NSArray&lt;/code&gt;,
&lt;code&gt;NSDictionary&lt;/code&gt;, &lt;code&gt;NSData&lt;/code&gt;, and others are supported out of the box.&lt;/p&gt;

&lt;p&gt;So you can use your own class as-is — as long as you implement the required
protocol methods &lt;code&gt;initWithCoder:&lt;/code&gt; and &lt;code&gt;encodeWithCoder:&lt;/code&gt;. Then, you and Core
Data can get a room and your data will be happily persisted and realized
with your classes intact.&lt;/p&gt;

&lt;p&gt;The standard downside is that transformable attributes are stored in the
SQLite backend as binary plists inside BLOBs, so you can&amp;rsquo;t query
those fields directly from an &lt;code&gt;NSPredicate&lt;/code&gt;.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>iOS 7 By Tutorials</title>
    <link rel="alternate" href="/blog/i7t/"/>
    <id>/blog/i7t/</id>
    <published>2013-09-17T17:00:00-07:00</published>
    <updated>2013-09-17T17:00:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;With iOS 7 released today, what&amp;rsquo;s a developer to do? Wade through screenfuls of
framework documentation and header files?&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p style="text-align: center;"&gt;&lt;img src="/images/blog/animate-rage.png" width="604" height="202" alt="Need documentation"&gt;&lt;/p&gt;

&lt;p&gt;No! Learn by example and step-by-step instructions and challenges!&lt;/p&gt;

&lt;p style="text-align: center;"&gt;
&lt;a href="http://www.raywenderlich.com/store/ios-7-by-tutorials?source=silentsilicon" target="_blank"&gt;&lt;img src="/images/blog/i7t.png" width="560" height="396" alt="iOS 7 By Tutorials book"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve been on the editorial team over at &lt;a href="http://raywenderlich.com"&gt;raywenderlich.com&lt;/a&gt;
for almost a year now. Aside from being among the best virtual coworkers a guy could
ask for, these are some super smart folks who can explain everything from selectors
to Sprite Kit to source control.&lt;/p&gt;

&lt;p&gt;I had a blast writing the chapter on unit testing, as well as tech editing part of the book.
You&amp;rsquo;ll love it as much as I know the whole team loved working on it!&lt;/p&gt;

&lt;p&gt;Store links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.raywenderlich.com/store/ios-7-by-tutorials?source=silentsilicon"&gt;iOS 7 by Tutorials&lt;/a&gt; – everything you wanted to know about iOS 7.
(On &lt;a href="http://www.amazon.com/gp/product/0989675106/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0989675106&amp;linkCode=as2&amp;tag=grhe-20"&gt;Amazon.com&lt;/a&gt;&lt;img src="http://ir-na.amazon-adsystem.com/e/ir?t=grhe-20&amp;l=as2&amp;o=1&amp;a=0989675106" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt; too).&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.raywenderlich.com/store/ios-games-by-tutorials?source=silentsilicon"&gt;iOS Games by Tutorials&lt;/a&gt; – work
through building five games with Sprite Kit!&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.raywenderlich.com/store/ios-7-and-ios-games-by-tutorials-bundle?source=silentsilicon"&gt;The iOS 7 bundle&lt;/a&gt; – 
everything you need to learn about what&amp;rsquo;s new in iOS 7, including Sprite Kit.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners"&gt;Sprite Kit Tutorial for Beginners&lt;/a&gt; – a preview
chapter from &lt;em&gt;iOS Games by Tutorials&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
</content>
  </entry>
  <entry>
    <title>Adventures in Dvorak, two months later</title>
    <link rel="alternate" href="/blog/dvorak-2/"/>
    <id>/blog/dvorak-2/</id>
    <published>2013-08-12T17:00:00-07:00</published>
    <updated>2013-08-12T17:00:00-07:00</updated>
    <author>
      <name>Greg Heo</name>
    </author>
    <content type="html">&lt;p&gt;I&amp;rsquo;m now two months into the great 
&lt;a href="https://en.wikipedia.org/wiki/Dvorak_Simplified_Keyboard"&gt;Dvorak&lt;/a&gt; switchover.
So far so good! I can no longer type on a QWERTY keyboard unless I look at
the keys. The context switch seems to be strong enough that typing on the
iPhone makes my brain auto-switch to QWERTY, although I find myself mistyping
there sometimes.&lt;/p&gt;

&lt;p&gt;The stuff I thought would be the worst &amp;ndash; shortcut keys and vi keys &amp;ndash; ended
up being not such a big deal. I thought muscle memory would be much stronger,
but it turns out I rememember keyboard commands by the letter or character and
not the physical position.&lt;/p&gt;

&lt;p&gt;My new favorite typing practice site is &lt;a href="http://typing.io/"&gt;typing.io&lt;/a&gt;.
I used the classic GNU Typist at first, but typing.io is programmer-specific
and I get to read an interesting selection of code as I go! Most importantly,
the exercises there include all those curly braces and semicolons that
programmers type a lot.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m still thinking about buying an Ultimate Keyboard:&lt;/p&gt;

&lt;p style="text-align: center;"&gt;&lt;a href="http://www.daskeyboard.com/model-s-ultimate/"&gt;&lt;img src="/images/blog/ultimate-keyboard.jpg" alt="Ultimate keyboard" title="Ultimate keyboard" width="520" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the meantime, I did get a
&lt;a href="http://www.amazon.com/gp/product/B00B4XJYOU/ref=as_li_ss_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=B00B4XJYOU&amp;amp;linkCode=as2&amp;amp;tag=grhe-20"&gt;Dvorak Keyboard Cover&lt;/a&gt;.
&lt;img src="http://ir-na.amazon-adsystem.com/e/ir?t=grhe-20&amp;l=as2&amp;o=1&amp;a=B00B4XJYOU" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;
One flaw is it omits the all-important square brackets but as an Objective-C
developer, I learned where those are right on day one!&lt;/p&gt;
</content>
  </entry>
</feed>
