Azur Haljeta
11/15/2020, 4:19 PMfunc isValidEmail(_ email: String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailPred.evaluate(with: email)
}
However, the NSPredicate
constructor has different signature when used in iosMain
. Is this even a constructor in Swift?- (BOOL)validateEmailWithString:(NSString*)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
fun emailValidation(email:String): Boolean {
val emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
val predicate = NSPredicate.predicateWithFormat("SELF MATCHES %@", emailRegex)
return predicate.evaluateWithObject(email)
}
It fails with this error:
Passing String as variadic Objective-C argument is ambiguous; cast it to NSString or pass with '.cstr' as C string
corneil
11/15/2020, 4:26 PMAzur Haljeta
11/15/2020, 4:29 PMNSString
, it works
Seems hacky to me but nevermindgildor
11/15/2020, 11:46 PM