2

I am creating a alert view controller with action by following code

var alertView = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: NVConstant.alertInfoBtnTitle, style: UIAlertActionStyle.Default, handler: alertHandler))

And My alert Handler code is following:

@lazy var alertHandler:(UIAlertAction!)->Void = { a![enter image description here][1]ction in
    var clickedButtonTitle:String = action.title
    switch clickedButtonTitle{
    case NVConstant.notificationAlertConfirmatinTitle :
        Utility.cancelAlarmForTheActivity(activity: self.selectedActivity.0)
    case NVConstant.notificationAlertCancelTitle :
        self.selectedSwitch.setOn(true, animated: true)
    default :
        return
    }

}

So my question is how to avoid strong reference of self in callback closure. I tried by using [unowned self] but after that application crash i think due to parameter mismatch.

@lazy var alertHandler:(UIAlertAction!)->Void = { [unowned self] action in  //Crash 
// code
}

Following is Crash : https://i.stack.imgur.com/JfgNi.png

So how can i avoid strong reference here?

Saurav Nagpal
  • 1,247
  • 1
  • 10
  • 27

1 Answers1

1

Try rewritting the closure like this..

@lazy var alertHandler:(UIAlertAction!)->Void = { [unowned self] (UIAlertAction: action) -> () in

}
  • Hi Ernesto thanks for the reply but tried this as well and application crash in same way – Saurav Nagpal Aug 22 '14 at 05:00
  • This is the correct syntax, however you should know that this can be avoided if the owner of the controller is the function (not the controller). See http://stackoverflow.com/a/34931631/654870 – Kyle Clegg Jun 27 '16 at 09:16