Avatar ·

How to properly validate email in Swift?

Hello everyone! I'm a beginner iOS developer and I've encountered a problem with email validation in Swift. I'm writing a user registration app, and I need to check that the entered email has a correct format. I tried writing my own validation, but it doesn't work properly. For example, here's my code: ```swift func isValidEmail(_ email: String) -> Bool { return email.contains("@") && email.contains(".") } ``` But this validation allows strings like "test@", "@test.com" or "test@.com". Obviously, these are not valid email addresses. I also heard that you can use NSRegularExpression, but I don't understand how to properly compose a regular expression and apply it in Swift. Please advise how to make reliable email validation? Do I need to use regular expressions? Are there any