Live data from Hacker News

A Video Walkthrough of Swift Fundamentals

firebase.com

1–10 of 20 posts

Re: A Video Walkthrough of Swift Fundamentals

#2
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!

Re: A Video Walkthrough of Swift Fundamentals

#3
post #2

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 towar…

Hello,

While I do not have time to go over the whole video, surely you could have come up with much more serious reasons why Swift might be preferable to Objective C than "uhm, it has no namespaces and looks strange". Also, there were no reasons why "not to use Swift [yet]", making sure to mention the terrible state the developer tools are at this point, as well as many many missing language features and other quirks which make it a language not yet ready, in my experienced opinion, for large projects, or when some C/C++ operability is required.

Have fun coding and making tutorials!

Re: A Video Walkthrough of Swift Fundamentals

#6
post #2

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 towar…

>One of my favorite parts of Swift is the optional concept

What if you need the fallback value to be an error possessing information about what went wrong?

    data Maybe a = Just a | Nothing

    data Either a b = Left a | Right b

Re: A Video Walkthrough of Swift Fundamentals

#7
post #2

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 towar…

>One of my favorite parts of Swift is the optional concept What if you need the fallback value to be an error possessing information about what went wrong? data Maybe a = Just a | Nothing data Either a b = Left a | Right b

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.

Re: A Video Walkthrough of Swift Fundamentals

#8
post #7

Earlier quoted context omitted.

>One of my favorite parts of Swift is the optional concept What if you need the fallback value to be an error possessing information about what went wrong? data Maybe a = Just a | Nothing data Either a b = Left a | Right b

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…

Other approach: use enums as union types. From https://developer.apple.com/library/ios/documentation/Swift/...:

  enum Barcode {
    case UPCA(Int, Int, Int, Int)
    case QRCode(String)
  }
This can be read as:

“Define an enumeration type called Barcode, which can take either a value of UPCA with an associated value of type (Int, Int, Int, Int), or a value of QRCode with an associated value of type String.”

So, you can do

  enum ResultOrError {
    case Result(ReturnValue)
    case Error(int,String)
  }
Unfortunately, enums and generics do not mix, AFAIK, so it looks clumsy.

I think the reason for the special syntax of '?.' is that the designers think it is a very common case, at least in Swift programs.

And yes, I would like to see a better syntax for that, too. A generalization of C's short-hand 'if'

   flag ? trueExpression : falseExpression;
For switch statements would be cool.

Re: A Video Walkthrough of Swift Fundamentals

#9
post #7

Earlier quoted context omitted.

>One of my favorite parts of Swift is the optional concept What if you need the fallback value to be an error possessing information about what went wrong? data Maybe a = Just a | Nothing data Either a b = Left a | Right b

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…

> Getting built-in support for Eithers would be super awesome.

Wait, what? Swift has sum types (ie. enums), so there's no need for it to be built in.

Re: A Video Walkthrough of Swift Fundamentals

#10
> One of my favorite things is that methods are way less awkard than Objective-C. It looks, you know, like other programming languages now.

This made me laugh. I've also suffered from them. I tried to convince myself to like them when I started learning Objective-C, but soon I failed.

Post reply on HN