Fixing a bug in a ternary-like if-else block [duplicate] - swift

This question already has answers here:
Change in Xcode 10 Playgrounds Variable Initialization change? Are Xcode 10 Playgrounds an interpreter?
(2 answers)
Closed 4 years ago.
Instead of using the ternary operator, I coded a if-else block which doesn't seem to compile... Didn't understand the bug exactly, why isn't it working? Error line is :
error: MyPlayground2.playground:6:1: error: variables currently must have an initial value when entered at the top level of the REPL
var parentAge: Int
And my code:
import UIKit
var parent: String = "mom"
var parentAge: Int
let optOne = 39
let optTwo = 43
if parent == "mom" {
parentAge = optOne
print(parentAge)
} else {
parentAge = optTwo
print(parentAge)
}

error: variables currently must have an initial value when entered at the top level of the REPL
You should either initialise it with optional or assign an initial value if not optional.
var parentAge: Int?
var parentAge: Int = 0
Both of above representation are correct.

Related

Swift adding object to array appends a copy instead of a reference? [duplicate]

This question already has answers here:
Is Swift Pass By Value or Pass By Reference
(10 answers)
Closed 1 year ago.
Sample code:
struct test {
var itest: Int?;
}
var tests : [test] = [];
var t = test();
tests.append(t)
t.itest = 99;
print(tests[0].itest)
print(t.itest)
produces
nil
Optional(99)
So it seems the append command creates a copy of t and not its reference.
If the array would contain a reference of t, both print outs must be itest=99.
A struct is not an "object" in the way you are probably thinking. It's a value. When you assign a value, it's copied. That's by design.

How to fix 'characters' is unavailable: Please use String directly in swift 5 [duplicate]

This question already has answers here:
How to filter characters from a string in Swift 4
(2 answers)
Closed 3 years ago.
i've some code to filtering number inside variable.
Here's the code:
var numbers = String(anotherNumbers.characters.filter { "01234567890.".characters.contains($0) })
In the swift 3, this code working correctly. But in the Swift 5, i get an error 'characters' is unavailable: Please use String directly
How to fix this error?
Thankyou.
You can remove characters to use String directly.
For example
var anotherNumbers = "0123456789"
var numbers = String(anotherNumbers.filter { "01234567890.".contains($0) })
returns "0123456789"

I am confuse structure vs class in swift language [duplicate]

This question already has answers here:
structure vs class in swift language
(13 answers)
Closed 3 years ago.
I am confused about structure vs class. I have seen this example According to this example structure-vs-class According to link may be code output is 15,15,15,20
BUT code output is
because Structure is not changed but when I have run code on Xcode it returns me 15,15,20,20
class objectmanagement
{
public var x : Int = 10;
func display()
{
print("\(x)")
}
}
struct StuctManagement{
var obj = objectmanagement()
}
let SA = StuctManagement()
SA.obj.x = 15
var SB = StuctManagement()
SB.obj = SA.obj
SA.obj.display()
SB.obj.display()
SB.obj.x = 20
SA.obj.display()
SB.obj.display()
I am confused please help me to understand this output this is same as a class output
In swift class is reference type. (see here)
When you say:
SB.obj = SA.obj
It means that the object of SA is the exact object in SB. (there is one pointer for SA.obj and SB.obj)
Although
let SB = SA
makes copy of SA and create SB with different reference.

Swift: let is uninitialized but var gets initialized. Why? [duplicate]

This question already has answers here:
Why optional constant does not automatically have a default value of nil [duplicate]
(2 answers)
Why doesn't Swift allow setting value of an optional constant after object initialization?
(2 answers)
Closed 5 years ago.
I'm using playgrounds to learn Swift and I noticed something strange when trying to set optionals on var and let. Below is the code with output:
var num1: Int?
print(num1) //Output: "nil\n"
let num2: Int?
print(num2) //Output: Error: Constant num2 used before initialized
I do not understand why 'var' gets initialized with nil and 'let' is uninitialized when made optional.
var is variable, you can declare it without initialisation. But let is constant, you should initialise it’s value, i.e.
var num1: Int?
print(num1) //Output: "nil\n"
let num2: Int = 20
print(num2)

What is the different between `if var` and `if let` in swift? [duplicate]

This question already has answers here:
What is the difference between `let` and `var` in Swift?
(32 answers)
Closed 7 years ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Consider this two codes:
if let myValue = myObject.value as NSString?{
//logic here
}
vs
if var myValue = myObject.value as NSString?{
//logic here
}
I know the let keyword is define a constant, is this mean that the first line of code, if the myObject.value is NSString , the myValue constant will be made? This looks confusing.
If you use the let then you will not be able to change myValue.
if let myValue = myObject.value as NSString? {
myValue = "Something else" // <-- Compiler error
}
On the other hand with var you can.
if var myValue = myObject.value as NSString? {
myValue = "Something else" // <-- It's fine
}
Please note that myValue does exists only within the scope of the if and changing its value does not produce effect outside of its scope.
Hope this helps.
You use let to create a constant, so in the first example you cannot change myValue.
When you use var, myValue is a variable which you can change inside the if statement.
The variable type is a different depending if you are using let or var.
let is final and can not be changed afterwards.
var is a normal variable.
Your myValue cannot be changed or modified if you using let in this statement.
“Use let to make a constant and var to make a variable.
The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.
This means you can use constants to name a value that you determine once but use in many places.
var myVariable = 42
myVariable = 50
let myConstant = 42
A constant or variable must have the same type as the value you want to assign to it.
However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type.
In the example above, the compiler infers that myVariable is an integer because its initial value is an integer.
Read the : iBook The Swift Programming Language
Thanks .
The example you stated is of Optional Binding
Optional binding is used to determine whether an optional has a value, and if so, to make that value available as a temporary constant or variable.
if let myValue = myObject.value as NSString?{
//logic here
}
Above code will extract myObject.value if exist as a constant, whereas the code below will extract myObject.value as a variable
if var myValue = myObject.value as NSString?{
//logic here
}