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 ?
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.
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.
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 ( -> ).
Click the Assistant Editor icon to open the Console Output panel.
The error is listed there.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With