Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I see exceptions in Swift playground?

I was playing around with Swift, and have this code in a playground

class Foo {
    let value: String
    init(value: String!)
    {
        self.value = value
    }
}

let x : String? = nil
let foo = Foo(value: x)

The bottom line should throw an exception in the initializer, because I am unwrapping x which is nil. However I am not able to see the exception message or the fact that an error happens at runtime. If I add code below this, it does not get run (no output gets shown).

How can I see the exceptions that are thrown at runtime in a Swift playground ?

like image 743
driis Avatar asked Jun 05 '14 20:06

driis


People also ask

How do you add a breakpoint in Xcode playground?

Navigate to a line in your code where you want execution to pause, then click the gutter or line number in the source editor to set a breakpoint. Xcode displays a breakpoint icon to indicate the location. Drag a breakpoint up or down to move it to another location; drag it away from the gutter to remove it.

How do I access Xcode playground?

To open Playground on Xcode, navigate to File in the menu and click on New > Playground... To test our square function, we will choose the Blank template. Name your Playground file, then click on Create.

How do you throw an error in Swift?

To indicate that a function, method, or initializer can throw an error, you write the throws keyword in the function's declaration after its parameters. A function marked with throws is called a throwing function. If the function specifies a return type, you write the throws keyword before the return arrow ( -> ).


2 Answers

Click the Assistant Editor icon to open the Console Output panel.

The error is listed there.

like image 186
Cezary Wojcik Avatar answered Nov 03 '22 20:11

Cezary Wojcik


As of Xcode 6.0 Beta 5, exceptions will now show up with an error marker in the source editor and in the result sidebar. If you press the quicklook button in the result sidebar we will show you the full backtrace of the exception.

like image 26
Rick Ballard Avatar answered Nov 03 '22 19:11

Rick Ballard