<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Marc Prud&apos;hommeaux</title><description>Personal site and blog of Marc Prud&apos;hommeaux — software developer based in New England.</description><link>https://www.prux.org/</link><atom:link href="https://www.prux.org/rss.xml" rel="self" type="application/rss+xml"/><item><title>On Leaving Swift for Rust</title><link>https://www.prux.org/posts/goodbye-swift-hello-rust/</link><guid isPermaLink="true">https://www.prux.org/posts/goodbye-swift-hello-rust/</guid><description>A fond farewell to Swift</description><pubDate>Thu, 29 Oct 2026 12:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Yesterday I &lt;a href=&quot;https://github.com/swiftlang/swift-org-website/pull/1589&quot;&gt;resigned&lt;/a&gt; from the &lt;a href=&quot;https://www.swift.org/android-workgroup/&quot;&gt;Swift Android workgroup&lt;/a&gt;. I’ve received a number of inquiries about this, and rather than responding individually to each one, I figured I would just write up a post with my reasoning and submit it to the public record.&lt;/p&gt;
&lt;p&gt;First off, my resignation was not triggered by any sort of conflict, grievance, or specific incident. Early Swift community efforts got off to a rough start when Swift language creator Chris Lattner quit  due to what he &lt;a href=&quot;https://mjtsai.com/blog/2022/02/22/why-latter-left-the-swift-core-team/&quot;&gt;described&lt;/a&gt; as a “toxic environment”. This event predates my direct involvement in the project, so I am not privy to the specifics. But I want to be clear that this was in no way my experience: everyone I interacted with, both from Apple and external organizations, both in meetings and in forum discussions and direct messages, have been polite, courteous, and the paragon of civility and professional conduct. Despite Apple having an ancient &lt;a href=&quot;https://appleinsider.com/articles/11/10/21/steve_jobs_vowed_to_destroy_google_android_called_it_a_stolen_product&quot;&gt;blood feud&lt;/a&gt; with Android, and my own &lt;a href=&quot;https://appfair.org/blog/a-future-for-app-stores/&quot;&gt;rather&lt;/a&gt; &lt;a href=&quot;https://appfair.org/blog/digital-markets-act-workshop/&quot;&gt;acrimonious&lt;/a&gt; &lt;a href=&quot;https://appfair.org/blog/fear-and-loathing-in-the-app-stores/&quot;&gt;relationship&lt;/a&gt; with certain divisions of the company, every single one of my direct interactions with the Swift team has been pleasant and productive. I will miss our meeting and chats, and I harbor zero ill-will towards the project or any of the individual members I’ve worked with.&lt;/p&gt;
&lt;p&gt;So why quit?&lt;/p&gt;
&lt;h2 id=&quot;computer-languages-past-and-future&quot;&gt;Computer Languages, Past and Future&lt;/h2&gt;
&lt;p&gt;By way of background, I’ve been programming for over 40 years. I started with Basic, then Pascal, on to C, then C++, over to Java, up to Scala, back down to Kotlin, around to Objective-C, before finally landing on Swift. Swift is a delightful language: I loved it from day one and I’ve been &lt;a href=&quot;https://www.prux.org/talks/#2014&quot;&gt;boosting&lt;/a&gt; it around the world since 2014. Swift really “does what it says on the tin”: it couples the performance of C++ with the ease of Java. And Swift is as developer-friendly as it gets:  an intuitive object-oriented syntax coupled with maximally efficient value types, fabulous tooling (on Apple platforms), and a thoughtfully-designed build system.&lt;/p&gt;
&lt;p&gt;I have &lt;em&gt;not&lt;/em&gt; soured on Swift: I think it is an excellent language, and it is here to stay. But it is here to stay in that same straight-and-narrow lane shared by all corporate-directed languages, like C# is here to stay for Microsofties and like Kotlin is here to stay for Androidophiles. Which isn’t bad in and of itself: if you want to write software solely for &lt;code&gt;[CORPORATE PRODUCT]&lt;/code&gt;, then &lt;code&gt;[CORPORATE LANGUAGE]&lt;/code&gt; will always be well-maintained, supported, and they will &lt;em&gt;always&lt;/em&gt; provide support for their latest products, with an easy migration path forward. They are the safe and happy path, as long as you stay in your lane and limit your ambitions to writing software for &lt;code&gt;[CORPORATE PRODUCT]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But when you stray from that happy path, the rough edges start surfacing, and there is no one getting a paycheck to smooth those out. Swift for Android/Windows/other-non-Apple-platform is in exactly that position: yes, it &lt;em&gt;technically&lt;/em&gt; works, and yes, the language steering committees are committed to continuing the level of support they promised, but it will never be the same seamless experience as developing for iOS/watchOS/visionOS/whateverOS. This extends beyond the language and into the tooling: &lt;a href=&quot;https://skip.dev/&quot;&gt;Skip&lt;/a&gt; enables you use Xcode to develop your apps in parallel for iOS and Android, but this has only been achieved in the face of Xcode’s constitutional hostility towards doing anything other than optimizing for their own first-party platforms.&lt;/p&gt;
&lt;h2 id=&quot;the-rise-of-the-clankers&quot;&gt;The Rise of the Clankers&lt;/h2&gt;
&lt;p&gt;If humanity were destined to continue creating software solely with their monkey brains and meat fingers, Swift would likely be the language I would have stuck with until the day I retired. But that isn’t the trajectory we are currently on. Barring such speculative calamities as “model collapse”, Butlerian Jihad, or Skynet, we have taken a turn in a new direction.&lt;/p&gt;
&lt;p&gt;We software developers have collectively [as/de]cended to a plane where we now direct the computers to write our computer code, and these computers have decided that they really prefer &lt;em&gt;computery&lt;/em&gt; languages over the &lt;em&gt;humany&lt;/em&gt; languages. &lt;em&gt;Humany&lt;/em&gt; languages are those designed to fit inside our wonderful little biological brains. I have grave reservations about all of this from many perspectives: ethical, ecological, political, socio-economical (just to name a few). But it is what it is, and all but the most ardent of refusenik software developers have come around to this view in the past 6—12 months.&lt;/p&gt;
&lt;p&gt;Swift isn’t a &lt;em&gt;computery&lt;/em&gt; language, it is a developer-friendly &lt;em&gt;humany&lt;/em&gt; language: it is a better Java, a better C#, and a (much) better Objective-C. Throughout my career I’ve been able to accomplish amazing feats in these languages, disproportionate to my limited intellect and abilities, but that comes at the cost of these languages being hobbled in pursuit of their mission to slice and dice complex problems in such a way as to be tractable for one or more human brains. I speak of object-orientation; I speak of automatic memory management.&lt;/p&gt;
&lt;p&gt;But there is one &lt;strong&gt;very&lt;/strong&gt; &lt;em&gt;computery&lt;/em&gt; language that until recently has been waiting in the wings…&lt;/p&gt;
&lt;h2 id=&quot;all-roads-lead-to-rust&quot;&gt;All roads lead to Rust&lt;/h2&gt;
&lt;p&gt;In February, Andreas Kling dropped &lt;a href=&quot;https://ladybird.org/posts/adopting-rust/&quot;&gt;Swift for Rust&lt;/a&gt; for the Ladybird browser, a 180°&lt;sup&gt;&lt;a href=&quot;https://www.prux.org/posts/goodbye-swift-hello-rust/#user-content-fn-1&quot; id=&quot;user-content-fnref-1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; turnabout from their opposite decision from a &lt;a href=&quot;https://www.youtube.com/watch?v=DSEZ2ZYLdHg&quot;&gt;year earlier&lt;/a&gt;. In July, Jarred Sumner ported Bun from &lt;a href=&quot;https://bun.com/blog/bun-in-rust&quot;&gt;Zig to Rust&lt;/a&gt;. In September, David Heinemeier Hansson filicided &lt;a href=&quot;https://www.globalnerdy.com/2026/09/27/dhhs-keynote-at-rails-world-2026-the-most-confusing-funeral-ive-ever-experienced/&quot;&gt;Ruby on Rails for Rust&lt;/a&gt;. And this trend is only accelerating: nearly every week there is some new project converting some massively complex codebase from language X to Rust. &lt;a href=&quot;https://news.ycombinator.com/item?id=48841676&quot;&gt;PostgreSQL&lt;/a&gt;! The &lt;a href=&quot;https://www.theregister.com/devops/2026/10/15/24000-ai-experiment-ports-typescript-compiler-to-rust/5303409&quot;&gt;TypeScript compiler&lt;/a&gt;! These projects are coming fast and furious with no sign of slowing down. And for every one of these projects, over 95% of the code was not written by a human.&lt;/p&gt;
&lt;p&gt;Swift is wonderful. It enabled me to bring iOS development to Android through Skip and share a single codebase between the two dominant mobile platforms. LLMs are quite proficient with Skip: Claude or Codex can one-shot a passable prototype for a new iOS+Android app written in Swift in a matter of minutes and you can have it running side-by-side on emulators or devices and start iterating to your heart’s content. The single shared codebase gives the LLMs the ability to develop and refine features in single codebase, rather than constantly needing one codebase (e.g., in Kotlin and Compose) play catch-up with the other (Swift and SwiftUI) codebase.&lt;/p&gt;
&lt;p&gt;But I always wanted more from Skip. I wanted to add support for desktop platforms for Mac, Linux, and Windows. I wanted the same codebase to run on the web through webassembly. I wanted to build for emerging mobile platforms like HarmonyOS and anything that might come next. And I wanted to do it all using each platform’s native first-party toolkits rather than the usual web-shell compromise or pixel-painting mimicry, because, like Apple, I believe that &lt;a href=&quot;https://developer.apple.com/videos/play/wwdc2024/102/?time=3849&quot;&gt;the best apps are built with native SDKs&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In addition to supporting a wider range of &lt;em&gt;target&lt;/em&gt; platforms, I also wanted to bring this power to more development &lt;em&gt;host&lt;/em&gt; platforms. You can currently only use Skip to develop apps from a macOS host; Windows and Linux users are left out in the cold. And while there is some &lt;a href=&quot;https://xtool.sh/&quot;&gt;impressive work&lt;/a&gt; happening to bring iOS development to non-macOS hosts, those tools will take a while to percolate, and appear to &lt;a href=&quot;https://github.com/xtool-org/xtool/issues/55#issuecomment-2898015355&quot;&gt;face the wrath&lt;/a&gt; of the platform overlords. For Skip, it is irksome enough that even the Android target of the app cannot be developed on a non-macOS host, but when it comes to expanding the platform targets, it becomes downright absurd if, say, you would be unable use Skip to develop a HarmonyOS app from a Windows host.&lt;/p&gt;
&lt;p&gt;Swift — both the language as well as the tooling — is simply not equipped to handle those needs yet, and despite its impressive progress with their cross-compilation SDKs, I don’t think it ever will be. It will &lt;em&gt;always&lt;/em&gt; be an Apple-platforms-first technology, and anything beyond that insular world will be consigned to an eternal Sisyphean uphill grind.&lt;/p&gt;
&lt;p&gt;Rust, on the other hand, wasn’t designed for a single platform, or even a small handful of platforms. They support &lt;a href=&quot;https://doc.rust-lang.org/nightly/rustc/platform-support.html&quot;&gt;300 platforms&lt;/a&gt;. Cross-compilation has been a foundational principle from day one, rather than being a bolted-on afterthought. This leads to an ecosystem that values building for everything and everyone, with no platform bias. When I started out on this journey, I was frankly astonished when the code I was tinkering with for iOS — which included dozens of rando &lt;a href=&quot;https://crates.io/&quot;&gt;crates.io&lt;/a&gt; dependencies that I yolo’d into my little research project — &lt;em&gt;just worked&lt;/em&gt; for Android. And Windows. And Linux. And HarmonyOS. And Webassembly.&lt;/p&gt;
&lt;p&gt;Then when I discovered that despite its massive power and potential, you still &lt;a href=&quot;https://areweguiyet.com/&quot;&gt;can’t craft a decent consumer-facing app&lt;/a&gt; in Rust, my mission was clear. Escaping the surly bonds of the platform has always been my personal obsession, and in the mere six months since I started down this road, I’ve created the ultimate all-singing-all-dancing omni-platform development tool. More on that in a future post. “Like and subscribe”, as the kids say.&lt;/p&gt;
&lt;p&gt;But getting back to the point: Swift will be fine. It is here to stay, at least for Apple platform development. And Skip will be fine: it has a growing community of developers and maintainers that will keep the project going strong and keep up with Swift and Xcode updates. Skip will continue to improve the Android side of the coin in step with Apple’s improvement of the iOS side. I am confident that both technologies will keep chugging along indefinitely, powering fabulous apps on Apple and Android platforms alike, well into the future.&lt;/p&gt;
&lt;p&gt;But that is not &lt;em&gt;my&lt;/em&gt; future. Software development is suddenly no longer hard. App development is suddenly no longer hard. And I need to be solving hard problems in order to thrive.&lt;/p&gt;
&lt;section&gt;&lt;h2 id=&quot;footnote-label&quot;&gt;Footnotes&lt;/h2&gt;
&lt;ol&gt;
&lt;li id=&quot;user-content-fn-1&quot;&gt;
&lt;p&gt;The initial revision of this post said “360°” when it meant “180°”. Thank you, internet commentariat, for your gracious corrections. &lt;a href=&quot;https://www.prux.org/posts/goodbye-swift-hello-rust/#user-content-fnref-1&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;&lt;img src=&quot;https://marcprux.goatcounter.com/count?p=%2Frss%2Fposts%2Fgoodbye-swift-hello-rust%2F&amp;amp;t=On+Leaving+Swift+for+Rust&quot; width=&quot;1&quot; height=&quot;1&quot; alt=&quot;&quot; /&gt;</content:encoded></item><item><title>Apple DMA Compliance Workshop</title><link>https://www.prux.org/posts/apple-dma-compliance-workshop/</link><guid isPermaLink="true">https://www.prux.org/posts/apple-dma-compliance-workshop/</guid><description>A firsthand account of the European Commission’s workshop on Apple’s DMA compliance, including my questions about marketplace access and privacy.</description><pubDate>Mon, 18 Mar 2024 12:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href=&quot;https://appfair.org/blog/digital-markets-act-workshop/&quot;&gt;the App Fair Project’s blog&lt;/a&gt; on March 18, 2024. This account describes the policies and discussion at that time.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;: The video for the workshop has been made available at &lt;a href=&quot;https://webcast.ec.europa.eu/compliance-with-the-dma-apple-2024-03-18&quot;&gt;the European Commission’s webcast&lt;/a&gt;. I’ve updated the blog post with timecodes for each of the questions and answers listed herein.&lt;/p&gt;
&lt;p&gt;On March 18th I attended an EC-hosted workshop&lt;sup&gt;&lt;a href=&quot;https://www.prux.org/posts/apple-dma-compliance-workshop/#user-content-fn-1&quot; id=&quot;user-content-fnref-1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; in Brussels on Apple’s compliance measures for the Digital Markets Act. It was a grueling 8-hour affair in a hot windowless room. There were around 75 attendees by my count, from a wide cross-section of organizations, few of whom seemed to feel that Apple was upholding the letter and spirit of the law in their compliance efforts.&lt;/p&gt;
&lt;p&gt;Apple’s team of three, headed by Kyle Andeer (formerly an FTC trial lawyer), gamely managed to fend off the barrage, mostly by appealing to Apple’s paramount respect for “user security, privacy and safety” over and over again. The questions tended to be hostile and self-serving, and the responses tended to be vacuous, non-committal, and lacking any technical substance. In short, it went as one might expect.&lt;/p&gt;
&lt;p&gt;Questioners were selected randomly from the attendees (both in-person and online). I managed to get two in. Following are my questions and their responses (pulled out of a whisper-generated transcript from the video, which can be accessed &lt;a href=&quot;https://gist.github.com/appfairz/83e179243971f65bea36745bc2e15ce5&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;
&lt;h2 id=&quot;marketplace-requirements&quot;&gt;Marketplace requirements&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Marc Prud’hommeaux, the App Fair Project&lt;/strong&gt; — &lt;a href=&quot;https://gist.github.com/appfairz/83e179243971f65bea36745bc2e15ce5#file-apple_eu_dma_compliance_workshop_whisper_transcript-txt-L2817&quot;&gt;12:25:41&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Hi, my name is Marc Prud’hommeaux, and I’m here representing the nonprofit App Fair Project, which is building an app marketplace to create and distribute free and open source apps as non-commercial digital public goods.&lt;/p&gt;&lt;p&gt;To be approved for an iPhone app marketplace entitlement, Apple is currently requiring that an organization, either 1: have been an Apple developer program member for two years and have an app that has been downloaded one million times in the EU in the previous year.&lt;/p&gt;&lt;p&gt;We’ve been a developer program member since April of 2022, but it’s impossible for us to satisfy the download count requirement because the web browser app that we submitted that year was rejected by Apple.&lt;/p&gt;&lt;p&gt;Option number 2: provide a one million euro standby letter of credit from an A-rated institution as has been discussed.&lt;/p&gt;&lt;p&gt;That number presents a discriminatory and insurmountable barrier to a nonprofit organization such as ours.&lt;/p&gt;&lt;p&gt;I’ve requested an exemption from our Apple representative and was denied.&lt;/p&gt;&lt;p&gt;My question is, since nonprofit organizations are exempt from the core technology fee, what is the rationale for requiring any letter of credit at all?&lt;/p&gt;&lt;p&gt;And what is the objective fairness and reasonableness standard that prevents Apple from increasing that number to 10 million euros or 100 million euros or some arbitrarily high amount that would effectively exclude all alternative app marketplaces at some point in the future?&lt;/p&gt;&lt;/blockquote&gt;
&lt;h2 id=&quot;apples-response-on-marketplace-requirements&quot;&gt;Apple’s response on marketplace requirements&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Kyle Andeer, Apple Vice President of Products and Regulatory Law&lt;/strong&gt; — &lt;a href=&quot;https://gist.github.com/appfairz/83e179243971f65bea36745bc2e15ce5#file-apple_eu_dma_compliance_workshop_whisper_transcript-txt-L2959&quot;&gt;12:33:24&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Again, when we think about alternative marketplaces and this was something we thought about for a long period of time, we wanted to assure that we had credible and accountable operators of stores and we want to have a single set of objective criteria.&lt;/p&gt;&lt;p&gt;We did not want to have special deals.&lt;/p&gt;&lt;p&gt;We did not want to have special assessments because as soon as you do that, you open yourself up to charges of discrimination.&lt;/p&gt;&lt;p&gt;And so what we focused on was what is a set of criteria that we could apply to make sure that the operators of these stores were credible and accountable and responsible.&lt;/p&gt;&lt;p&gt;And those were the two criteria that we established in addition to some of the other things I talked about, which is the other commitments, whether it’s engaged and ongoing monitoring of fraud to comply with laws like the DSA or the GDPR to publishing transparent data collection policies.&lt;/p&gt;&lt;p&gt;All these other things are important, but at the end of the day, if you don’t have an accountable and responsible operator, then those things mean nothing.&lt;/p&gt;&lt;p&gt;And so what we tried to do, and again, I think I answered this in response to an earlier question, we looked to find criteria that would allow us to have some confidence that the operator is someone we can trust to operate a store in the best interest of our users.&lt;/p&gt;&lt;p&gt;There may be others, and so we welcome feedback about what other criteria could we use to accomplish the goal that we’ve set out.&lt;/p&gt;&lt;p&gt;So we’re going to continue and see how things emerge.&lt;/p&gt;&lt;p&gt;Clearly, it hasn’t been an issue for a number of different developers, some of which we’ve heard from today, some of which we know are out there in terms of being able to secure the line of credit to allow them to enter this program.&lt;/p&gt;&lt;/blockquote&gt;
&lt;h2 id=&quot;installation-data-and-privacy&quot;&gt;Installation data and privacy&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Marc Prud’hommeaux, the App Fair Project&lt;/strong&gt; — &lt;a href=&quot;https://gist.github.com/appfairz/83e179243971f65bea36745bc2e15ce5#file-apple_eu_dma_compliance_workshop_whisper_transcript-txt-L5143&quot;&gt;16:04:26&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Hi, Marc Prud’hommeaux from the App Fair Project.&lt;/p&gt;&lt;p&gt;The specific apps that people install and run, including where and when they launch them, can be considered sensitive information when it comes to political and social activity, women’s health and free speech.&lt;/p&gt;&lt;p&gt;Does Apple track personally identifiable information about which apps are installed from third-party marketplaces and where and when they are when the apps are launched?&lt;/p&gt;&lt;p&gt;If so, Apple may be compelled to disclose this information to any of the various legal jurisdictions they operate in.&lt;/p&gt;&lt;p&gt;This could jeopardize vulnerable users.&lt;/p&gt;&lt;p&gt;Will this app installation launch activity still be reported to Apple, even when they opt out of sharing analytics with Apple?&lt;/p&gt;&lt;/blockquote&gt;
&lt;h2 id=&quot;apples-response-on-installation-data&quot;&gt;Apple’s response on installation data&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Gary Davis, Apple Data Protection Officer&lt;/strong&gt; — &lt;a href=&quot;https://gist.github.com/appfairz/83e179243971f65bea36745bc2e15ce5#file-apple_eu_dma_compliance_workshop_whisper_transcript-txt-L5231&quot;&gt;16:08:24&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;In that instance, I’m going to somewhat highlight Apple track record in relation to responding to requests from law enforcement where we consider that the requests are disproportionate or inappropriate and clearly in such circumstances we have shown that we will raise questions about those requests and where appropriate pushback.&lt;/p&gt;&lt;p&gt;Obviously, if a request is lawful and is proportionate, we do our best to assist law enforcement in those circumstances.&lt;/p&gt;&lt;p&gt;Where we do have personal data associated with the download of an app, it is simply the download of an app.&lt;/p&gt;&lt;p&gt;It doesn’t indicate anything about usage.&lt;/p&gt;&lt;p&gt;We do not collect any information about your individual usage of an app in a personally identifiable way.&lt;/p&gt;&lt;p&gt;Some will come from analytics that is shared with developers, but that’s across the population of users, not individual users.&lt;/p&gt;&lt;p&gt;And the same installed information that we have from the App Store will be available for app marketplace downloads as well.&lt;/p&gt;&lt;/blockquote&gt;
&lt;section&gt;&lt;h2 id=&quot;footnote-label&quot;&gt;Footnotes&lt;/h2&gt;
&lt;ol&gt;
&lt;li id=&quot;user-content-fn-1&quot;&gt;
&lt;p&gt;&lt;a href=&quot;https://digital-markets-act.ec.europa.eu/events-poolpage/apple-dma-compliance-workshop-2024-03-18_en&quot;&gt;European Commission: Apple DMA Compliance Workshop, March 18, 2024&lt;/a&gt;. &lt;a href=&quot;https://www.prux.org/posts/apple-dma-compliance-workshop/#user-content-fnref-1&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;&lt;img src=&quot;https://marcprux.goatcounter.com/count?p=%2Frss%2Fposts%2Fapple-dma-compliance-workshop%2F&amp;amp;t=Apple+DMA+Compliance+Workshop&quot; width=&quot;1&quot; height=&quot;1&quot; alt=&quot;&quot; /&gt;</content:encoded></item><item><title>Inversion of Execution in Swift</title><link>https://www.prux.org/posts/inversion-of-execution-in-swift/</link><guid isPermaLink="true">https://www.prux.org/posts/inversion-of-execution-in-swift/</guid><description>A Swift 1.2 experiment in letting the caller choose whether the same function runs synchronously or asynchronously.</description><pubDate>Fri, 06 Mar 2015 21:29:45 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Originally published as a &lt;a href=&quot;https://gist.github.com/marcprux/d7509048a458c445d807&quot;&gt;GitHub gist&lt;/a&gt; on March 6, 2015. This is a historical Swift 1.2 example; the original APIs and syntax are preserved.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Demonstration of an &lt;em&gt;Inversion of Execution&lt;/em&gt; (&lt;strong&gt;IoE&lt;/strong&gt;) technique for how the same function
can be executed either synchronously with a return value or asynchronously with a
callback block depending on an optional trailing &lt;code&gt;iex&lt;/code&gt; (inverted executor) closure.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;When executed without a trailing closure, the result is provided as a return value:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;let&lt;/span&gt;&lt;span&gt; result: &lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;When executed with a trailing closure, the work operation will be synchronously passed as a function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;let&lt;/span&gt;&lt;span&gt; noresult: &lt;/span&gt;&lt;span&gt;Void&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; { f &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    let&lt;/span&gt;&lt;span&gt; result: &lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To execute asynchronously on the main thread, place the callback in the trailing closure:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span&gt;1_000&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span&gt; { f &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    NSOperationQueue.&lt;/span&gt;&lt;span&gt;mainQueue&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;addOperationWithBlock&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        let&lt;/span&gt;&lt;span&gt; result: &lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To execute asynchronously on a background queue:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span&gt;1_000_000&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span&gt; { f &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    dispatch_async&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dispatch_get_global_queue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;QOS_CLASS_BACKGROUND, &lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        let&lt;/span&gt;&lt;span&gt; result: &lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        ...&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The work being performed doesn’t need to be aware of the execution context (modulo synchronization concerns).&lt;/p&gt;
&lt;h2 id=&quot;the-executor-closure&quot;&gt;The executor closure&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;func&lt;/span&gt;&lt;span&gt; sum&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;X&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    numbers&lt;/span&gt;&lt;span&gt;: [&lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    iex&lt;/span&gt;&lt;span&gt;: ((() &lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; Int&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; X)&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; { f &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; as!&lt;/span&gt;&lt;span&gt; X }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; X {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    // Note: compiler crashes if iex is not marked as an optional or implicitly unwrapped optional&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    return&lt;/span&gt;&lt;span&gt; iex&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // The hard stuff: actually perform the calculation&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        return&lt;/span&gt;&lt;span&gt; reduce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;numbers, &lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;an-async-capable-reduce&quot;&gt;An async-capable reduce&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;func&lt;/span&gt;&lt;span&gt; reductor&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;S&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; SequenceType&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; U&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; X&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    sequence&lt;/span&gt;&lt;span&gt;: S,&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    initial&lt;/span&gt;&lt;span&gt;: U,&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    combine&lt;/span&gt;&lt;span&gt;: (U, S.&lt;/span&gt;&lt;span&gt;Generator&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Element&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; U,&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    iex&lt;/span&gt;&lt;span&gt;: ((() &lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; U) &lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; X)&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; { f &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; as!&lt;/span&gt;&lt;span&gt; X }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; X {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    return&lt;/span&gt;&lt;span&gt; iex&lt;/span&gt;&lt;span&gt; { &lt;/span&gt;&lt;span&gt;reduce&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;sequence, initial, combine&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;a-recursive-example&quot;&gt;A recursive example&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;func&lt;/span&gt;&lt;span&gt; naïvefib&lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;X&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;iex&lt;/span&gt;&lt;span&gt;: ((() &lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; Int&lt;/span&gt;&lt;span&gt;) &lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt; X)&lt;/span&gt;&lt;span&gt;!&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; { &lt;/span&gt;&lt;span&gt;$0&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; as!&lt;/span&gt;&lt;span&gt; X }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; X {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    return&lt;/span&gt;&lt;span&gt; iex&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // here we take advantage of the trailing closure having a default synchronous executor:&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // we recursively execute ourselves synchronously by omitting the parameter&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        return&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; 2&lt;/span&gt;&lt;span&gt; ?&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;naïvefib&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span&gt;-&lt;/span&gt;&lt;span&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; +&lt;/span&gt;&lt;span&gt; naïvefib&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span&gt;-&lt;/span&gt;&lt;span&gt; 2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;demonstrating-the-behavior&quot;&gt;Demonstrating the behavior&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;import&lt;/span&gt;&lt;span&gt; XCTest&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;class&lt;/span&gt;&lt;span&gt; InversionOfExecutionDemo&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; XCTestCase &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    func&lt;/span&gt;&lt;span&gt; testExample&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // Running with direct callback provides the work result as a return value&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        let&lt;/span&gt;&lt;span&gt; result1 &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;span&gt;iex&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; { &lt;/span&gt;&lt;span&gt;$0&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        XCTAssertEqual&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;, result1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // You can also exclude the final parameter and it will automatically execute as a return value&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // But note that the type cannot be inferred, so it needs to be declared with the property&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        let&lt;/span&gt;&lt;span&gt; result2: &lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        XCTAssertEqual&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;14&lt;/span&gt;&lt;span&gt;, result2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // Running with an immediate callback provides the work result synchronously as the callback argument&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        let&lt;/span&gt;&lt;span&gt; noreturn: &lt;/span&gt;&lt;span&gt;Void&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;6&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; { f &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            let&lt;/span&gt;&lt;span&gt; result3 &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;immediate: &lt;/span&gt;&lt;span&gt;\(&lt;/span&gt;&lt;span&gt;result3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            XCTAssertEqual&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;9&lt;/span&gt;&lt;span&gt;, result3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // Running with a execution callback will enqueue the work and asynchronously provide it to the callback&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        weak&lt;/span&gt;&lt;span&gt; var&lt;/span&gt;&lt;span&gt; xp &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; expectationWithDescription&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;eventual callback&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        sum&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;Array&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span&gt;1000&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span&gt; { f &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            dispatch_async&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dispatch_get_global_queue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;QOS_CLASS_BACKGROUND, &lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                let&lt;/span&gt;&lt;span&gt; result4 &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;eventual: &lt;/span&gt;&lt;span&gt;\(&lt;/span&gt;&lt;span&gt;result4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                XCTAssertEqual&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;500_500&lt;/span&gt;&lt;span&gt;, result4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                xp&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;fulfill&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // Await the the eventual callback result&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        waitForExpectationsWithTimeout&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; { err &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;span&gt; XCTAssertNil&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        let&lt;/span&gt;&lt;span&gt; concat: &lt;/span&gt;&lt;span&gt;String&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; reductor&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;span&gt;&quot;&quot;&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        XCTAssertEqual&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;abc&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;, concat&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // A small fib calculation should be executed synchronously&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        let&lt;/span&gt;&lt;span&gt; fib5: &lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; naïvefib&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        weak&lt;/span&gt;&lt;span&gt; var&lt;/span&gt;&lt;span&gt; xp2 &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; expectationWithDescription&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;eventual callback&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        // a large fib calculation should be run in the background&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        naïvefib&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;30&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; { f &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            dispatch_async&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;dispatch_get_global_queue&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;QOS_CLASS_BACKGROUND, &lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                let&lt;/span&gt;&lt;span&gt; fib30 &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; f&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                println&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;fib30: &lt;/span&gt;&lt;span&gt;\(&lt;/span&gt;&lt;span&gt;fib30&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                XCTAssertEqual&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;832_040&lt;/span&gt;&lt;span&gt;, fib30&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                xp2&lt;/span&gt;&lt;span&gt;?&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;fulfill&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        waitForExpectationsWithTimeout&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;10&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; { err &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;span&gt; XCTAssertNil&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;err&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;https://marcprux.goatcounter.com/count?p=%2Frss%2Fposts%2Finversion-of-execution-in-swift%2F&amp;amp;t=Inversion+of+Execution+in+Swift&quot; width=&quot;1&quot; height=&quot;1&quot; alt=&quot;&quot; /&gt;</content:encoded></item><item><title>Debugging slow Swift compile times</title><link>https://www.prux.org/posts/debugging-slow-swift-compile-times/</link><guid isPermaLink="true">https://www.prux.org/posts/debugging-slow-swift-compile-times/</guid><description>Slow Swift compile times often come down to type inference. Here&apos;s how to find the offending lines and speed them up with a few well-placed casts.</description><pubDate>Fri, 10 Oct 2014 12:00:00 GMT</pubDate><content:encoded>&lt;p&gt;A number of blog posts have been reporting slow compile times with Swift code. I recently ran into this in a test case where I was creating a fairly simple nested structure, which makes me assume that type inference was the culprit.&lt;/p&gt;
&lt;p&gt;In Xcode 6.1, this tiny bit of code takes an astonishing &lt;em&gt;40 seconds&lt;/em&gt; to compile:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;func&lt;/span&gt;&lt;span&gt; hangCompiler&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;B&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ &lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt; ]], [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;C&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ ]], [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;D&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ &lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt; ]] ]] ]]&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So if you have code with a bunch of nested declarations, it appears the type inference solver can be the thing that takes up a ton of time. How to identify the slow parts of your compile time? I went to the “Report Navigator” tab of Xcode during the slow build, clicked on the currently running build, and clicked the horizontal bar button next to “Compile Perf.swift” to see the Xcode command that is being run. The line is super long, but looks something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;/Applications/Xcode.app/.../usr/bin/swift&lt;/span&gt;&lt;span&gt; -frontend&lt;/span&gt;&lt;span&gt; -c&lt;/span&gt;&lt;span&gt; -primary-file&lt;/span&gt;&lt;span&gt; .../Perf.swift&lt;/span&gt;&lt;span&gt; ...x86_64/Perf.o&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I then copied and pasted it into a new Terminal.app window, and while it was grinding away, I hit &lt;kbd&gt;CTRL&lt;/kbd&gt;+&lt;kbd&gt;\&lt;/kbd&gt; (control + backslash), and it spit out some very useful info:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;1. While type-checking &apos;doPerf&apos; at /tmp/SwiftPerformance/Perf.swift:12:5&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;2. While type-checking expression at [/tmp/SwiftPerformance/Perf.swift:12:5 - line:12:76] RangeText=&quot;[&quot;A&quot;: [ [&quot;B&quot;: [ 1, 2, 3, 4, 5 ]], [&quot;C&quot;: [ ]], [&quot;D&quot;: [ [&quot;A&quot;: [ 1 ]] ]] ]]&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Handy!&lt;/p&gt;
&lt;p&gt;How to speed up the compile time? You can give some hints to the type inference engine by seeding in some ugly casts:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;B&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ &lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;4&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;5&lt;/span&gt;&lt;span&gt; ]] &lt;/span&gt;&lt;span&gt;as&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;String&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;C&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ ]] &lt;/span&gt;&lt;span&gt;as&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;String&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;D&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ [&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [ &lt;/span&gt;&lt;span&gt;1&lt;/span&gt;&lt;span&gt; ]] &lt;/span&gt;&lt;span&gt;as&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;String&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;] ]]&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Those little hints bring the compile time down to .18 seconds.&lt;/p&gt;
&lt;p&gt;So if you are seeing very slow compile times with certain source files, you can isolate the offending lines by pasting in the compiler command and hitting &lt;kbd&gt;CTRL&lt;/kbd&gt;+&lt;kbd&gt;\&lt;/kbd&gt;, and then if nested type inference is the culprit, you can speed it up by sprinkling in some casts here and there.&lt;/p&gt;&lt;img src=&quot;https://marcprux.goatcounter.com/count?p=%2Frss%2Fposts%2Fdebugging-slow-swift-compile-times%2F&amp;amp;t=Debugging+slow+Swift+compile+times&quot; width=&quot;1&quot; height=&quot;1&quot; alt=&quot;&quot; /&gt;</content:encoded></item><item><title>True JavaScript+UIWebView integration in iOS7</title><link>https://www.prux.org/posts/true-javascript-uiwebview-integration-in-ios7/</link><guid isPermaLink="true">https://www.prux.org/posts/true-javascript-uiwebview-integration-in-ios7/</guid><description>iOS7 quietly added a modern Objective-C wrapper around JavaScriptCore, but forgot to document how to get a JSContext from a UIWebView. Here&apos;s the missing trick.</description><pubDate>Tue, 15 Oct 2013 12:00:00 GMT</pubDate><content:encoded>&lt;p&gt;For a long time on the Mac (ever since Mac OS 10.3), we’ve had good, albeit weird, Objective-C and JavaScript interoperability with the &lt;code&gt;WebScriptObject&lt;/code&gt;. With your WebKit &lt;code&gt;WebView&lt;/code&gt;, you could do fun things like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;WebView &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;webView &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [[WebView &lt;/span&gt;&lt;span&gt;alloc&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;init&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;WebScriptObject &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;script &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [webView &lt;/span&gt;&lt;span&gt;windowScriptObject&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt; three &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [script &lt;/span&gt;&lt;span&gt;evaluateWebScript&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;1+2&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;NSAssert&lt;/span&gt;&lt;span&gt;([three &lt;/span&gt;&lt;span&gt;isEqual&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@(&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;as expected&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You could even call Objective-C from JavaScript with bridged objects:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;WebView &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;webView &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [[WebView &lt;/span&gt;&lt;span&gt;alloc&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;init&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;WebScriptObject &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;script &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [webView &lt;/span&gt;&lt;span&gt;windowScriptObject&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;script&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;myObject&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [[SomeObject &lt;/span&gt;&lt;span&gt;alloc&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;init&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span&gt; selectorResult &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [script &lt;/span&gt;&lt;span&gt;evaluateWebScript&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;myObject.invokeASelectorAsIfItWereAFunction()&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I refer to the API as &lt;em&gt;weird&lt;/em&gt; because you would need to do strange stuff like implement the &lt;code&gt;NSObject&lt;/code&gt; informal protocol method:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;BOOL&lt;/span&gt;&lt;span&gt;)isSelectorExcludedFromWebScript:(&lt;/span&gt;&lt;span&gt;SEL&lt;/span&gt;&lt;span&gt;)aSelector&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;in your target class in order for JavaScript to be able to invoke arbitrary methods on your classes. And there’s no real error handling. But overall it works well: Objective-C can call arbitrary JavaScript and JavaScript could call Objective-C. It has been used in countless Mac apps over the years.&lt;/p&gt;
&lt;p&gt;Mac OS 10.5 greatly enhanced the JavaScript interoperability by exposing a C API to the underlying JavaScript engine used by WebKit: JavaScriptCore. With the C API you could directly set and get properties on JavaScript objects, and even create a standalone WebKit-free &lt;code&gt;JSContextRef&lt;/code&gt; instance to run your JavaScript:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;JSGlobalContextRef ctx &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; JSGlobalContextCreate&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;NULL&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;JSValueRef &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;exception &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; NULL&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;JSValueRef result &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; JSEvaluateScript&lt;/span&gt;&lt;span&gt;(ctx&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; JSStringCreateWithCFString&lt;/span&gt;&lt;span&gt;((__bridge CFStringRef)&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;1+2&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; NULL&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; NULL&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; 0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; exception);&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;double&lt;/span&gt;&lt;span&gt; num &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; JSValueToNumber&lt;/span&gt;&lt;span&gt;(ctx&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; exception);&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;NSAssert&lt;/span&gt;&lt;span&gt;(num &lt;/span&gt;&lt;span&gt;==&lt;/span&gt;&lt;span&gt; 3.0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;works, but C is soooooo tedious&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;JSGlobalContextRelease&lt;/span&gt;&lt;span&gt;(ctx);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;WebScriptObject&lt;/code&gt; class added a reference to the underlying &lt;code&gt;JSObjectRef&lt;/code&gt; instance so that you could directly interface with the JavaScript runtime that was attached to a &lt;code&gt;WebView&lt;/code&gt;. The C API was tedious, but Objective-C wrapper libraries such as Patrick Geiller’s delightful &lt;em&gt;JSCocoa&lt;/em&gt; project (&lt;a href=&quot;https://github.com/parmanoir/jscocoa/&quot;&gt;https://github.com/parmanoir/jscocoa/&lt;/a&gt;) arose to reduce the pain. It was adopted by many Mac apps and frameworks (such as &lt;em&gt;Acorn&lt;/em&gt; and &lt;em&gt;Coda&lt;/em&gt;) as a bridge that allowed plugins to be written in JavaScript.&lt;/p&gt;
&lt;p&gt;On iOS’ &lt;code&gt;UIWebView&lt;/code&gt;, in contrast, JavaScript interoperability has always been limited to exactly one method: the notorious:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;-&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;NSString&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;)stringByEvaluatingJavaScriptFromString:(&lt;/span&gt;&lt;span&gt;NSString&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;)script&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This method would let you do things like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;UIWebView &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;webView &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [[UIWebView &lt;/span&gt;&lt;span&gt;alloc&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;init&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;NSString&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;result &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [webView &lt;/span&gt;&lt;span&gt;stringByEvaluatingJavaScriptFromString&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;1+2&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;NSAssert&lt;/span&gt;&lt;span&gt;([result &lt;/span&gt;&lt;span&gt;isEqualToString&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;correct, but strings are kind of unsatisfying&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Strings in, strings out. Back when I was writing the Stanza e-book reader app, this was how I performed the vast majority of interaction between the UI and the underlying web renderer. But there is no interoperability between the Objective-C world and the JavaScript world, so you need to cobble together some kind of bridge on your own, which many people did. As far as I can tell, almost everyone that made a two-way bridge between JavaScript and Objective-C used a hacky trick with setting the document’s URL to some encoded data, and then intercepting that data in the &lt;code&gt;UIWebView&lt;/code&gt;’s delegate:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;-&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;BOOL&lt;/span&gt;&lt;span&gt;)webView:(UIWebView &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;)webView shouldStartLoadWithRequest:(&lt;/span&gt;&lt;span&gt;NSURLRequest&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;)request navigationType:(UIWebViewNavigationType)navigationType&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The delegate would then decode the data encoded in the URL and do some stuff in the Objective-C world, and then call back into an asynchronous handler using the &lt;code&gt;stringByEvaluatingJavaScriptFromString&lt;/code&gt; method again. This seems to be the path that all the JavaScript-to-native bridge technologies like PhoneGap/Apache Cordova take. It works, but it is painful because everything is asynchronous, there’s no error handling, and object marshaling is all manual and error-prone.&lt;/p&gt;
&lt;p&gt;And that was the state of the art until a few months ago, when Apple quietly (&lt;a href=&quot;http://www.steamclock.com/blog/2013/05/apple-objective-c-javascript-bridge/&quot;&gt;http://www.steamclock.com/blog/2013/05/apple-objective-c-javascript-bridge/&lt;/a&gt;) slipped a new Objective-C JavaScriptCore wrapper into the WebKit source code. It basically looks like a modernized form of the venerable Mac &lt;code&gt;WebScriptObject&lt;/code&gt; classes, heavily influenced by JSCocoa.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;WebView &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;webView &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [[WebView &lt;/span&gt;&lt;span&gt;alloc&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;init&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;JSContext &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;ctx &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [JSContext &lt;/span&gt;&lt;span&gt;contextWithJSGlobalContextRef&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;webView.mainFrame.globalContext&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;JSValue &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;three &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [ctx &lt;/span&gt;&lt;span&gt;evaluateScript&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;1+2&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;NSAssert&lt;/span&gt;&lt;span&gt;([[three &lt;/span&gt;&lt;span&gt;toNumber&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;isEqual&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@(&lt;/span&gt;&lt;span&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;It Is As It Was&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also do awesome things by setting a block as a property in the context (or any JavaScript object) and it will automatically be exposed to the JavaScript runtime as a function. Behold:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;[ctx &lt;/span&gt;&lt;span&gt;evaluateScript&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;console.log(&apos;this is a log message that goes nowhere :(&apos;)&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;ctx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;console&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;][&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;log&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; ^&lt;/span&gt;&lt;span&gt;(JSValue &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;msg) {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    NSLog&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;JavaScript &lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt; log message: &lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; [JSContext &lt;/span&gt;&lt;span&gt;currentContext&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; msg);&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;[ctx &lt;/span&gt;&lt;span&gt;evaluateScript&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;console.log(&apos;this is a log message that goes to my Xcode debug console!!!! :)&apos;)&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The possibilities are endless. And at the “Integrating JavaScript into Native Apps” session at WWDC this year (&lt;a href=&quot;http://asciiwwdc.com/2013/sessions/615&quot;&gt;http://asciiwwdc.com/2013/sessions/615&lt;/a&gt;), I was excited to hear them mention that this new API is also available in iOS7! iOS has never even exposed the C-level JavaScriptCore APIs, so this should be quite a breakthrough in hybrid apps that want to interoperate between JavaScript and Objective-C.&lt;/p&gt;
&lt;p&gt;And while the API is indeed publicly available in iOS7 now, there are a few problems that indicate that this feature isn’t quite completely baked. First off, it is &lt;em&gt;completely undocumented&lt;/em&gt;. Search the Xcode API docs for &lt;code&gt;contextWithJSGlobalContextRef&lt;/code&gt;, and all you will find is the API diffs. Heck, search the internet (as of this writing) for &lt;code&gt;contextWithJSGlobalContextRef&lt;/code&gt;, and all you will find is links to API diffs, some WebKit source code change logs, and a couple of sites in Chinese. Literally &lt;em&gt;no one&lt;/em&gt; is using this yet. I guess they just forgot the docs. Your best bet is to probably read the header files (e.g., &lt;code&gt;/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/System/Library/Frameworks/JavaScriptCore.framework/Headers/JSContext.h&lt;/code&gt;) and browse the source code (e.g., &lt;a href=&quot;https://github.com/mirrors/WebKit/blob/master/Source/JavaScriptCore/API/JSContext.mm&quot;&gt;JSContext.mm in WebKit&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The other, more serious, problem is that they appear to have forgotten to document how to get a &lt;code&gt;JSContext&lt;/code&gt; from a &lt;code&gt;UIWebView&lt;/code&gt;, which is critical because without it, all you have is a stand-alone &lt;code&gt;JSContext&lt;/code&gt; object. And while this could be useful for loading small stand-alone JavaScript libraries that don’t rely on a browser, it doesn’t let you drive any of your browser UI with Objective-C and basically makes it an isolated script interpreter. A number of people have complained about this limitation on &lt;a href=&quot;https://devforums.apple.com/&quot;&gt;https://devforums.apple.com&lt;/a&gt;, but thus far no one seems to have come up with any solution. After a little poking around, however, I found that you can get a hold of a &lt;code&gt;UIWebView&lt;/code&gt;’s &lt;code&gt;JSContext&lt;/code&gt; object using a KVO-compliant path, like so:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;JSContext &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;ctx &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [webView &lt;/span&gt;&lt;span&gt;valueForKeyPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;documentView.webView.mainFrame.javaScriptContext&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And it is thereby now possible to combine the new power of the JavaScriptCore bindings with the flexibility of UIWebView content. For example, here’s a &lt;code&gt;UIViewController&lt;/code&gt; that adds a &lt;code&gt;UIWebView&lt;/code&gt; and animates the background colors via Objective-C:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span&gt;&lt;span&gt;//&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;//  ViewController.m&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;//  UIWVScript&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;//&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;//  Created by Marc Prud&apos;hommeaux on 10/15/13.&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;//  Copyright © 2013 impathic. All rights reserved.&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;//&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;#&lt;/span&gt;&lt;span&gt;import&lt;/span&gt;&lt;span&gt; &quot;&lt;/span&gt;&lt;span&gt;ViewController.h&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;@import JavaScriptCore;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;@import ObjectiveC;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;@implementation&lt;/span&gt;&lt;span&gt; ViewController&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;- (&lt;/span&gt;&lt;span&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;viewDidLoad&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [&lt;/span&gt;&lt;span&gt;super&lt;/span&gt;&lt;span&gt; viewDidLoad&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    // set up a full-screen UIWebView&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    UIView &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;view &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; self&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;view&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    UIWebView &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;webView &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [[UIWebView &lt;/span&gt;&lt;span&gt;alloc&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;init&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [view &lt;/span&gt;&lt;span&gt;addSubview&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;webView&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    webView&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;translatesAutoresizingMaskIntoConstraints&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; view&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;translatesAutoresizingMaskIntoConstraints&lt;/span&gt;&lt;span&gt; =&lt;/span&gt;&lt;span&gt; NO&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;NSString&lt;/span&gt;&lt;span&gt; *&lt;/span&gt;&lt;span&gt;fitFormat &lt;/span&gt;&lt;span&gt;in&lt;/span&gt;&lt;span&gt; @[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;H:|-0-[webView]-0-|&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;V:|-0-[webView]-0-|&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        [view &lt;/span&gt;&lt;span&gt;addConstraints&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;[NSLayoutConstraint &lt;/span&gt;&lt;span&gt;constraintsWithVisualFormat&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;fitFormat&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                                                                   options&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                                                                   metrics&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;nil&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;                                                                     views&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;NSDictionaryOfVariableBindings&lt;/span&gt;&lt;span&gt;(webView)]&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    JSContext &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;ctx &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [webView &lt;/span&gt;&lt;span&gt;valueForKeyPath&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;documentView.webView.mainFrame.javaScriptContext&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    NSAssert&lt;/span&gt;&lt;span&gt;([ctx &lt;/span&gt;&lt;span&gt;isKindOfClass&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;[JSContext &lt;/span&gt;&lt;span&gt;class&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;could not find context in web view&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [ctx &lt;/span&gt;&lt;span&gt;evaluateScript&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;console.log(&apos;this is a log message that goes nowhere :(&apos;)&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    ctx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;console&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;][&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;log&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; ^&lt;/span&gt;&lt;span&gt;(JSValue &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;msg) {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        NSLog&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;JavaScript &lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt; log message: &lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; [JSContext &lt;/span&gt;&lt;span&gt;currentContext&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; msg);&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [ctx &lt;/span&gt;&lt;span&gt;evaluateScript&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;console.log(&apos;this is a log message that goes to my Xcode debug console :)&apos;)&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    JSValue &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;style &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; ctx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;document&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;][&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;body&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;][&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;style&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;]; &lt;/span&gt;&lt;span&gt;// yes, this works: KVC FTW!&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    NSAssert&lt;/span&gt;&lt;span&gt;(style&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;there should be a style element in the document body&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    // set up the body&apos;s style so all CSS changes are animated&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    NSTimeInterval&lt;/span&gt;&lt;span&gt; colorChangeInterval &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; 1.0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    style&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;transition-timing-function&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;linear&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    style&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;transition-delay&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; @(&lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    style&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;transition-duration&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;NSString&lt;/span&gt;&lt;span&gt; stringWithFormat&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @(colorChangeInterval)&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    // this sets up a JavaScript-function-to-Cocoa-block bridge&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    ctx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;setRandomColor&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; ^&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        style&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;background&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;] &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;NSString&lt;/span&gt;&lt;span&gt; stringWithFormat&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;hsl(&lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @(&lt;/span&gt;&lt;span&gt;arc4random&lt;/span&gt;&lt;span&gt;() &lt;/span&gt;&lt;span&gt;%&lt;/span&gt;&lt;span&gt; 255&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;50&lt;/span&gt;&lt;span&gt;%&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;50&lt;/span&gt;&lt;span&gt;%&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;        NSLog&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;fading to color: &lt;/span&gt;&lt;span&gt;%@&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; style&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;background&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;]); &lt;/span&gt;&lt;span&gt;// notably, this will not output hsl format, but instead rgb format like: &quot;rgb(63, 191, 179)&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    JSValue &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;setColorFunction &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; ctx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;setRandomColor&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    NSAssert&lt;/span&gt;&lt;span&gt;([setColorFunction &lt;/span&gt;&lt;span&gt;isObject&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;it was a block, but now it should be a bridged JSValue function object&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    // we can call our background-setting code directly via JavaScript&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [setColorFunction &lt;/span&gt;&lt;span&gt;callWithArguments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;nil&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    JSValue &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt;setIntervalFunction &lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span&gt; ctx&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;@&quot;&lt;/span&gt;&lt;span&gt;setInterval&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;]; &lt;/span&gt;&lt;span&gt;// grab the built-in setInterval repeating timer function&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    NSAssert&lt;/span&gt;&lt;span&gt;([setIntervalFunction &lt;/span&gt;&lt;span&gt;isObject&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @&quot;&lt;/span&gt;&lt;span&gt;setInterval should have been a function object&lt;/span&gt;&lt;span&gt;&quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    // now set up a repeating timer in JavaScript that calls our background-changing Cocoa function&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;    [setIntervalFunction &lt;/span&gt;&lt;span&gt;callWithArguments&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span&gt;@[setColorFunction&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; @(colorChangeInterval &lt;/span&gt;&lt;span&gt;*&lt;/span&gt;&lt;span&gt; 1000.&lt;/span&gt;&lt;span&gt;)]&lt;/span&gt;&lt;span&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;span&gt;@end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;img src=&quot;https://marcprux.goatcounter.com/count?p=%2Frss%2Fposts%2Ftrue-javascript-uiwebview-integration-in-ios7%2F&amp;amp;t=True+JavaScript%2BUIWebView+integration+in+iOS7&quot; width=&quot;1&quot; height=&quot;1&quot; alt=&quot;&quot; /&gt;</content:encoded></item><item><title>Lexcycle has been acquired by Amazon.com!</title><link>https://www.prux.org/posts/lexcycle-acquired-by-amazon/</link><guid isPermaLink="true">https://www.prux.org/posts/lexcycle-acquired-by-amazon/</guid><description>The original 2009 announcement that Amazon had acquired Lexcycle, the company behind the Stanza ebook reader.</description><pubDate>Mon, 27 Apr 2009 12:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Originally published on the Lexcycle blog on April 27, 2009, under my account and signed by Marc, Neelan, and Abe. Reproduced from the &lt;a href=&quot;https://web.archive.org/web/20090430101437/http://www.lexcycle.com/lexcycle_acquired_by_amazon&quot;&gt;archived announcement&lt;/a&gt;. The plans and contact details below are from that time.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We are very excited to announce that Lexcycle has been acquired by Amazon.com!&lt;/p&gt;
&lt;p&gt;It has been an amazing experience to see Stanza grow out of our brainstormed ideas into one of the most downloaded iPhone applications. Throughout this period we have attempted to listen to and innovate for our customers to provide a great ebook reading experience.&lt;/p&gt;
&lt;p&gt;We are not planning any changes in the Stanza application or user experience as a result of the acquisition. Customers will still be able to browse, buy, and read ebooks from our many content partners. We look forward to offering future products and services that we hope will resonate with our passionate readers.&lt;/p&gt;
&lt;p&gt;We are excited to join forces with a company that has innovated on behalf of readers for over a decade and is a pioneer in ebooks. Like Amazon, we believe there is a lot of innovation ahead for ebooks and we could not think of a better company to join during this exciting time.&lt;/p&gt;
&lt;p&gt;As always, we encourage people to ask questions or provide feedback through our forums at &lt;code&gt;forum.lexcycle.com&lt;/code&gt; or directly to us at &lt;code&gt;info@lexcycle.com&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Best,&lt;br /&gt;
Marc, Neelan and Abe&lt;/p&gt;&lt;img src=&quot;https://marcprux.goatcounter.com/count?p=%2Frss%2Fposts%2Flexcycle-acquired-by-amazon%2F&amp;amp;t=Lexcycle+has+been+acquired+by+Amazon.com%21&quot; width=&quot;1&quot; height=&quot;1&quot; alt=&quot;&quot; /&gt;</content:encoded></item></channel></rss>