Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Joseph Perl
Joseph Perl
877 Points

Trouble creating a function in swift.

Hello. I am trying to create a function in swift which gives the user an alert that they haven't entered a username or password when signing up. I think there may be a problem with the way I've declared the functions parameters, but am not sure. I am using version 10.3 of Xcode. I got the code I used from a video lesson, and the code worked in the video but not on my Xcode. The is:

func displayAlert(title, message) {

let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)

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

    self.present(alert, animated: true, completion: nil)

}

@IBOutlet weak var email: UITextField!

@IBOutlet weak var password: UITextField!

@IBAction func signupOrLogin1(_ sender: Any) {

    if email.text == "" || password.text == "" {
        displayAlert("Error in form", "Please enter an email and password")

    } 

Most of the error messages are right to the right of the top line. They say: "Unnamed parameters must be written with the empty name '_'" They also say "use of undeclared type "message" and use of undeclared type "title". Another error to the right of the second line says: "Use of unresolved identifier 'message'".

Is this is syntax error in the top line? May of should used the word "string" somewhere when declaring the parameters. I also remember seeing the -> symbol sometimes next to functions, and am not sure if my version of the above code is only compatible with another version of swift or Xcode.

Thank you.

1 Answer

Kevin Tanner
Kevin Tanner
3,385 Points

Yes this is a syntax error. It looks to me like you aren't declaring the type of the parameters needed for the function. Try the code below with the types declared.

func displayAlert(title: String, message: String) {

}