Coder
Coder

Reputation: 529

'[String]' does not have a member named 'text' Swift

I been trying to resolved this issue for awhile and haven't found any solution yet. '[String]' does not have a member named 'text' at let c:Int? = conversion.text.toInt(). I know it has to do with the string being a floating point. What would be the fix?

 @IBOutlet var pickerView1: UIPickerView!

        @IBOutlet var textField1: UITextField!
        @IBOutlet weak var enterUnits: UITextField!
        @IBOutlet var answerConversion: UITextField!
        var measurements = ["m to feet","cm to inches", "mm to inches"]
        var conversion = ["3.12","4.54","5.23"]    

pickerview

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
            if pickerView.tag == 0 {

                let a:Int? = enterUnits.text.toInt()
                let c:Int? = conversion.text.toInt()

                let answer = (a!) * (c!)
                textField1.text = measurements[row]
                answerConversion.text = conversion[row]
                answerConversion.text = "\(answer)"

                }

Upvotes: 0

Views: 389

Answers (2)

saurabh
saurabh

Reputation: 6775

You can declare the array of Double instead of String

 let conversion = [3.12,4.54,5.23] 

And in pickerView you can access like

let c = conversion[row]

Upvotes: 1

tskulbru
tskulbru

Reputation: 2346

[String] doesnt have a property text, which is why you get your error. You need to call your [String] with a index before you can cast it to a int.

Example:

myStringArray[myIndex].toInt()

Upvotes: 1

Related Questions