HNHacker News
TopNewBestAskShowJobs

katfang

22 karma · joined September 12, 2014

submissionscomments
katfang··on A Video Walkthrough of Swift Fundamentals
I agree! Eithers are cool, too. What I'm really excited about is that these sorts of concepts are starting to find their way into languages like Swift. Getting built-in support for Eithers would be super awesome.

In the meantime, you can vaguely simulate it with named tuples.

  let response = (error: "Could not connect to server", success: false)

It's not quite the same, but at least there's an easy way to bundle your data together.

edit: I like Someone's suggestion to use and discussion about using enums as union types below.

katfang··on A Video Walkthrough of Swift Fundamentals
This is my first screencast! I had a lot of fun making it, and I hope you enjoy it.

One of my favorite parts of Swift is the optional concept, which separates variables which are never null from variables that can sometimes be null. This helps prevent errors from trying to use objects that don't exist. The syntax involves `!` and `?` which can make your code seem excited and confused, but it's definitely a step toward making your code safer!

For example, say you have a UI element that might have a label. If indeed it has a label, and you wanted to set the color of it to red, you can store your label like this:

`var label: UILabel?`

which signals that it may not exist. Then to set the color, you would do this:

`label?.textColor = UIColor.redColor()`

The question mark says if it isn't there, then don't assign the new color. We don't need to check the case where the label does not exist because Swift automatically does it for us!

katfang··on Screencast: Learn Swift in 40 Minutes
Hey! I had a lot of fun recording this. Hope it's helpful, and let me know what you think! This is my first screencast, so I'm all ears for feedback.