10

I've been trying to make an app in Xcode 7 using Swift and I'm trying to convert numbers inside a textfield to an integer using this code, let a = Int(percentLabel.text!)

But everytime I try this I get an error that says Instance Member 'percentLabel' cannot be used on type 'ViewController'

I'm not sure why I'm getting this error, maybe it's a bug which I've already found in Xcode 7.

Here is my full code.

import UIKit

class ViewController: UIViewController {
    @IBOutlet var tipSlider: UISlider!
    @IBOutlet var splitSlider: UISlider!
    @IBOutlet var billAmount: UITextField!
    @IBOutlet var totalLabel: UILabel!
    @IBOutlet var tipTotal: UILabel!
    @IBOutlet var totalPerPerson: UILabel!
    @IBOutlet var percentLabel: UILabel!
    @IBOutlet var splitLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    let a = Int(percentLabel.text!)

    @IBAction func tipChanged(sender: AnyObject) {
        let currentValue = Int(tipSlider.value)
        percentLabel.text = "\(currentValue)"
    }

    @IBAction func splitChanged(sender: AnyObject) {
        let currentValue = Int(splitSlider.value)
        splitLabel.text = "\(currentValue)"
    }

    @IBAction func amountChanged(sender: AnyObject) {


    }
}

2 Answers 2

12

You cannot have these two lines outside of a function call in a class

let myString = percentLabel
let myInt: Int? = Int(myString)

You cannot intialise a property to a value of another property, you cannot set a variable's default value to that of another property. you would usually do this in a function like viewDidLoad or viewWillAppear. Arsen's answer should also work as it will try to get the value lazily, or as you request it.

let myString = percentLabel would also not return the text, you'd need to do let myString = percentLabel.text but you cannot assign this value like a property. you'll need to put his code in the body of a function

Sign up to request clarification or add additional context in comments.

4 Comments

I made a change to the code, instead of of let myString = percentLabel let myInt: Int? = Int(myString) I meant to have let a = Int(percentLabel.text!) which gives me the same error. I would appreciate if you could explain it a bit more because I am not experienced in Swift or programming
I've updated the answer. It's just because you are creating a property on the class that depends on a value of another property. I don't think you can do that.
Actually, I think I've gotten it figured out. Thanks so much!
Arsens answer should do the trick for you in this case. otherwise it would depend on WHEN you want the value set.
9

This code returns a new value every time you call it. I expect it what you're looking for

var myInt: Int? {
    return Int(percentLabel.text!)
}

instead of

let myString = percentLabel
let myInt: Int? = Int(myString)

Comments