Swift Tips & Tricks

URL Pattern Matching
John Patrick Morgan shares his experiments on different approaches to use pattern matching to build a URL routing library. Examples include using a pattern matching operator, tuples, linked list of enums and a blended approach.

Random Number Generators in Swift
Matt Gallagher defines a RandomGenerator protocol and follows by implementing a collection of unique random number generators. The examples include wrappers around OS X built-in algorithms, as well as Swift implementations of popular algorithms.

sizeof and sizeofValue in Swift
The familiar sizeof and sizeofValue operators in C/Objective-C are functions in Swift. Wei Wang explains how you can use the Swift implementations to get results that mimic what we've become accustomed to in their C counterparts.

Convert Swift structs to JSON
Emil Loer writes a protocol to enable conversion of any Swift struct into a JSON object. Of particular interest is the use of the Swift reflection API (Mirror struct) to "discover and iterate over the list of properties in the struct."

Swift Open Source Projects & Tools

Track: Thread Safe Disk and Memory Cache
Implementations of disk and memory caches based on Least Recently Used (LRU) algorithm. You can manage a cache through functions to limit size, age of entries and memory usage.

Creating Objective-C and C++ Packages Using Swift Package Manager
Ankit Aggarwal demonstrates how to use the Swift Package Manager to create a package containing Objective-C and Swift and another with C++ and Swift. This is another classic Ankit post - intriguing, in-depth and code-centric.

JLRoutes: URL Routing Library
Joel Levin wrote this block-based API to "handle complex URL schemes in your application without having to do any URL or string parsing of any kind." The project can be used on iOS or OS X.

Regex: Regular Expressions for Swift
In this regular expression library you'll find a number of options beyond simple regex matching, including: groups and named groups; replace and replace using custom function; split and split with groups.

What's Buzzing in Swift Evolution?

Here's Erica Sadun, author of the Swift Developer's Cookbook, Swift Documentation Markup, Swift Playground Secrets, and a frequent Swift blogger at ericasadun.com, pointing you at some of this week's lively discussions:

Mapping the Future
Nate Cook has been waiting for a pretty long time for his mapValue pull request to get a little core team love and this was finally the week. Dave Abrahams wrote: "Done. I was just waiting until we could assign it SE-0100. Congratulations! ;-)" And with that the Swift Evolution process hit 100 language proposals, although the first six were in-place when Swift went open source.

Freedom of Association
This is a particularly fascinating proposal with a novel twist and an active conversation. Unlike most proposals, "Remove associated type inference" adds more work for the programmer while simplifying things at the compiler end. Reception has been mixed to cold, even among Apple's standard library team.

With or Without You
Closure-based set-up has been discussed on Swift Evolution for months but the topic suddenly picked up steam this week on Twitter. So Brent Royal Gordon and I are breathing some new life into an old proposal. We're getting it into shape for a formal SE draft, where it should hopefully simplify value type constant modification and Cocoa object initialization. It's an elegant way to streamline code that otherwise forms blocky repetitive clumps.

On the Case!
Possibly the least popular proposal in the entire open source history of Swift Evolution, SE-0098 establishes consistent keyword casing rules. If it were a strict majority vote system, this proposal would have gone down in flames several days back. As is, the core team must decide whether to formalize an existing standard by removing outlier cases or listen to the larger community and introduce flexible naming that's more future proof for Swift language extensions.

What's the Frequency, Kenneth?
A fascinating thread about static vs. dynamic dispatch that really dives deep into Swift, its design, and where it's headed. As Dave Abrahams writes in-thread, "Protocols unify static and dynamic dispatch, and one is not inherently better than the other. They have different strengths. Protocol-oriented programming is about leveraging those strengths appropriately, not about a mandate to choose one or the other form of dispatch."