Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return boolean value from a function in Swift

I've looked around but was suprised that I couldn't really find anything that explained this. If I have:

func checkEmail ()  {    var test = true     return test }  ...elsewhere in the code....  var emailStatus = checkEmail () 

How can I make this function return the boolean value of true?

like image 206
Schuey999 Avatar asked Dec 19 '14 01:12

Schuey999


People also ask

How do you return a boolean in Swift?

The method is called isOriginal() , and it takes one parameter that's a string. But before the opening brace there's something important: -> Bool . This tells Swift that the method will return a boolean value, which is the name for a value that can be either true or false.

Can a function return a boolean result?

A Boolean function is like a built-in function except that it returns a value of true or false instead of number, string, or date. The result of a Boolean function cannot be printed; it can only be used as a condition. A Boolean function is composed of a function name followed by an operand in parentheses.

How does a function return a value in Swift?

If you want to return your own value from a function, you need to do two things: Write an arrow then a data type before your function's opening brace, which tells Swift what kind of data will get sent back. Use the return keyword to send back your data.


1 Answers

func checkEmail() -> Bool {    var test = true    return test } 

Elsewhere in the code....

var emailStatus = checkEmail() 
like image 73
Jie Li Avatar answered Sep 19 '22 04:09

Jie Li