Saurav Nagpal
Saurav Nagpal

Reputation: 1267

how to avoid strong reference in callback of UIAlertViewController

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.sstatic.net/JfgNi.png

So how can i avoid strong reference here?

Upvotes: 2

Views: 437

Answers (1)

Try rewritting the closure like this..

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

}

Upvotes: 1

Related Questions