Nishad Arora
Nishad Arora

Reputation: 304

How or can I pass a structure in a parameter of a function?

I want to create a structure type parameter so that I can pass only two values as parameters but I don't want to use Bool.Kindly help me with the below code or please elaborate if I am doing anything wrong.Just started coding.

struct Alert {
    let alert = "Alert"
    let info  = "Information"
} 

func information(message : String) {
    AskConfirmation(type : Alert.info , title: "Information", message: message, completion: { (bool) in
        self.dismiss(animated: true, completion: nil)
    })
}

func alert(message : String) {
    AskConfirmation(type : Alert.alert , title: "Alert", message: message, completion: { (bool) in
        self.dismiss(animated: true, completion: nil)
    })
}

func askConfirmation (type : Alert.Type, title : String, message : String, completion:@escaping (_ result:Bool) -> Void) {
    if type == Alert.alert {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        self.present(alert, animated: true, completion: nil)

        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
            completion(true)
        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action    in
            completion(false)
        }))
    }else if type == Alert.info {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        self.present(alert, animated: true, completion: nil)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
            completion(true)
        }))
    }
}

This is how I will use my custom alert function.

alert(message: "Priority not selected")
askConfirmation(type : "Information" , title: "Information", message: "Quote saved successfully", completion: nil)

Upvotes: 0

Views: 50

Answers (2)

Milan Nosáľ
Milan Nosáľ

Reputation: 19757

Use enumeration instead:

enum Alert {
    case alert
    case info
}

func information(message : String) {
    AskConfirmation(type : Alert.info , title: "Information", message: message, completion: { (bool) in
        self.dismiss(animated: true, completion: nil)
    })
}

func alert(message : String) {
    AskConfirmation(type : Alert.alert , title: "Alert", message: message, completion: { (bool) in
        self.dismiss(animated: true, completion: nil)
    })
}

func askConfirmation (type : Alert, title : String, message : String, completion:@escaping (_ result:Bool) -> Void) {
    if type == Alert.alert {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        self.present(alert, animated: true, completion: nil)

        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
            completion(true)
        }))

        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action    in
            completion(false)
        }))
    }else if type == Alert.info {
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        self.present(alert, animated: true, completion: nil)
        alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
            completion(true)
        }))
    }
}

In your case you want to have two predefined custom values (so not a boolean value), enumerations enable you to do just that.

I recommend looking at these advanced techniques with enums too.

Upvotes: 1

Scriptable
Scriptable

Reputation: 19750

The reason your code is not working is because you are accessing values directly that have not been set.

struct Alert {
    static let alert = "Alert"
    static let info  = "Information"
} 

func information(message : String) {
    AskConfirmation(type : Alert.info , title: "Information", message: message, completion: { (bool) in
        self.dismiss(animated: true, completion: nil)
    })
}

Changing the values to static allows you to access them directly.

Upvotes: 0

Related Questions