Wednesday 14 June 2017

Guard Statement in Swift Programming

Correction: Instead of where replace with , (comma) 

The guard statement is a Swift language feature introduced as part of Swift 2. A guard statement contains a Boolean expression which must evaluate to true in order for the code located after the guard statement to be executed. The guard statement must include an else clause to be executed in the event that the expression evaluates to false. The code in the else clause must contain a statement to exit the current code flow (i.e. a return, break, continue or throw statement). Alternatively the else block may call any other function or method that does not itself return.

Syntax:
func function_name (paramsname: paramType…) -> <ReturnType>
{
guard <boolean expressions> , <condition check> else {
    // code to be executed if expression is false
    <exit statement here>

// code here is executed if expression is true
}

Limitations:
*This statement used inside the functions
*This occupies the complete method and to do the check and process.

Code Snippet:
var value:String! = "Tessa"
print(value)
testFun(v:value)

func testFun(v: String?)  {
    guard let vu = v, vu == nil else {
    print ("Value is nil")
    return
    }
    print("Value is not nil")  // Output
}

Without Guard Statement: #Rust Coding:
func nonguardSubmit() {
    if let name = nameField.text {
        if let address = addressField.text {
            if let phone = phoneField.text {
                sendToServer(name, address: address, phone: phone)
            } else {
                show("no phone to submit")
            }
        } else {
            show("no address to submit")
        }
    } else {
        show("no name to submit")
    }
}
With Guard Statement:
func submit() {
    guard let name = nameField.text else {
        show("No name to submit")
        return
    }

    guard let address = addressField.text else {
        show("No address to submit")
        return
    }

    guard let phone = phoneField.text else {
        show("No phone to submit")
        return
    }

    sendToServer(name, address: address, phone: phone)
}

func sendToServer(name: String, address: String, phone: String) {
  ...
}
Guard Statement Improvised Code 1:
guard let name = nameField.text where name.characters.count > 3 && name.characters.count <= 16, let range = name.rangeOfCharacterFromSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) where range.startIndex == range.endIndex else {
    show("name failed validation")
    return
}

sendToServer(name)
Guard Statement Improvised Code 2:
func tappedSubmitButton() {
    guard let name = nameField.text where isValid(name) else {
        show("name failed validation")
        return
    }

    sendToServer(name)
}

func isValid(name: String) -> Bool {
    // check the name is between 4 and 16 characters
    if !(4...16 ~= name.characters.count) {
        return false
    }

    // check that name doesn't contain whitespace or newline characters
    let range = name.rangeOfCharacterFromSet(.whitespaceAndNewlineCharacterSet())
    if let range = range where range.startIndex != range.endIndex {
        return false
    }

    return true
}
Courtesy: Link1, Link2

No comments: