Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pattern match with optionals in Swift?

Tags:

swift

I'm not too fond of the if let syntax with optionals and I'm trying to see if I can get pattern matching to work instead. I'm trying the following code in a playground, but not seeing any output on the println statements. What am I doing wrong?

let one:Int? = 1

switch one {
case .Some(let numeral):
    println("Caught a \(numeral)")
default:
    println("Nothing to catch")
}
like image 813
Jamie Forrest Avatar asked Mar 20 '23 10:03

Jamie Forrest


1 Answers

A little bit out of context, but: Playground doesn't print the println() statements in the right column. You can write again the variable that you want to read:

...
case .Some(let numeral):
    println("Caught a \(numeral)")
    numeral
...

In this case you'll see {Some 2}.

Or you can open the Assistant Editor (View -> Assistant Editor -> Show Assistant Editor) and read the Console output to read the println() evaluated.

EDIT after Xcode 6 beta-5

With Xcode 6 beta-5, you can finally println(), you'll see the text in the right column.

like image 109
JJSaccolo Avatar answered Apr 01 '23 08:04

JJSaccolo