Return Option with Async Methods

I am using Regex to test a username in Swift. The method in Swift has a return type of Bool to let the calling method know that it should proceed. Originally, I was using synchronous methods to perform find. I’ve recently been converting them to asynchronous methods and have run into a problem. In the sync methods I was able to return “true” or “false”. In the async methods I am presented with the following error on each Bool return inside the async find method.

Unexpected non-void return value in void function

Here is code for each method.

Sync Version

func signUpButtonUsernameRegexTest(usernameEntry: String) -> Bool
    {
        let usernameText = usernameEntry
        
        let regexCorrectPattern = "^[a-zA-Z0-9_]{1,18}$"
        let regexWhitespacePattern = "\\s"
        let regexSpecialCharacterPattern = ".*[^a-zA-Z0-9_].*"
        
        if regexTestString(usernameText, withPattern: regexCorrectPattern)
        {
            // The regex has passed.
            let usernameEntry = usernameText
            print("The regex is true and a match")
            
            // Search for the username to see if it already exists.  Create whereClause and dataQuery.
            let whereClause = "username = '" + usernameEntry + "'"
            let dataQuery = BackendlessDataQuery()
            dataQuery.whereClause = whereClause


            var fault: Fault?
            let data = backendless.data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &fault)
            if fault == nil
            {
                if data.totalObjects.intValue == 0
                {
                    // The username is not taken.
                    print("The signUpButtonUsernameRegexTest is returning true: username does not exist")
                    return true
                }
                else if data.totalObjects.intValue >= 1
                {
                    // The username is taken.  Alert the user.
                    print("The signUpButtonUsernameRegexTest is returning false: username already exists")
                    return false
                    
                    // Add code to alert the user.
                }
                else
                {
                    print("The signUpButtonUsernameRegexTest is returning false: unknown error 1")
                    return false
                    
                    // Add code to alert the user.
                }
            }
            else
            {
                print("Server reported an error: \(fault)")
                
                // I have to add code to test for invalid user credentials and report that to the user.
                let faultMessage = fault?.message
                let faultCode = fault?.faultCode
                let errorCodeMessage = "There is an error in SignUpViewController's signUpButtonUsernameRegexTest(), find().  Fault Message: \(faultMessage), Fault Code: \(faultCode)"
                
                // Call the sendErrorCodeEmail() to report the error to Tipper.
                self.sendErrorCodeEmail(errorCodeMessage)


                return false
            }
        }
        else if regexTestString(usernameText, withPattern: regexWhitespacePattern)
        {
            // The username contains whitespace characters.  Alert the user.
            print("The signUpButtonUsernameRegexTest is returning false: whitespace exists")
            return false
            
            // Add code to alert the user.
        }
        else if regexTestString(usernameText, withPattern: regexSpecialCharacterPattern)
        {
            // The username contains special characters.  Alert the user.
            print("The signUpButtonUsernameRegexTest is returning false: special characters exists")
            return false
            
            // Add code to alert the user.
        }
        else if usernameText == ""
        {
            // The usernameField is empty.  Make sure the sign up button is disabled.
            print("The signUpButtonUsernameRegexTest is returning false: username field is empty")
            return false
            
            // Add code to alert the user.
        }
        else
        {
            // For some reason the Regex is false.  Disable the sign up button.
            print("The signUpButtonUsernameRegexTest is returning false: unknown error 2")
            return false
            
            // Add code to alert the user.
        }
    }

Async Version

func signUpButtonUsernameRegexTest(usernameEntry: String) -> Bool
    {
        let usernameText = usernameEntry
        
        let regexCorrectPattern = "^[a-zA-Z0-9_]{1,18}$"
        let regexWhitespacePattern = "\\s"
        let regexSpecialCharacterPattern = ".*[^a-zA-Z0-9_].*"
        
        if regexTestString(usernameText, withPattern: regexCorrectPattern)
        {
            // The regex has passed.
            let usernameEntry = usernameText
            print("The regex is true and a match")
            
            // Search for the username to see if it already exists.  Create whereClause and dataQuery.
            let whereClause = "username = '" + usernameEntry + "'"
            let dataQuery = BackendlessDataQuery()
            dataQuery.whereClause = whereClause


            self.backendless.data.of(NewEmailUser.ofClass()).find(dataQuery, response: {(result : BackendlessCollection!) -> () in
                
                if result.totalObjects.intValue == 0
                {
                    // The username is not taken.
                    print("The signUpButtonUsernameRegexTest is returning true: username does not exist")
                    return true
                }
                else if result.totalObjects.intValue >= 1
                {
                    // The username is taken.  Alert the user.
                    print("The signUpButtonUsernameRegexTest is returning false: username already exists")
                    return false
                    
                    // Add code to alert the user.
                }
                else
                {
                    print("The signUpButtonUsernameRegexTest is returning false: unknown error 1")
                    return false
                    
                    // Add code to alert the user.
                }
                }, error: { (fault : Fault!) -> ()in
                    
                    print("Server reported an error: \(fault)")
                    
                    // I have to add code to test for invalid user credentials and report that to the user.
                    let faultMessage = fault?.message
                    let faultCode = fault?.faultCode
                    let errorCodeMessage = "There is an error in SignUpViewController's signUpButtonUsernameRegexTest(), find().  Fault Message: \(faultMessage), Fault Code: \(faultCode)"
                    
                    // Call the sendErrorCodeEmail() to report the error to Tipper.
                    self.sendErrorCodeEmail(errorCodeMessage)
                    
                    return false
            })
        }
        else if regexTestString(usernameText, withPattern: regexWhitespacePattern)
        {
            // The username contains whitespace characters.  Alert the user.
            print("The signUpButtonUsernameRegexTest is returning false: whitespace exists")
            return false
            
            // Add code to alert the user.
        }
        else if regexTestString(usernameText, withPattern: regexSpecialCharacterPattern)
        {
            // The username contains special characters.  Alert the user.
            print("The signUpButtonUsernameRegexTest is returning false: special characters exists")
            return false
            
            // Add code to alert the user.
        }
        else if usernameText == ""
        {
            // The usernameField is empty.  Make sure the sign up button is disabled.
            print("The signUpButtonUsernameRegexTest is returning false: username field is empty")
            return false
            
            // Add code to alert the user.
        }
        else
        {
            // For some reason the Regex is false.  Disable the sign up button.
            print("The signUpButtonUsernameRegexTest is returning false: unknown error 2")
            return false
            
            // Add code to alert the user.
        }
    }

Is there a way to fix this so I can use the async version?

Hi Jon,

You can’t return a value like that from the async method - they just don’t work this way.
I’m not aware of the exact syntax in Swift, so I would suggest you to google on “how to return a value from async method”. For example, here is one of the numerous StackOverflow questions with your exact error: http://stackoverflow.com/questions/32121108/unexpected-non-void-return-value-in-void-function-swift-2-0

Thank you. I will look into this. Worse case, I will use the sync version.