"If" statement with an optional value not working - swift

I have an if statement with a variable, however the if statement does not work correctly and I am guessing it is because of an optional value on the variable.
The statement goes something like this
If (x == 6) {
}
x does = 6 but I cannot get the if statement to work.
When I do a "print x", the result is
Optional("6")
So I know the number is 6 but it seems that the optional value is making this if statement not work. I cannot get this unwrapped so I'm looking for another option.

See the double quotes? It means that x is String type not Int. You can do this way to make be more standard
if let x = x where x == "6" {
}

If you are getting:
Optional("6")
it means that the value is actually a string and not an int. If it was an int you would get:
Optional(6)
To double check you can try:
if x == "6"
{
}
I hope that helps.

How have you defined x
You’ll not get an optional if it is like
let x = 6 \\or var x = 6
if x == 6 {
print(x)
}
Will return you 6 not Optional("6")

Related

Instantiating a class with "!" in Swift? [duplicate]

I understand that in Swift all variables must be set with a value, and that by using optionals we can set a variable to be set to nil initially.
What I don't understand is, what setting a variable with a ! is doing, because I was under the impression that this "unwraps" a value from an optional. I thought by doing so, you are guaranteeing that there is a value to unwrap in that variable, which is why on IBActions and such you see it used.
So simply put, what is the variable being initialized to when you do something like this:
var aShape : CAShapeLayer!
And why/when would I do this?
In a type declaration the ! is similar to the ?. Both are an optional, but the ! is an "implicitly unwrapped" optional, meaning that you do not have to unwrap it to access the value (but it can still be nil).
This is basically the behavior we already had in objective-c. A value can be nil, and you have to check for it, but you can also just access the value directly as if it wasn't an optional (with the important difference that if you don't check for nil you'll get a runtime error)
// Cannot be nil
var x: Int = 1
// The type here is not "Int", it's "Optional Int"
var y: Int? = 2
// The type here is "Implicitly Unwrapped Optional Int"
var z: Int! = 3
Usage:
// you can add x and z
x + z == 4
// ...but not x and y, because y needs to be unwrapped
x + y // error
// to add x and y you need to do:
x + y!
// but you *should* do this:
if let y_val = y {
x + y_val
}

operator or method in swift which work as walrus operator of python

walrus operator of python language ( := )
work:- assign the value & also return that value.
language like swift at value assign it return nothing.
how to implement walrus operator kind a thing in swift language ?
I think it done by make function, pass address of variable & value.
assign value in that address & return value.
Is this work or any other way for this?
Joakim is correct.
Swift doesn't have a unary operator like C.
In C, you could do:
b = 10;
while (b>0) {
print(b--);
}
In Swift, there isn't a unary ++ or -- operator, so you would do:
var b = 10
while (b > 0) {
print b
b -= 1
}
but, really, in Swift, you'd do this instead
for b in (0...10).reversed() {
print b
}
See Reverse Range in Swift

Exclamation mark prefix on variables during if statements and other as well?

I'm very confused even after looking through similar questions of what the(!) operator does when it is prefixed on a variable or other object in if statements, functions, etc?
Example:
mutating func add(value: T)
{
if !contains(items, value)
{
items.append(value)
}
}
the exclamation mark ! is used for two purposes. When you see it appear at the beginning of an object, such as in !contains(items, values), it means "NOT". For example...
let x = 10
let y = 5
if x == y {
print("x is equal to y")
} else if x != y {
print("x is NOT equal to y")
}
The above code will print => "x is NOT equal to y" .
The logical NOT (!) operator can be used to reverse boolean values. For example...
var falseBoolValue = false
falseBoolValue = !falseBoolValue
print(falseBoolValue)
The above code will print => "true"
In addition to the usage as the logical NOT operator, the exclamation mark is also used to implicitly unwrap optional values. Whenever you see the exclamation mark appear at the end of an object name, such as in someVariable!, it is being used to implicitly unwrap an optional value. Read about optionals to gain a better understanding of how ! is used with optional values.
it is NOT prefix there. Meaning your if looks for "items NOT containing value".

Assigning last array element to a variable in Swift

I have this very simple line of code
var dblArray : [Double] = [0.01]
var x = dblArray.last
println(x * x)
The '.last' module returns the last element of the array, which is 0.01. However, based on the playground assistant view, it shows that the actual assignment to var x is (Some 0.01). And doing a println will lead to "Optional 0.01"
What I'm hoping to accomplish is merely capturing the value of the last element and placing it in x.
What am I doing wrong here?
I'm pretty certain .last would have to be an optional, if only to handle the edge case of an empty array, where .last would make no sense as a "solid" value.
In any case, if you're sure the array won't be empty, just unwrap the value. If you're not sure then you'll need to check intelligently such as with:
var x = 0
if let junk = dblArray.last {
x = junk
}
I think that's the correct syntax, I don't have my Mac with me at the moment, but it should hopefully be close enough to show the concept.

Comparing two double variables Objective-C

double a = 10.123420834;
double b = 100.123412321;
if (a > b) {
// do something here
}
I am trying to compare the two values, the code above doesn't seems to work. Any idea?
The code is correct.
Note that your snippet is equivalent to
float a = 10.123420834;
float b = 100.123412321;
if (a > b) {
// do something here
}
since Objective C uses double by default unless the number is followed by an f.
Also note that a < b, so the if statement will always evaluate to FALSE. Hence you may want to do
double a = 10.123420834;
double b = 100.123412321;
if (a > b) {
// do something here
} else {
// do something else here
}
to test this properly.
double a = 10.123420834
double b = 100.123412321
You need to have a semicolon at the end of each of those lines.
The code in your example is correct. Your problem must be in the "do something here", or elsewhere.