How to make a subscope in Swift? [duplicate] - ios

This question already has answers here:
How to create local scopes in Swift?
(5 answers)
Closed 2 years ago.
In C I can insert pairs of brackets to indicate a subscope.
int main() {
int x = 5;
{
int y = 6;
x += y; //Works
}
x = y+8; //Uh oh
}
In Swift, if I try something similar it will assume I'm trying to call the previous statement as a function, or errors for other reasons. How do I simply make a subscope in Swift, to deallocate variables earlier?

You can try
do {
// insert sub scope code
}

Related

Dart Operators : The name 'b' isn't a type and can't be used in an 'is' expression [duplicate]

This question already has answers here:
What is the difference between 'is' and '==' in Dart?
(2 answers)
Closed 11 months ago.
I'm new to dart and reading about dart operators. In the book fluter in Action by Eric Windmill (Chapter 2, 2.24, page 34) the auther says:
is and is! verify that two objects are of the same type. They are equivalent to == and !=.
Trying to implement this as shown in the code below
void main() {
int a = 7;
int b = 2;
bool z = a == b; // It works when I use the equals symbol
print('Result: $z');
}
But when I use the ```is`` keyword, I get an error
void main() {
int a = 7;
int b = 2;
bool z = a is b; // It doesn't work here
print('Result: $z');
}
Error
The name 'b' isn't a type and can't be used in an 'is' expression.
Try correcting the name to match an existing type.
Not sure what the context of that statement is but is and is! is not the same as == and != in the sense that they does the same. I guess what the want to explain, is that the opposite of is is is! like the opposite of == is !=.
== is used to check if two objects are equal (based on what this object at left side defines to be equal). So for int we return true when using == if both numbers have the same numeric value.
The is operator is used to test if a given object does implement a given interface. An example:
void main() {
Object myList = <int>[];
if (myList is List<int>) {
print('We have a list of numbers.');
}
}
The error you are getting are telling you that for using is you need to provide an actual type at the right side of the is and not an object instance.

What is use of underScore in Double value in swift? [duplicate]

This question already has answers here:
What is type of 123_456_789?
(2 answers)
Closed 5 years ago.
I just recently look into Apple documentation I found code like below
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
What is the use of "_" in double value? it's working without error why?
Just a way to improve readability. According to Swift documentation:
Numeric literals can contain extra formatting to make them easier to
read. Both integers and floats can be padded with extra zeros and can
contain underscores to help with readability. Neither type of
formatting affects the underlying value of the literal.

Convert Equation String to Int [duplicate]

This question already has answers here:
Swift - Resolving a math operation in a string
(4 answers)
Closed 7 years ago.
I'm trying to convert a string like so:
var equation = "8+3*4/5-1*(5+5)"
to an Int so that it will calculate it with order of operations but with my code it is just return nil. Here is what I have tried
var answer = Int(equation)
Try it this way:
var equation = "\(8+3*4/5-1*(5+5))" //"0"
And this way you can convert it to Int
var answer = equation.toInt() // 0
Or you can directly do it this way:
var equation = 8+3*4/5-1*(5+5) // 0
And As Martin R suggested if you want to do math operation in a string you can do it this way:
let expn = NSExpression(format:"8+3*4/5-1*(5+5)")
println(expn.expressionValueWithObject(nil, context: nil)) //"0"
Which will result as String but you can convert it to Int as I suggested above.
Here is original link for math operation in a string: Swift - Resolving a math operation in a string

How to find index of Int element in an array? [duplicate]

This question already has answers here:
How to find index of list item in Swift?
(23 answers)
Closed 7 years ago.
Is there a "better"(less verbose) way of doing this?
var found: Int?
for i in 0...myArray-1 {
if myArray[i] == 3 {
found = i
}
}
similar to
let i = find(myArray, "1")
but for Int ?
This question is slightly "different" than How to find index of list item in Apple's swift? because it directly address the find function for Int, instead of String. Granted, the only difference is the title and pointing out that you can remove the quotation marks to get Int.
Here's an Int example
let myArray = [1, 2, 3]
var i = find(myArray, 2)
Good luck

How can I convert a String to an Int in swift? [duplicate]

This question already has answers here:
Converting String to Int with Swift
(31 answers)
Closed 8 years ago.
I'm working in a small project and I faced this problem : text field is String by default so I need to take the input and convert it to integer then calculate I was searching for a way to solve the converting problem and I didn't find any working solution so can any one help me please ?
try like this
In Swift 2.0, toInt(),replaced with initializers. (In this case, Int(someString).)
let myString : String = "50"
let x : Int? = Int(myString)
if (x != null) {
// converted String to Int
}
myString.toInt() - convert the string value into int .

Resources