Constant used before being initialzed [duplicate] - swift

This question already has an answer here:
Constant unassigned optional will not be nil by default
(1 answer)
Closed 4 years ago.
let errorCodestring : String?
if let theError = errorCodestring {
print(theError)
}
In the above code Xcode throws the error:
constant 'errorCodestring'being used before being initialized
What is wrong in the code and how to clear the error?

You have to initialize the constant before using it.
let errorCodestring : String? = nil

Related

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"

Unwrapping optionals with just `let` [duplicate]

This question already has answers here:
Please Help Me Intepret This Code SWIFT
(4 answers)
What is the `?` is or the `:` at the equation when coding in Swift
(3 answers)
Closed 4 years ago.
Can someone help me understand the following code expression, how would you read it?
let myVar = (someOptional != nil) ? someOptional! : ""
If someOptional is not nil unwrap it? if this is correct, what does do the ? and the :"" in the expression?
I have always unwrapped my optionals with if lets but and I'm not sure how the above code reads.
Here is a more concrete real code example that shows how it is used...
let currentZipCode = (placemark.postalCode != nil) ? placemark.postalCode! : ""

Optionals in Swift - not assigning default value for optionals [duplicate]

This question already has answers here:
Why optional constant does not automatically have a default value of nil [duplicate]
(2 answers)
Constant unassigned optional will not be nil by default
(1 answer)
Closed 5 years ago.
let company : String? // = nil
if company == nil {
var newCompany = company ?? "apple"
}
In this code identifier company we need to assign as
let company : String? = nil
but apple documentation is telling that no need for assigning nil value
You declared a constant without a value and without an initializer that can change it, which is nonsensical. The compiler, therefore, warns you that you should initialize the constant.
Change let to var and your code will work fine.

How can I get the string value in Optional() in swift, which is followed by nil? [duplicate]

This question already has answers here:
What is an optional value in Swift?
(15 answers)
Closed 5 years ago.
How can I get the string value in Optional() in swift, which is followed by nil?any other proper way to use this evaluateJavaScript() and get the callback value?
webView.evaluateJavaScript("(function() { return 'this'; })();",
completionHandler: {
result in
//Handle your variable
print(result)//(Optional(this), nil)
})
Use if let to parse the optional value:
if let value = result {
print(value)
}

exc_bad_instruction in Swift 3.0 [duplicate]

This question already has answers here:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?
(16 answers)
Closed 5 years ago.
I don't understand why it is coming.
Are you sure that object for key "quantity" is array of Doubles? You force unwrapping results so make sure that it is [Double]
Try this
guard let quantity: [Double] = salesByStateResponse.value(forKey: "quantity") as? [Double] else {
return
}