top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to handle runtime error in SWIFT language

0 votes
674 views

how to handle exceptions which occurs during run time in SWIFT

posted Jul 22, 2014 by Shiva

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

There are no Exceptions in Swift, similar to Objective-C's approach.

In development, you can use assert to catch any errors which might appear, and need to be fixed before going to production.

The classic NSError approach isn't altered, you send an NSErrorPointer, which gets populated.

Brief example:

var error: NSError?
var contents = NSFileManager.defaultManager().contentsOfDirectoryAtPath("/Users/leandros", error: &error)
if let error = error {
    println("An error occurred \(error)")
} else {
    println("Contents: \(contents)")
}
answer Aug 4, 2014 by Raju
Similar Questions
0 votes

Dictionary gives error when i tried to re-assign values.

  var dictionary = [:]
    dictionary = ["one":"Hello","two":"Good","three":"Morning"]
    println(dictionary)
    dictionary["two"] = "Night"
    println(dictionary)

And error is

Playground execution failed: error: <REPL>:35:17: error: cannot assign to the result of this expression
dictionary["two] = "Night"
~~~~~~~~~~~~~~~ ^
+1 vote

How to declare single parameter which accept multiple values.

+1 vote

How to use default values for parameter in function. What will happens when value is not passed for a default parameters.

0 votes

I am facing situation to use external parameter name to a function parameter name .. How to achieve this

0 votes

Is it possible to set multiple return type for function in SWIFT. If yes please provide me syntax .

...