Reputation: 49
i wrote a code to convert string to double but gives error :
Cannot convert the expression's type 'Double' to type 'NSString'
and my code is :
var first = previewLable.text
var second = label1.text
var number1: Double = (first as NSString).doubleValue
var number2: Double = (second as NSString).doubleValue
what i have to do to fix this error ???
Thanks
Upvotes: 2
Views: 6133
Reputation: 14993
Also if you're in Swift 2 you can use
Double("3.141592")
which gives an Optional Double, depending on whether it could be parsed
Double("hi")
would give nil
Upvotes: 13
Reputation: 71862
You just need to unwrap it like this:
var number1: Double = (first! as NSString).doubleValue
var number2: Double = (second! as NSString).doubleValue
Or you can use this way which is safe:
if let first = previewLable.text, second = lable1.text{
var number1: Double = (first as NSString).doubleValue
var number2: Double = (second as NSString).doubleValue
}
And for info about ?
and !
read this.
Upvotes: 2