Wednesday, 21 February 2018

Constant Value, Validate Email, Random color and Random Name Generate - Swift

Constant Values - Swift
//MARK: Constant Values
struct Window {
    static let screenWidth = UIScreen.main.bounds.size.width
    static let screenHeight = UIScreen.main.bounds.size.height
}

Validate the Email - Swift
func validateEmail(str_Email:String) -> Bool {
   
    let emailFormat = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailFormat)
    return emailPredicate.evaluate(with: str_Email)
}

Random color generation - Swift
func randomColor() -> UIColor{
   let red = CGFloat(drand48())
   let green = CGFloat(drand48())
   let blue = CGFloat(drand48())
   return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}

Random Name generation - Swift
func generate_name(extension_str: String) -> String {
    let aRandomInt = randomNumber()
    let dateformate = DateFormatter()
    dateformate.dateFormat = "ddMMyyyyHHmmss"
    let date: String = dateformate.string(from: Date())
    let random_num = "name-\(date)\(aRandomInt)"
    return random_num
}

No comments: