-3

There has been same titles of this question but different situations. In this case this is very simple but I can't find a same problem online. So here's the code

class ViewController: UIViewController {

@IBOutlet weak var fldTotalUnits: UITextField!

var intTotalUnits:Int? = Int(fldTotalUnits)

The error here says "Cannot use instance member 'fldTotalUnits' within property initializer;..." I tried replacing var with let, I tried NSString, I tried .toInt() but nothign worked... so how do I this?

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Geni-sama
  • 307
  • 5
  • 15
  • 1. put that `Int` creation code into some `func` 2. use the `text` property of the textfield instead. – luk2302 Mar 21 '17 at 13:37
  • 1
    There are [plenty of existing questions/answers](http://stackoverflow.com/search?q=%5Bswift%5D+Cannot+use+instance+member+within+property+initializer) on that error. – rmaddy Mar 21 '17 at 13:55
  • Possible duplicate of [How to initialize properties that depend on each other](http://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other) – rmaddy Mar 21 '17 at 13:56

2 Answers2

1

String to Int conversion is not complicated. You simply do the conversion at the wrong place. You are trying to reference one member in the initialization of another member, that is not allowed. In this particular case simply because fldTotalUnits has the value nil when you would try to use it via Int(fldTotalUnits). When creating an instance of your class ViewController fldTotalUnits is set to nil and initialized with a useful value later. Therefore what you have to do in the first place is move the line into a separate method:

func doSomething() {
    var intTotalUnits:Int? = Int(fldTotalUnits)
}

Now you will see that the compiler complains about there not being a suitable initializer because you have to access the text property of fldTotalUnits instead of using the actual textfield:

func doSomething() {
    var intTotalUnits:Int? = Int(fldTotalUnits.text!)
}

Now you can think about moving the declaration of intTotalUnits to somewhere else, but setting its value has to happen in some method.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • you're actually right! i have to put the conversion inside a function and add that .text! OH MY GOOOOOD! You just solved my problem that's giving me pain for hours.. I LOVE YOU MAN THANK YOU FOR BEING ALIVE!!! – Geni-sama Mar 21 '17 at 13:57
  • `var intTotalUnits: Int { return Int(fldTotalUnits.text!) ?? 0}` – Leo Dabus Mar 21 '17 at 13:58
  • 1
    @LeoDabus yeah, sure. There was already an answer for that which somehow got downvotes and was deleted - I wanted to explain what went wrong here since OP got confused in the first place. – luk2302 Mar 21 '17 at 13:59
  • if i may ask, are those variables declared inside the function accessible throughout the program? – Geni-sama Mar 21 '17 at 14:01
  • variables declared inside the function are accessible only inside the function – Leo Dabus Mar 21 '17 at 14:02
  • @Geni-sama that is why I wrote the last sentence, you might consider using Leo's comment instead anyway since it probably does *exactly* what you want. – luk2302 Mar 21 '17 at 14:03
  • i tried the code, it's running, however, there's a warning that says "initialization of variable 'intTotalUnits' was never used; consider replacing..." is this okay? should i just ignore this? – Geni-sama Mar 21 '17 at 14:05
  • add a `print(intTotalUnits)` inside viewDIdLoad or use it in another method – Leo Dabus Mar 21 '17 at 14:07
0

The code in your question is trying to create an Int from a UITextField, not a String. What you should say is something like…

var intTotalUnits:Int?


func updateTotalUnits()
    guard let text = fldTotalUnits.text else { return }

    intTotalUnits = Int(text)    
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160