I have problem when I want show alert error message inside in other alert action

I'm trying show error message and success after make delete request but the app crashed when call another method with error message.

@IBAction func deleteAccountAction(_ sender: Any) {
        let userID = prefs.value(forKey: "userId") as! String
        print("user id: \(userID)")
        
        let alert = UIAlertController(title: "Delete account", message: NSLocalizedString("Are you sure you want to delete your account?, This action cannot be reversed.", comment: "") , preferredStyle: .alert)
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
            // ...
        }

        let okayAction = UIAlertAction(title: "OK", style: .default) { (action) in
            RestAPIManager.sharedInstance.deleteAccount(userID: userID){
                (json, error) in
                if(json != JSON.null){
                    print(json)
                    if(json["success"] == true){
                        //i want success alert here
                    }else{

                       self.errorAlert()
                    }
                }else{
                    //this function causes the application to crash
                   self.errorAlert()
                }
            }
            
        }
        alert.addAction(okayAction)
       alert.addAction(cancelAction)
        self.present(alert, animated: true)
    }

// show error message function
func errorAlert(){
       var dialogMessage = UIAlertController(title: "Error", message: "Error", preferredStyle: .alert)
       self.present(dialogMessage, animated: true, completion: nil)
    }

Hello @NikiBer and welcome to the forums.
As this is more of a UIKit-only-related question I think it would be more fitting to ask this on the Apple Developer Forums, but I think I can nudge you into the right direction already.

Specifically I'd advise against nesting this kind of modular presentation from a user's point of view (I'm not even sure this can work with UIAlertController specifically as I have never even tried). So I think it would be better to properly dismiss the first alert you show and then present a new alert informing the user about the result (an error or success message). You might even opt to not animate the dismissal.

Here's a short example modifying your code accordingly:

// ...
RestAPIManager.sharedInstance.deleteAccount(userID: userID) { [weak self] json, error in
    guard json != JSON.null, json["success"] else {
        self?.showResultAlert(wasSuccessful: false)
    }
    self?.showResultAlert(wasSuccessful: true)
}

// ...

func showResultAlert(wasSuccessful: Bool) {
    // note that you might want to ensure you are still presenting the alert, I omit this for brevity
    dismiss(animated: true) { // if you don't want animations, pass false
        // present a new alert, using wasSuccessful to select which kind. 
        let newAlert = UIAlertController(title: "...", message: "...", preferredStyle: .alert)
        newAlert.addAction(UIAlertAction(title: "...", style: .default, handler: { _ in 
            // do other things
            dismiss(animated: true)
        }))
    }
}

I took the liberty to clean up your code a bit and make it a bit more swifty. Since I assumed your completion handler for deleteAccount is asynchronous I also used a weak reference to self as this looked like a potential retain cycle to me, but this depends on how the rest of your app works.

Also keep in mind, that these alerts are modal! I don't think it's a good idea to have users waiting, looking at the dialog, while the request goes over the network (and they could also repeatedly tap buttons, so in production code you have to ensure you properly take care of that, too).

Personally I'd go an entirely different route and dismiss the alert as soon as the request starts, showing some activity indicator in the underlying UI as it runs, but I don't know enough about the rest of your app to be more specific.

Hopefully this rump helps you, take care!