Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I turn the input from a UITextField into a Double?

So I've been trying to use the input from a UITextField to create a new variable that is a double. I've had no success

Here is the variable that I am using:

class FirstViewController: UIViewController {

    @IBOutlet weak var circleRadiusTextField: UITextField!

   var circleRadiusDouble = circleRadiusTextField.text.doubleValue

}

"var circleRadiusDouble = circleRadiusTextField.text.doubleValue" is the problem.

It returns the error 'FirstViewController.Type' does not have a member named 'circleOriginXTextField'

If anyone could find a solution, it would be greatly appreciated

1 Answer

You need to cast it as a double. I loaded up playground as demonstrated this.

import UIKit

let notDouble: Int = 5

let double: Double = Double(notDouble)

,,,

When I tried that, it replied with "String cannot be turned to Double" I solved the problem, turns out I was initilazing the variable in the incorrect place

Something tells me it's not that. Basically anything you put into the UITextField will produce a string. Try casting the UiTextField's text as an Int.