Skip to content

Instantly share code, notes, and snippets.

@yoshyosh
Created April 14, 2015 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshyosh/b5d0a330e7c3abd9acc2 to your computer and use it in GitHub Desktop.
Save yoshyosh/b5d0a330e7c3abd9acc2 to your computer and use it in GitHub Desktop.
---
layout: launch-post
title: "Favorite Swift Tips & Tricks of 2014"
subtitle: ""
excerpt: 'To celebrate the end of the year and the first 6 months of Swift, we asked ten of our Swift-est friends to share their favorite tips & tricks of 2014.'
tagline: "Favorite Swift Tips & Tricks of 2014"
tags: tips
ogType: Article
metaImage: "/assets/news/favorite-tips-cover.jpg"
---
<div class="swift-tips-tricks-container">
_To celebrate the end of the year and the first 6 months of Swift, we asked ten of our Swift-est friends for their favorite Swift tips & tricks of 2014. Thanks to everyone for sharing!_
<hr>
<div class="tip-container" id="natasha">
<img src="/assets/cases/natasha.jpg" />
<div class="tip-header-container">
<h2>Natasha Murashev</h2>
<h3>iOS Engineer at Capital One and blogger at <a href="http://natashatherobot.com">Natasha The Robot</a></h3>
</div>
Swift allows for a much more functional programming approach when structuring iOS applications, so I've been learning a lot about functional programming in order to make better decisions for my Swift code. Here are some of my favorite functional swift resources so far:
[Functional Functions](http://commandshift.co.uk/blog/2014/10/10/functional-functions/) - This one easy change to your functions will make your code a lot more independent and testable.
[Wrapper Types](http://www.objc.io/snippets/8.html) - I love how readable and safe typealiasing can make code
[The Design of Types](http://www.swiftcast.tv/articles/the-design-of-types) - More on designing your program with the right types! "Designing the right types for your problem is a great way to help the compiler debug your program"
[Practical Use for Curried Functions in Swift](http://drewag.me/posts/practical-use-for-curried-functions-in-swift) - In Haskell, a function can only take one parameter! While Swift is not that extreme, it does allow for "currying" or partially applying a function, which leads to more re-useable pieces of code.
[Error Handling in Swift: Might and Magic](http://nomothetis.svbtle.com/error-handling-in-swift) - I love the simplicity, readability, and safety of this approach to error handling, especially compared to what we usually do in Objective-C.
[Railway oriented programming (video here)](http://fsharpforfunandprofit.com/posts/recipe-part2/) - This is in F#, but it's a great way to think about error handling in code - just design for the happy path!
[Functional Programming in Swift](http://www.objc.io/books/) - This is a bit more advanced book on the functional swift topic, but I've found myself finding something new here every time I re-read it. Great for those who'd like a lot more depth and exposure into functional world and how to apply the concepts to iOS code.
_Watch Natasha’s video: [Building TableViews in Swift & iOS8](http://realm.io/news/building-tableviews-swift-ios8/)_
</div>
<hr>
<div class="tip-container" id="chris">
<img src="/assets/cases/chrisE.jpg" />
<div class="tip-header-container">
<h2>Chris Eidhof</h2>
<h3>Creator of [objc.io](http://www.objc.io) and [Functional Programming in Swift](http://www.objc.io/books)</h3>
</div>
**Functional Quicksort** — the following variant of Quicksort will definitely not win any speed prize. Most real implementations of Quicksort will use constant memory. This snippet, however, is one of the shortest and clearest ways to write Quicksort:
{% highlight swift %}
func qsort(input: [Int]) -> [Int] {
if let (pivot, rest) = input.decompose {
let lesser = rest.filter { $0 < pivot }
let greater = rest.filter { $0 >= pivot }
return qsort(lesser) + [pivot] + qsort(greater)
} else {
return []
}
}
{% endhighlight %}
It builds on the [decompose](http://www.objc.io/snippets/1.html) snippet: if the array is not empty, it uses the first element as the pivot, and seperates the array into two new arrays: the first containing only smaller elements, and the second containing only larger (or equal) elements. Then it sorts the smaller elements, appends the pivot element, and finally appends the sorted larger elements.
_Originally appeared on http://www.objc.io/snippets/3.html_
_Watch Chris’ video: [Functional Programming in Swift](http://realm.io/news/functional-programming-swift-chris-eidhof/)_
</div>
<hr>
<div class="tip-container" id="austin">
<img src="/assets/cases/austin.jpg" />
<div class="tip-header-container">
<h2>Austin Zheng</h2>
<h3>Senior Software Engineer at [LinkedIn](https://www.linkedin.com/)</h3>
</div>
A nifty (if rather useless) Swift trick that I like is the fact that you can set a delegate to an instance of an anonymous class by creating a closure that defines a conforming class, having that closure return a new instance of that class, and running the closure. That sounds complicated, and so I have a bit of [sample code here](https://gist.github.com/austinzheng/8fda3f61e1fd06383928).
_You can watch Austin’s videos: [Swift: Enums, Pattern Matching & Generics](http://realm.io/news/swift-enums-pattern-matching-generics/) & [Lessons Learned Building “2048” in Swift](http://realm.io/news/swift-2048/), or check out his [blog](http://austinzheng.com)._
</div>
<hr>
<div class="tip-container" id="clay">
<img src="/assets/cases/clayS.jpg" />
<div class="tip-header-container">
<h2>Clay Smith</h2>
<h3>Senior Software Engineer at [PagerDuty](http://pagerduty.com/)</h3>
</div>
Here's my essential bash alias for dealing with all problems xcode (and particularly sourcekit issues):
{% highlight bash %}
alias sourcekitsad='rm -rf ~/Library/Developer/Xcode/DerivedData'
{% endhighlight %}
_You can watch Clay’s [12 Apps of Swiftmas](http://realm.io/news/the-12-apps-of-swiftmas/) video, or check him out on [GitHub](http://github.com/smithclay)._
</div>
<hr>
<div class="tip-container" id="michael">
<img src="/assets/cases/mrh.jpg" />
<div class="tip-header-container">
<h2>Michael Helmbrecht</h2>
<h3>UX designer and iOS developer at [Motiv](http://www.mymotiv.com/)</h3>
</div>
Favorite bit of trivia would probably be that String does not conform to Printable, so there’s technically not a guarantee that string interpolation works with a String. I think it only works because String is bridged to NSString, which does have a -description method.
_You can watch Michael’s [12 Apps of Swiftmas](http://realm.io/news/the-12-apps-of-swiftmas/) presentation, or check out his [blog](http://www.mrh.is/writing/)._
</div>
<hr>
<div class="tip-container" id="david">
<img src="/assets/cases/davidK.jpg" />
<div class="tip-header-container">
<h2>David Kobilnyk</h2>
<h3>Software engineer at [ShopRunner](https://www.shoprunner.com).</h3>
</div>
Swift seems elegant enough as a language that I don't particularly find myself needing to hack with special tricks. I do like using enums together with raw types, like this:
{% highlight swift %}
public enum ReminderTimeType: String {
case Evening = "this evening"
case Tomorrow = "tomorrow"
case Weekend = "this weekend"
case NextWeek = "next week"
case CoupleWeeks = "in a couple weeks"
case CoupleMonths = "in a couple months"
case Someday = "someday"
public static let array = [
Evening, Tomorrow, Weekend, NextWeek, CoupleWeeks, CoupleMonths, Someday
]
public static let rawArray = array.map { $0.rawValue }
}
{% endhighlight %}
_You can watch David’s video: [Converting Objective-C to Swift](http://realm.io/news/converting-objc-to-swift/), or check him out on [GitHub](https://github.com/davidkobilnyk)._
</div>
<hr>
<div class="tip-container" id="alexis">
<img src="/assets/cases/alexisG.jpg" />
<div class="tip-header-container">
<h2>Alexis Gallagher</h2>
<h3>Senior iOS developer</h3>
</div>
My favorite piece of Swift trivia, trick, resource, or anything? My two favorite tricks of the moment: On the REPL, the handy function
{% highlight swift %}
func typeof<T>(@autoclosure () -> T) -> Any.Type { return T.self }
{% endhighlight %}
can be used to get the static type of an expression without evaluating the expression. And the undocumented private function `_stdlib_getDemangledTypeName` can be used to get the type name of any instance value.
_You can watch Alexis’ [12 Apps of Swiftmas](http://realm.io/news/the-12-apps-of-swiftmas/) presentation, or follow him on Twitter [@alexisgallagher](https://twitter.com/alexisgallagher)._
</div>
<hr>
<div class="tip-container" id="jp">
<img src="/assets/cases/jp.jpg" />
<div class="tip-header-container">
<h2>JP Simard</h2>
<h3>iOS Engineer at Realm</h3>
</div>
You can get all the private Swift standard library functions by digging through `libswiftCore.dylib` using the `nm` tool like this:
{% highlight bash %}
cd `xcode-select -p`/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx
nm -a libswiftCore.dylib | grep "T _swift_stdlib_"
{% endhighlight %}
Which reveals some very useful functions like `_stdlib_getTypeName()`, `_stdlib_demangleName()` and `_stdlib_conformsToProtocol()` among a few others.
_You can watch JP’s videos: [Swift for JavaScript Developers](http://realm.io/news/swift-for-javascript-developers/) & [Swift for Rubyists](http://realm.io/news/swift-for-rubyists/)._
</div>
<hr>
<div class="tip-container" id="warren">
<img src="/assets/cases/warren.jpg" />
<div class="tip-header-container">
<h2>Warren Moore</h2>
<h3>Author of <a href="http://metalbyexample.com">Metal By Example</a></h3>
</div>
This is kind of a hack, but it turns out that Swift allows you to treat a homogeneous array of structs as a pointer to the member type of the struct, when all the members of the struct are of the same type (e.g., Float). I use this to succinctly create Metal buffers of vertex data without a lot of unsightly casting.
</div>
<hr>
<div class="tip-container" id="mustafa">
<img src="/assets/cases/mustafa.jpg" />
<div class="tip-header-container">
<h2>Mustafa Furniturewala</h2>
<h3>Software Engineer at <a href="https://www.coursera.org">Coursera</a></h3>
</div>
My favorite Swift resource is this: [Swifter](http://swifter.natecook.com) (great reference guide when I'm coding)
Favorite piece of Trivia: You can enable dynamic dispatch on any swift func to enable objective-C style dynamic dispatching.
</div>
<hr>
</div>
{% include newsletter-swift-videos.html %}
<div class='shareaholic-canvas' data-app='share_buttons' data-app-id='7453434'></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment