I'm Learning swift using playgrounds. Can I declare a bool and have it be only true or false so I can use it as a condition for a function?
Simply writing:
var boolean = true
will create a boolean named 'boolean' and will be equal to true.
You can change it's value to false by writing
boolean = false
later on. The boolean variable will be true or false, not at the same time. I don't wanna be rude but you should check simple variable types and learn from there. It's easier that way.
You can declare a Boolean variable using "Bool" type annotation . For Example
var boolean = false
or
var boolean = Bool()
if you want to use in an if else condition . you can do like that
if boolean == true {
// Statements
}
Here , boolean is variable name .You can take any variable name.
Related
I am getting
Variable 'rollerSmall' was never used; consider replacing with '_' or removing it
error. how do I fix it? It's a warning. I'm trying to do this:
if roller == "1"{
var rollerSmall: Bool = true
}
if roller == "2"{
var rollerSmall: Bool = false
var rollerMedium: Bool = true
}
}
Variables have scope; it matters where you declare a variable. The problem is that you are declaring your variable inside the if clauses. But you will probably want to declare it, and use it, outside the if clauses. For example:
var rollerSmall = false
var rollerMedium = false
if roller == "1" {
rollerSmall = true
}
if roller == "2" {
rollerMedium = true
}
// use rollerSmall and rollerMedium here
Having said all that, this is probably still a terrible way of doing whatever you are trying to do. You should not be encoding things in this flimsy way. If your roller can only have states small and medium, for example, that is an enum and you shouldn't have integer values and different variables at all.
I'm using remix.ethereum.org
I wrote this very simple smart contract:
pragma solidity ^0.4.19;
contract TicTacToe {
bool myBool = false;
uint8 myUint8;
uint256 myUint256;
string myString = "myString";
bytes myBytes = "myString";
function setMyBoolean(bool myBoolArgument) public {
myBool = myBoolArgument;
}
function getMyBoolean() public view returns(bool) {
return myBool;
}
}
As you can see the default value for myBool is false
I can change this by calling the function setMyBoolean.
If I use this argument and type in true, myBool will be set to true
If I use this argument and type in false, myBool will be set to false
But if I type in any other combination of letters, myBool will be set to true as well. This surprises me because the default setting for myBool is false.
Why does this happen?
It's working as intended because this is the way that remix and abi encoder decided to handle booleans
// "false" will be converting to `false` and "true" will be working
// fine as abiCoder assume anything in quotes as `true`
if (type === 'bool' && args[i] === 'false') {
args[i] = false
}
https://github.com/ethereum/remix/blob/807ffd9772b07dafb343c08faf44c78ee456de77/remix-lib/src/execution/txHelper.js#L18
I assume that the value input is evaluated as as a string and, as the string exists and It Is not empty, It Is evaluated as true.
I'm new to iOS development and wondering if I could pass a string variable inside if statement? Here's my pseudo code:
x = 1
func myFunc() -> String {
myString = "x == 1"
return myString
}
if(myfunc()) {
code i want to execute
}
I am currently getting the following error: "'String' is not convertible to 'Bool'"
Is there a way I can do this?
You should use a comparison operator for this.
if myString == myFunc() {
// your code
}
If statement always wants a condition that can return a bool value. i.e. true and false.
In your above code, you are not providing sufficient data to if statement so that it can calculate whether the result iss true or false.
When you compare it like if myString == myFunc() , if statement will compare the string and return true if string matches else false.
if the string matches, it will execute the code that is within if conditions scope. Otherwise it will calculate the else condition.
UPDATE1:
I see you have updated the question, so you want to check if myFunc() is empty or not?
For that you can compare it with empty string.
if myFunc() == "" {
// your code
}
UPDATE2:
Question: (asked in comment) instead of writing "if(x == 1)" i am trying to use a variable so my if statement is "if(stringVaraible)" where stringVariable = "x ==1". Basically I am asking if it is possible to turn a string into normal code
Answer: No, you can't do that. Swift is a compiled language, not interpreted like Ajax. Read more here: https://stackoverflow.com/a/30058875/8374890
It's very specific and clear that you can't use String as boolean. The approach you can take is well known like..
if(myString == "x == 1") {
code i want to execute
}
or
if(desiredString == myFunc()) {
code i want to execute
}
I want to know about Bool in Swift.
If Bool is a basic primitive datatype, why is a Boolean's default value nil?
var test: Bool!
print(test) // nil
In Java the Boolean default value is false:
Default value of 'boolean' and 'Boolean' in Java
Bool, Bool! and Bool? all are different in Swift.
1. Bool is a non-optional data type that can have values - true/false. You need to initialize it in the initializer or while declaring it before using it.
var x : Bool = false
var x: Bool
init()
{
x = false
}
2. Bool? is an optional data type that can have values - nil/true/false. In order to use this type, you need to unwrap it using if let or force unwrapping.
var x: Bool?
if let value = x
{
//TODO: use value instead of x
}
3. Bool! is an implicitly unwrapped optional data type that can have values - nil/true/false. The difference here is it must contain a value before using it else it will result in runtime exception. Since it is implicitly unwrapped, no need to unwrap it using if let or force unwrapping.
var x: Bool! //Must contain value before using
Strictly spoken there is no default value in Swift.
Either the Bool is non-optional then you have to assign a (default) value
or if the Bool is an optional, then it is nil – which is no value in terms of Swift.
Bool in Swift is not a primitive. Everything in Swift are objects. Your variable test is a Bool!, which is an implicitly unwrapped optional and the default value is nil.
If you use this code (not an optional),
var test: Bool
print(test)
You will get an error:
variable 'test' used before being initialized
So in Swift you have to initialize stuff before you use it, for example:
var test: Bool = false
this piece of code prints two times 'false':
println(levelController?.died)
println(!levelController?.died)
I don't understand why, the levelController is instantiated and the died attribute is declared in LevelController like this:
var died = false
Can someone tell me what I might be doing wrong?
You can create your own not operator like this:
let not = (!)
And now we can assign a bool to test this:
let dead = false
if not(dead) {
print(dead)
// Code
} else {
// Code
}
This is Swift, not Objective C.
levelController?.died is not a boolean value. It is an optional boolean. It can be true, false, or nil. The first println prints false. The logical not operator, applied to an optional, returns false if the optional is not nil, and true if the optional is nil. Just as it does in C when applied to a pointer.
As a complete example, your code:
class Controller {
var died: Bool = false
}
var levelController: Controller? = Controller()
println(levelController?.died)
println(!levelController?.died)
Outputs the following:
Optional(false)
false
The first version of your code levelController?.died makes use of option chaining, unwrapping the levelController and accessing the died property. This explains why the output is Optional(false). You would typically use it as follows:
if let died = levelController?.died {
if (died) {
println("I died")
}
}
The if-let statements unwraps this optional value.
The second version of your code !levelController?.died tests whether the given optional value is nil or not. You will notice that changing died to true of false makes no difference.
However, changing the instantiation as follows:
var levelController: Controller? = nil
Results in !levelController?.died becoming true. This isn't really a terribly practical piece of code!