0

Sorry if my question has a ridiculously simple answer, but I'm new at this, so I have no idea what to do.

I am making an app that tells you how much medicine an animal needs based on their weight, etc. For the LRS (a type of medicine), the formula is

30 * weight * factor + 70 * factor + dehydration + weight

So in my code, I typed

lrs24.text == 30 * &weight * &factor + 70 * &factor + &deh + &weight;

The compiler said that "Expression was too complex to be solved in reasonable time..." What does this mean, and how can I fix it? Maybe I just typed in some wrong code? Thanks!

PS- The compiler says to break up the expression into smaller sub-expressions, but I don't know how to do that :(

PPS- Here is my full code:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var animalNum: UITextField!
@IBOutlet weak var logLabel: UILabel!
@IBOutlet weak var resetButton: UIButton!


@IBOutlet weak var weight: UITextField!
@IBOutlet weak var deh: UITextField!
@IBOutlet weak var losses: UITextField!
@IBOutlet var factor: [UITextField]!

@IBOutlet weak var lrs24: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Handle the text field’s user input through delegate callbacks.
    animalNum.delegate = self
}

// MARK: UITextFieldDelegate

func textFieldShouldReturn(textField: UITextField) -> Bool {
    // Hide the keyboard.
    textField.resignFirstResponder()
    return true
}

func textFieldDidEndEditing(textField: UITextField) {
    logLabel.text = textField.text
}

// MARK: Actions
@IBAction func setDefaultLabelText(sender: UIButton) {
    logLabel.text = "Default Text"
}


@IBAction func textFieldsDidEndEditing(sender: AnyObject) {
    lrs24.text == (30 * weight + 70) * factor + weight * deh * 10 + losses;





}

}

2 Answers2

2

First of all, the difference between = and == is that the first one is an assignment of the value on the right side to whatever is on the left side, while the second one is an equality test between whatever is on the left side against whatever is on the right side. In other words, the first one is an operation while the second is a True/False condition.

You appear to be trying to concatenate a string while doing the arithmetic for the values of the components of the string at the same time. Not that that's a problem, but the reference given above by John Hascall indicates that the error is a known problem when concatenating strings in Swift. A solution, as John Hascall also points out, would be to simplify the equation. I would suggest getting the values first though arithmetic, put each in a String and then concatenate those Strings.

Now, you cannot get the values of the UITextField by simply referring to the name of the UITextfield. It's an object that has properties, and one of those properties is the value of the text as a number (if you test first to make sure it is a number). Read up on accessing the properties of a UITextfield and getting a valid number in order to figure out the code for that aspect.

Insert the values for (30 * weight * factor), (70 * factor), dehydration and weight each into a String (which should avoid the error you're getting) and then concatenate those strings into your final result.

GlennRay
  • 959
  • 9
  • 18
0

Smaller subexpressions means going from stuff like:

a = b * c + d / 4 + e * (f/g);

to:

bc = b * c;
dq = d / 4;
fg = (f/g);
a = bc + dq + (e * fg);

Not sure why that would help here though.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
  • What if I named a new variable, var lrs24 = (30 * &weight * &factor + 70 )* &factor + &deh + &weight;, then I said lrs24.text = &lrs24 –  Dec 31 '15 at 22:23
  • This answer http://stackoverflow.com/questions/29707622/bizarre-swift-compiler-error-expression-too-complex-on-a-string-concatenation may prove helpful -- basically, it's a quirk of swift. – John Hascall Dec 31 '15 at 22:26
  • I'm sorry, but I'm only 12, I learned almost everything I know about code online, and I just started 3 months ago. Can someone please simply tell me what's wrong with my code, and tell me how to write it properly? –  Dec 31 '15 at 22:48
  • I've been trying to solve this for nearly an hour, and I'm eager to have results... –  Dec 31 '15 at 22:49
  • I am not a swift expert by any means, but my suggestion would be the same in any language. Start by breaking the problematic line into pieces, each on their own line, until the compiler tells you which little bit is really the problem. `A = 30 * &weight;` then `B = &factor + 70;` ...etc... – John Hascall Dec 31 '15 at 22:53
  • When I do that, the compiler says "Use of unresolved identifier a". Do I need to type the smaller pieces in a certain part of my code, like all css goes into the "header", or all html goes into the "body"? –  Dec 31 '15 at 23:23
  • You will need to declare each of the variables that hold a part of the result: A, B, etc... – John Hascall Dec 31 '15 at 23:24