1

I use Martin R's answer to print the NSSetUncaughtExceptionHandler in Swift.

How should I use NSSetUncaughtExceptionHandler in Swift

  func exceptionHandler(exception : NSException) {

    let alert = UIAlertController(title: "Exception", message: "\(exception)", preferredStyle: .Alert)

    self.presentViewController(alert, animated: true, completion: nil)

    print(exception)
    print(exception.callStackSymbols)
}

But how can I display the message in a UIView. Like this one

enter image description here

Because I got a compiler error message said that "A C function pointer can only be formed from a reference to a 'func' or a literal closure."

Handling unhandled exceptions and signals written by Matt Gallagher at Cocoa with Love.

enter image description here

Community
  • 1
  • 1
charles.cc.hsu
  • 689
  • 11
  • 15
  • have you tried exception.name or exception.reason? – Horst Oct 05 '15 at 15:51
  • @Horst, I would like to show the message on screen, user can capture and send it back. – charles.cc.hsu Oct 05 '15 at 16:04
  • You're not showing your entire code – newacct Oct 05 '15 at 22:23
  • @newacct I am new to Swift, about 1 year only. I am trying to convert the code written by Matt From ObjC to Swift. It can be compiled, but can not be run. an error message 'Thread 1: singal SIGABRT' in main.m at the line 'int retVal = UIApplicationMain(argc, argv, nil, nil);' – charles.cc.hsu Oct 05 '15 at 23:35
  • @charles.cc.hsu: I'm confused. Didn't you say you got a compile error? – newacct Oct 06 '15 at 02:35
  • @newacct Yes. compiler error. Now I can bridge the C function to Swift, but how can I do it only in swift? – charles.cc.hsu Oct 06 '15 at 03:54
  • I am trying this same. Same error I am receiving. How did u solve this ?? @charles.cc.hsu Now I am striking in the same error. I need to mail that error. But I cant. – McDonal_11 Sep 01 '16 at 11:21

2 Answers2

0

NSSetUncaughtExceptionHandler takes a C function pointer, and C function bodies are fixed at compile-time. C function pointers get bridged into Swift 2 as @convention(c) function types, and, like in C, you can only pass a function whose body can be fixed at compile-time; i.e. top-level Swift functions, or anonymous function which don't capture any variables.

Your anonymous function captures self from the enclosing scope, so it cannot be used as a C function. You should try to access the controller or do whatever you need to do in some other way using only global variables or classes.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Thanks you! I will follow your suggestion to figure out the solution. Before that, I maybe just keep the original C function and use bridge to work with Swift. – charles.cc.hsu Oct 07 '15 at 02:18
  • @charles.cc.hsu: How would you write it as a C function? If you can answer that they you can write it in Swift without capturing any variables. – newacct Oct 07 '15 at 04:18
  • I am still struggling against converting the [C code](https://github.com/Charles-Hsu/UncaughtExceptions/blob/master/Classes/UncaughtExceptionHandler.m) to Swift, because I am not familiar with ObjC also. The UncaughtExceptionHandler.m is too low level function for me. I don't know what is the meaning of 'volatile' and how to convert it to Swift (somewhere in SO said use memset_s). Second, the code use UIAlertView is deprecated, I also do not know how to convert it to UIAlertController, because UncaughtExceptionHandler is subclass of NSObject only. – charles.cc.hsu Oct 08 '15 at 00:34
  • Here is a solution [UIAlertController from any NSObject subclass] (http://stackoverflow.com/questions/31472321/how-do-i-display-uialertcontroller-from-any-nsobject-subclass), I will give it a try. – charles.cc.hsu Oct 08 '15 at 00:41
  • how will the memory be freed? – Droppy Oct 10 '15 at 18:38
  • @newacct I am trying this same. Same error I am receiving. How did u solve this ?? Now I am striking in the same error. I need to mail that error. But I cant. – McDonal_11 Sep 01 '16 at 11:22
  • @McDonal_11: Well, how would you do it in C? Basically, the function used cannot capture any variables from an outside scope. It can only use global variables. – newacct Sep 01 '16 at 22:22
  • @newacct http://stackoverflow.com/questions/39265162/xcode7-how-to-mail-crash-report-in-swift This my question. Pls guide me. – McDonal_11 Sep 02 '16 at 04:29
-1

with the use of NSSetUncaughtExceptionHandler

NSSetUncaughtExceptionHandler { exception in
            print("EXception Details Are \n\nExceptionName--> \(exception.name) \nReason -->\(exception.reason!)\n\(exception.description)")
            print(exception.callStackSymbols)
        }
davejal
  • 6,009
  • 10
  • 39
  • 82