Here you have several options.
You can use find to get UIAlertAction index
find allows you to find the index of an object in an array. You can use it to find the action index (which is passed as a parameter to the UIAlertAction handler, which is UIAlertAction itself) in the alert.actions array of all actions.
let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet) let closure = { (action: UIAlertAction!) -> Void in let index = find(alert.actions as! [UIAlertAction], action) println("Index: \(index)") } alert.addAction(UIAlertAction(title: "Doc1", style: .Default, handler: closure)) alert.addAction(UIAlertAction(title: "Doc2", style: .Default, handler: closure)) alert.addAction(UIAlertAction(title: "Doc3", style: .Default, handler: closure)) alert.addAction(UIAlertAction(title: "Doc4", style: .Default, handler: closure)) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel) { _ in println("User cancelled.") }) self.presentViewController(alert, animated: true) {}
You can create a closure ... which returns a closure
Create a closure that takes the parameter of your choice (here Int ) and returns a closure that captures this parameter so you can use it.
let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet) let closure = { (index: Int) in { (action: UIAlertAction!) -> Void in println("Index: \(index)") } } alert.addAction(UIAlertAction(title: "Doc1", style: .Default, handler: closure(0))) alert.addAction(UIAlertAction(title: "Doc2", style: .Default, handler: closure(1))) alert.addAction(UIAlertAction(title: "Doc3", style: .Default, handler: closure(2))) alert.addAction(UIAlertAction(title: "Doc4", style: .Default, handler: closure(3))) alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel) { _ in println("User cancelled.") }) self.presentViewController(alert, animated: true) {}
So you have a function (closure) that generates closures for your UIAlertAction handlers, all with the same body, except that they capture another object (here Int ).
What is really great about this solution is that you can capture anything. You can even capture the hypothetical Doctor object that represents your doctor, or the doctor’s identifier directly, etc.!
Use loop
But usually you add your actions using a for loop, so why not take advantage of this, plus take advantage of the closure and the fact that they capture variables to make a nice function that will directly tell the doctor ID you select?
func testMyAlert() { let doctors = [ ["Name": "Doctor for Disease AAA", "Doctor_id": "21"], ["Name": "Doctor for Disease BBB", "Doctor_id": "22"], ["Name": "Doctor for Disease AAA", "Doctor_id": "25"] ] chooseDoctor(doctors) { selectedDocID in if let docID = selectedDocID { println("User selected doctor with ID \(docID)") } else { println("User cancelled, no doctor selected") } } } func chooseDoctor(doctors: Array<[String:String]>, completion: Int?->Void) { let alert = UIAlertController(title: "Doctors", message: "Choose a doctor", preferredStyle: .ActionSheet) for doc in doctors { let action = UIAlertAction(title: doc["Name"]!, style: UIAlertActionStyle.Default) { _ in