Xcode - Swift compiler error: unable to convert expression type to "Double" to type "Double"

How to convert label output string to double in swift?

I have a timer that updates the label: labelOutletForSecondsCount.text. The label is initialized with the string "0.00". I, than wanted to keep the stopped timeString (I get from my timer) as Double in a variable, to use it in some calculations.

I thought this line of code should do this, but I get the following "Swift compiler error: I cannot convert the type of the expression" Double "to the type" Double ".

var timeStringIntoDoubleValue = (labelOutletForSecondsCount.text as NSString).doubleValue

He did a great job with this line of code:

var textFieldInsertLengthIntoDoubleValue = (textFieldInsertLength.text as NSString).doubleValue

But why does this not work with labelOutletForSecondsCount.text?

I should mention that I am doing this in computed properties:

var length : Double {
    var textFieldInsertLengthIntoDoubleValue = (textFieldInsertLength.text as NSString).doubleValue
    return textFieldInsertLengthIntoDoubleValue
}

var time : Double {
    var timeStringIntoDoubleValue = (labelOutletForSecondsCount.text as NSString).doubleValue
    return timeStringIntoDoubleValue
}

: "" "".

+4
1

. text UILabel :

var text: String? // default is nil

NSString. :

var timeStringIntoDoubleValue = (labelOutletForSecondsCount.text! as NSString).doubleValue

, text nil. " ":

var timeStringIntoDoubleValue = 0.0
if let text = labelOutletForSecondsCount.text {
    timeStringIntoDoubleValue = (text as NSString).doubleValue
}

"nil-coalescing operator" ??, :

var timeStringIntoDoubleValue = (labelOutletForSecondsCount.text ?? "0" as NSString).doubleValue

labelOutletForSecondsCount.text ?? "0" () nil "0" .


, , , text UITextField :

var text: String! // default is nil
+2

Source: https://habr.com/ru/post/1568446/


All Articles