[Swift 1] Day 4 doubleValue and NSString error


#1

I’m reached Lesson 4.11 and I’m completely stuck. I entered the following:

var amount = (amountTextField.text as NSString).doubleValue

as part of the lesson to get the tip calculator to work, and receive this error: ‘String?’ is not convertible to ‘NSString’

I’m really frustrated because I’m copying the lesson exactly (at least I think I am).

Anyway, I tried searching here and elsewhere and I’ve hit a wall. What am I doing wrong? Thanks!


#2

I haven’t looked into it, but I know the lessons came out before Swift 1.2 update.
Have you tried using optionals or force unwrapping ? ( like as? or as! )
You can try and break it down into two liner:

if let amountAsNsString = amountTextField.text as? String {
var amount = amountAsNsString.doubleValue

again…I haven’t tried it, but it’s gotta be something like that…

Gluck!


#3

Thanks for the quick reply! I very much appreciate your help.

Unfortunately, I get a “Downcast from String? to String only unwraps optionals”

Sigh…I wish I knew what I was doing so my brain wasn’t in such a fog. I’ll keep plugging away in hopes of figuring this out.

Thanks again! :grinning:


#4

Sorry, can you try and replace the as? to as!


#5

No need to apologize! :smile:

This is what I placed:

if let amountAsNsString = amountTextField.text as! String {
var amount = amountAsNsString.doubleValue

and it returns the same error as before:
Downcast from 'String?'to ‘String’ only unwraps optionals; did you mean to use “!”?


#6

I don’t know why I put String there…you have a String that you are trying to convert to NSString correct ?
Replace String with NSString…


#7

@Venom

just got a chance to look at it.
I opened up my old example and it’s actually still working.
I thought they changed it with Swift 1.2 , but I was thinking of something slightly different.

Mine is compiling without a problem.
The only difference is that I first declare my variable as a Double and then use it.
So I do:

var amount: Double!
or you can do
var amount = Double(50)
and then you can do:
amount = (amountTextField.text as NSString).doubleValue

However I tried it your way too, where you declare and use it at the same time and I
was able to do it without the compiler complaining about anything…

So I looked at your code again and now I see that I’ve totally missed what the problem is :slight_smile:
Problem is that your text field is an optional value - basically you are not guaranteeing that it has any value. So the compiler it’s just telling you that it cannot convert it if there is no value.
So either do a ‘if let’ first, or just make sure you have a value in it and force unwrap the .text using ‘!’

I would say it’s probably best to check. ( my last comment )

Just do:

if let amountAsNsString = amountTextField.text as! NSString {
var amount = amountAsNsString.doubleValue


#8

I can’t thank you enough for the help.

Try as I might, I’m getting error upon error. So, I decided to download the class project and run the code as presented and it worked. So, I must have done something, at some point, that messed with everything. I’m admitting defeat in this regard and moving forward with the class using Paul’s code. It’s frustrating but a learning experience.

Thanks again for your help!!! :grinning:

Onward through the fog…


#9

no problem, still curious to what the problem is.
Wanna post your full code ? The one that didn’t work ?


#10

I would but I trashed it, sorry.

Instead, I decided to walk-through the lesson for Day 4 again…from the start. This time, I was able to get everything to work properly. So, obviously, it was a short circuit between my chair and keyboard. :wink:

Thanks so much for your help! It’s always a calming thing knowing there’s help on the forum!!